![]() |
|
Модераторы: bsa |
![]()
|
|
| PNNA |
|
|||
|
Новичок Профиль Группа: Участник Сообщений: 5 Регистрация: 15.10.2015 Репутация: нет Всего: нет |
#include <iostream>
#include <exception> using namespace std; class LQueue { private: struct QItem { int info; QItem* next; QItem* prev; QItem(int Ainfo): info(Ainfo), next(NULL), prev(NULL) {} }; QItem *front, *rear; unsigned size; void Erase(); void Clone(const LQueue&); void DeleteItem(QItem*); void* PtrByIndex(unsigned) const; public: LQueue(): front(NULL), rear(NULL), size(0) {}; LQueue(const LQueue&); LQueue& operator = (const LQueue&); ~LQueue(); void PushF(int); void PushB(int); bool PopF(int&); bool PopB(int&); bool Pop(); bool Del(unsigned); const int& GetByIndex(unsigned) const; void SetByIndex(int,unsigned); void Browse(void ItemWork(int)) const; }; LQueue::LQueue(const LQueue& Q) { Clone(Q); } LQueue::~LQueue() { Erase(); } LQueue& LQueue::operator = (const LQueue& Q) { if (this != &Q) { Erase(); Clone(Q); } return *this; } void LQueue::Erase() { while (Pop()); size = 0; } void LQueue::Clone(const LQueue& Q) { QItem *tmp = Q.front; for (unsigned i=0; i<Q.size; i++) { PushB(tmp->info); tmp = tmp->next; } } void LQueue::PushF(int Ainfo) { QItem* tmp = new QItem(Ainfo); if (size>0) { tmp->next = front; front->prev=tmp; } else { front = tmp; rear=tmp; } front = tmp; size++; } void LQueue::PushB(int Ainfo) { QItem* tmp = new QItem(Ainfo); if (size>0) { rear->next = tmp; tmp->prev=rear; } else front = tmp; rear = tmp; size++; } bool LQueue::PopF(int& Ainfo) { if (size==0) return false; Ainfo=front->info; QItem *tmp = front; front = front->next; delete tmp; size--; if (size==0) rear = NULL; return true; } bool LQueue::PopB(int& Ainfo) { if (size==0) return false; Ainfo=rear->info; QItem *tmp = rear; rear = rear->prev; delete tmp; size--; if (size==0) rear = NULL; return true; } const int& LQueue::GetByIndex (unsigned k) const { if ((k<0) || (k>=size)) throw exception("Impossible to execute operator[]: invalid index"); QItem *tmp = front; for (unsigned i=0; i<k; i++) tmp = tmp->next; return tmp->info; } void LQueue::SetByIndex(int zna,unsigned k) { if ((k<0) || (k>=size)) throw exception("Impossible to execute operator[]: invalid index"); QItem *tmp = front; for (unsigned i=0; i<k; i++) tmp = tmp->next; tmp->info=zna; } void ItemWork(int HHH) { cout << HHH << " "; } void LQueue::Browse(void ItemWork(int)) const { QItem *tmp = front; for (unsigned i=0; i<size; i++) { ItemWork(tmp->info); tmp = tmp->next; } } void LQueue::DeleteItem(QItem* jj) { QItem* tmp=jj; (jj->prev)->next=jj->next; (jj->next)->prev=jj->prev; delete tmp; } void* LQueue::PtrByIndex(unsigned k) const { if ((k<0) || (k>=size)) throw exception("Impossible to execute operator[]: invalid index"); QItem *tmp = front; for (unsigned i=0; i<k; i++) tmp = tmp->next; return tmp; } bool LQueue::Del(unsigned k) { QItem* mmp=(QItem*)PtrByIndex(k); DeleteItem(mmp); return true; } bool LQueue::Pop() { if (size==0) return false; QItem *tmp = front; front = front->next; delete tmp; size--; if (size==0) rear = NULL; return true; } int main() { try { setlocale(LC_ALL,".1251"); LQueue kk; int h; kk.PushF(7); kk.PushF(77); kk.PushF(73); kk.PushB(70); kk.PushF(75); kk.PushF(79); cout << "Создание: "; kk.Browse(ItemWork); cout << endl; cout << "Оператор присваивания: "; LQueue jj; jj=kk; jj.Browse(ItemWork); cout << endl; cout << "Конст. коп.: "; LQueue jjk(kk); jjk.Browse(ItemWork); } catch(exception e) { cout << e.what() << endl; } } |
|||
|
||||
| feodorv |
|
|||
![]() Эксперт ![]() ![]() ![]() ![]() Профиль Группа: Комодератор Сообщений: 2214 Регистрация: 30.7.2011 Репутация: 12 Всего: 45 |
Наверное, у Вас просто не проинициализированы внутренние поля класса LQueue:
-------------------- Напильник, велосипед, грабли и костыли - основные инструменты программиста... |
|||
|
||||
| end_fall |
|
|||
|
Unregistered |
Модератор: Сообщение скрыто. |
|||
|
||||
| PNNA |
|
|||
|
Новичок Профиль Группа: Участник Сообщений: 5 Регистрация: 15.10.2015 Репутация: нет Всего: нет |
Спасибо большое feodorv
|
|||
|
||||
![]()
|
| Правила форума "C/C++: Для новичков" | |
|
|
Запрещается! 1. Публиковать ссылки на вскрытые компоненты 2. Обсуждать взлом компонентов и делиться вскрытыми компонентами
Если Вам понравилась атмосфера форума, заходите к нам чаще! С уважением, JackYF, bsa. |
| 0 Пользователей читают эту тему (0 Гостей и 0 Скрытых Пользователей) | |
| 0 Пользователей: | |
| « Предыдущая тема | C/C++: Для новичков | Следующая тема » |
|
|
По вопросам размещения рекламы пишите на vladimir(sobaka)vingrad.ru
Отказ от ответственности Powered by Invision Power Board(R) 1.3 © 2003 IPS, Inc. |