Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <string.h>
- using namespace std;
- #pragma warning(disable : 4996)
- #define _CRT_SECURE_NO_WARNINGS
- class QueueIterator;
- class AbstractIterator
- {
- public:
- virtual ~AbstractIterator() {};
- virtual void Reset() = 0;
- };
- class AbstractContainer
- {
- public:
- virtual ~AbstractContainer() {};
- virtual bool IsEmpty() const = 0;
- virtual bool IsFull() const = 0;
- };
- class AbstractQueue : public AbstractContainer
- {
- public:
- virtual void push(int) = 0;
- virtual void del() = 0;
- };
- class ArrayQueue : public AbstractQueue
- {
- protected:
- int size;
- int* p;
- int head;
- int k;
- public:
- ArrayQueue()
- {
- size = 0; head = 0; k = 0;
- }
- ArrayQueue(int _size)
- {
- size = _size;
- p = new int[size];
- k = 0;
- head = 0;
- }
- ArrayQueue(ArrayQueue &q)
- {
- size = q.size;
- p = new int[size];
- k = q.k;
- head = q.head;
- }
- ~ArrayQueue()
- {
- delete[]p;
- }
- void push(int n)
- {
- if (this->IsFull())
- {
- cout << "Контейнер заполнен" << endl;
- }
- else
- {
- k++;
- p[head++] = n;
- }
- cout << "Элемент добавлен в конец:" <<n<< endl;
- }
- void del()
- {
- if (this->IsEmpty())
- {
- cout << "Контейнер пуст" << endl;
- }
- else{
- cout << "Удалили сверху: " << p[0] << endl;
- for (int i = 0; i < head; i++)
- p[i] = p[i + 1];
- k--;
- head--;
- }
- }
- bool IsEmpty()const
- {
- if (k == 0)
- return 1;
- else return 0;
- }
- bool IsFull()const
- {
- if (k == size)
- return 1;
- else return 0;
- }
- void Print()
- {
- for (int i = 0; i < head; i++)
- cout << p[i] << " ";
- cout << endl;
- }
- friend class QueueIterator;
- };
- class ArrayDequeue : public ArrayQueue
- {
- public:
- ArrayDequeue(int _size) :ArrayQueue(_size){}
- ArrayDequeue(const ArrayDequeue &d);
- ~ArrayDequeue()
- {
- }
- void pop()
- {
- if (this->IsEmpty())
- {
- cout << "Контейнер пуст" << endl;
- }
- else
- {
- k--;
- head--;
- cout << "Удалили снизу: " << p[head] << endl;
- }
- }
- void ins(int n)
- {
- if (this->IsFull())
- {
- cout << "Контейнер заполнен" << endl; return;
- }
- for (int i = head; i>0; i--)
- p[i] = p[i - 1];
- p[0] = n;
- head++;
- k++;
- cout << "\nЭлемент добавлен в начало:" <<n<< endl;
- }
- };
- class QueueIterator : public AbstractIterator
- {
- ArrayQueue _a;
- int pos;
- public:
- QueueIterator(){ pos = 0; }
- QueueIterator(ArrayQueue& a)
- {
- pos = 0;
- _a.head = a.head;
- _a.k = a.k;
- _a.size = a.size;
- _a.p = new int[_a.size];
- for (int i = 0; i < _a.head; i++)
- _a.p[i] = a.p[i];
- }
- bool InRange()
- {
- if (pos >= 0 && pos < _a.head)
- return 1;
- else return 0;
- }
- void Reset()
- {
- pos = 0;
- }
- int operator *()
- {
- return _a.p[pos];
- }
- void operator ++()
- {
- pos++;
- }
- ~QueueIterator(){};
- };
- void Print( ArrayQueue&y)
- {
- QueueIterator out(y);
- while (out.InRange() == 1)
- {
- cout << *out << " ";
- out++;
- }
- out.Reset();
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- ArrayDequeue A(8);
- int value;
- for (int i = 0; i < 6; i++)
- {
- cout << "\nЗначение > "; cin >> value;
- A.push(value);
- }
- Print(A);
- A.ins(66);
- Print(A);
- for (int i = 0; i < 5; i++)
- {
- A.del();
- A.Print();
- }
- A.pop();
- Print(A);
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment