Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- using namespace std;
- template <class T>
- struct Element
- {
- T value;
- Element *link;
- };
- template <class T>
- class Queue
- {
- Element<T> *first,*last;
- int cnt;
- void DelQ()
- {
- Element<T> *p=first;
- while(first)
- {
- first=first->link;
- delete(p);
- p=first;
- }
- cnt=0;
- first=NULL;
- last=NULL;
- }
- public:
- Queue()
- {
- first=NULL;
- last=NULL;
- cnt=0;
- }
- void Add(T el)
- {
- Element<T> *p=new Element<T>;
- p->value=el;
- p->link=NULL;
- if(last==NULL)
- {
- first=p;
- last=p;
- cnt++;
- return;
- }
- last->link=p;
- last=p;
- cnt++;
- }
- void Del(T *el)
- {
- if(last==NULL)
- {
- cout<<"The stack is empty\n";
- el=NULL;
- return;
- }
- Element<T> *p=first;
- *el=p->value;
- first=first->link;
- delete(p);
- cnt--;
- if(cnt==0)
- last=NULL;
- }
- ~Queue()
- {
- Element<T> *p=first;
- while(first)
- {
- first=first->link;
- delete(p);
- p=first;
- }
- cnt=0;
- first=NULL;
- last=NULL;
- }
- template <class T1>
- friend ostream&operator<<(ostream&os,Queue<T1> &a);
- Queue&operator=(Queue<T> &a)
- {
- if(this!=&a)
- {
- DelQ();
- Element<T> *p=a.first;
- while(p!=a.last)
- {
- Add(p->value);
- p=p->link;
- }
- Add(p->value);
- cnt=a.cnt;
- }
- return *this;
- }
- };
- template <class T>
- ostream&operator<<(ostream&os,Queue<T> &a)
- {
- Element<T> *p=a.first;
- os<<"Your queue: ";
- while(p!=a.last)
- {
- T el;
- a.Del(&el);
- os<<el<<' ';
- p=p->link;
- }
- T el;
- a.Del(&el);
- p=p->link;
- os<<el<<endl;
- }
- int main()
- {
- Queue<int> q1;
- int el;
- q1.Add(5);
- q1.Add(6);
- q1.Add(7);
- Queue<int> q;
- q=q1;
- cout<<q1;
- cout<<q;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement