Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. template <class T>
  8. struct Element
  9. {
  10. T value;
  11. Element *link;
  12. };
  13.  
  14. template <class T>
  15. class Queue
  16. {
  17. Element<T> *first,*last;
  18. int cnt;
  19. void DelQ()
  20. {
  21. Element<T> *p=first;
  22. while(first)
  23. {
  24. first=first->link;
  25. delete(p);
  26. p=first;
  27. }
  28. cnt=0;
  29. first=NULL;
  30. last=NULL;
  31. }
  32. public:
  33. Queue()
  34. {
  35. first=NULL;
  36. last=NULL;
  37. cnt=0;
  38. }
  39. void Add(T el)
  40. {
  41. Element<T> *p=new Element<T>;
  42. p->value=el;
  43. p->link=NULL;
  44. if(last==NULL)
  45. {
  46. first=p;
  47. last=p;
  48. cnt++;
  49. return;
  50. }
  51. last->link=p;
  52. last=p;
  53. cnt++;
  54. }
  55. void Del(T *el)
  56. {
  57. if(last==NULL)
  58. {
  59. cout<<"The stack is empty\n";
  60. el=NULL;
  61. return;
  62. }
  63. Element<T> *p=first;
  64. *el=p->value;
  65. first=first->link;
  66. delete(p);
  67. cnt--;
  68. if(cnt==0)
  69. last=NULL;
  70. }
  71. ~Queue()
  72. {
  73. Element<T> *p=first;
  74. while(first)
  75. {
  76. first=first->link;
  77. delete(p);
  78. p=first;
  79. }
  80. cnt=0;
  81. first=NULL;
  82. last=NULL;
  83. }
  84. template <class T1>
  85. friend ostream&operator<<(ostream&os,Queue<T1> &a);
  86. Queue&operator=(Queue<T> &a)
  87. {
  88. if(this!=&a)
  89. {
  90. DelQ();
  91. Element<T> *p=a.first;
  92. while(p!=a.last)
  93. {
  94. Add(p->value);
  95. p=p->link;
  96. }
  97. Add(p->value);
  98. cnt=a.cnt;
  99. }
  100. return *this;
  101. }
  102. };
  103.  
  104.  
  105. template <class T>
  106. ostream&operator<<(ostream&os,Queue<T> &a)
  107. {
  108. Element<T> *p=a.first;
  109. os<<"Your queue: ";
  110. while(p!=a.last)
  111. {
  112. T el;
  113. a.Del(&el);
  114. os<<el<<' ';
  115. p=p->link;
  116. }
  117. T el;
  118. a.Del(&el);
  119. p=p->link;
  120. os<<el<<endl;
  121. }
  122.  
  123. int main()
  124. {
  125. Queue<int> q1;
  126. int el;
  127. q1.Add(5);
  128. q1.Add(6);
  129. q1.Add(7);
  130. Queue<int> q;
  131. q=q1;
  132. cout<<q1;
  133. cout<<q;
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement