Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "implementation.h"
- using namespace std;
- int main()
- {
- queue *q=new queue();
- q->enqueue(1);
- q->enqueue(2);
- cout << q->size()<<endl;
- q->enqueue(7);
- cout << q->get(2) << endl;
- cout << q->dequeue()<< endl;
- q->enqueue(4);
- for (int i=0;i<q->size();cout << q->get(i++)<< " ");
- cout << endl;
- q->test(*q);
- for (int i=0;i<q->size();cout << q->get(i++)<< " ");
- cout<< endl;
- cout << q->get(2)<< endl;
- cout << q->size()<<endl;
- queue *q2=new queue();
- q2->enqueue(6);
- q2->enqueue(7);
- q=q2;
- for (int i=0;i<q->size();cout << q->get(i++)<< " ");
- cout<< endl;
- return 0;
- }
- //
- #include "implementation.h"
- #include <iostream>
- #include <algorithm>
- using namespace std;
- V::V()
- {
- var=-1;
- index=-1;
- }
- queue::~queue()
- {
- delete stack;
- }
- void queue::test(queue a)
- {
- for (int i=0;i<a.size();a.dequeue());
- }
- queue::queue()
- {
- stack = new V[cap];
- }
- void queue::push1(int x)
- {
- stack[head1].var = x;
- stack[head1++].index=ind++;
- space--;
- }
- void queue::push2 (int x)
- {
- stack[head2].var = x;
- stack[head2--].index=ind++;
- space--;
- }
- int queue::pop1()
- {
- ind--;
- return stack[--head1].var;
- }
- int queue::pop2 ()
- {
- ind--;
- return stack[++head2].var;
- }
- int queue::size()
- {
- return head1 -head2 +cap-1;
- }
- void queue::enqueue(int i)
- {
- push1(i);
- }
- int queue::dequeue()
- {
- if (head2==cap-1)
- {
- while (!head1!=0)
- {
- push2(pop1());
- }
- }
- return pop2();
- }
- int& queue::get(int j)
- {
- int a=NULL;
- for (int i=0;i<cap;i++)
- if (stack[i].index==j)
- return stack[i].var;
- cout<< "нет такого индекса" <<endl;
- return a;
- }
- queue::queue(const queue &obj)
- {
- stack=new V[obj.cap];
- cap=obj.cap;
- head1=obj.head1;
- head2=obj.head2;
- ind=obj.ind;
- std::copy(obj.stack,obj.stack+20,stack);
- }
- queue& queue::operator=(const queue &obj)
- {
- cap=obj.cap;
- head1=obj.head1;
- head2=obj.head2;
- ind=obj.ind;
- std::copy(obj.stack, obj.stack+cap,stack);
- }
- //
- #ifndef IMPLEMENTATION_H
- #define IMPLEMENTATION_H
- class V
- {
- public:
- int var,index;
- V();
- };
- //template<typename T>
- class queue
- {
- public:
- queue();
- int size();
- int& get(int i);
- void enqueue (int i);
- int dequeue ();
- queue&operator=(const queue &obj);
- queue(const queue &obj);
- virtual ~queue();
- void test (queue);
- private:
- V* stack;
- int cap=20, head1=0, head2=19,space=19;
- void push1(int x);
- void push2(int x);
- int pop1 ();
- int pop2 ();
- int ind=0;
- };
- #endif // DECLARATION_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement