Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include "implementation.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. queue *q=new queue();
  11. q->enqueue(1);
  12. q->enqueue(2);
  13. cout << q->size()<<endl;
  14. q->enqueue(7);
  15. cout << q->get(2) << endl;
  16. cout << q->dequeue()<< endl;
  17. q->enqueue(4);
  18. for (int i=0;i<q->size();cout << q->get(i++)<< " ");
  19. cout << endl;
  20. q->test(*q);
  21. for (int i=0;i<q->size();cout << q->get(i++)<< " ");
  22. cout<< endl;
  23. cout << q->get(2)<< endl;
  24. cout << q->size()<<endl;
  25. queue *q2=new queue();
  26. q2->enqueue(6);
  27. q2->enqueue(7);
  28. q=q2;
  29. for (int i=0;i<q->size();cout << q->get(i++)<< " ");
  30. cout<< endl;
  31.  
  32. return 0;
  33. }
  34.  
  35. //
  36. #include "implementation.h"
  37. #include <iostream>
  38. #include <algorithm>
  39. using namespace std;
  40.  
  41.  
  42.  
  43. V::V()
  44. {
  45. var=-1;
  46. index=-1;
  47. }
  48. queue::~queue()
  49. {
  50. delete stack;
  51. }
  52. void queue::test(queue a)
  53. {
  54. for (int i=0;i<a.size();a.dequeue());
  55. }
  56. queue::queue()
  57. {
  58. stack = new V[cap];
  59.  
  60. }
  61. void queue::push1(int x)
  62. {
  63. stack[head1].var = x;
  64. stack[head1++].index=ind++;
  65. space--;
  66. }
  67. void queue::push2 (int x)
  68. {
  69. stack[head2].var = x;
  70. stack[head2--].index=ind++;
  71. space--;
  72. }
  73. int queue::pop1()
  74. {
  75. ind--;
  76. return stack[--head1].var;
  77. }
  78.  
  79. int queue::pop2 ()
  80. {
  81. ind--;
  82. return stack[++head2].var;
  83.  
  84. }
  85.  
  86. int queue::size()
  87. {
  88. return head1 -head2 +cap-1;
  89. }
  90. void queue::enqueue(int i)
  91. {
  92. push1(i);
  93. }
  94. int queue::dequeue()
  95. {
  96. if (head2==cap-1)
  97. {
  98. while (!head1!=0)
  99. {
  100. push2(pop1());
  101. }
  102. }
  103. return pop2();
  104. }
  105. int& queue::get(int j)
  106. {
  107. int a=NULL;
  108. for (int i=0;i<cap;i++)
  109. if (stack[i].index==j)
  110. return stack[i].var;
  111. cout<< "нет такого индекса" <<endl;
  112. return a;
  113. }
  114. queue::queue(const queue &obj)
  115. {
  116. stack=new V[obj.cap];
  117. cap=obj.cap;
  118. head1=obj.head1;
  119. head2=obj.head2;
  120. ind=obj.ind;
  121. std::copy(obj.stack,obj.stack+20,stack);
  122. }
  123. queue& queue::operator=(const queue &obj)
  124. {
  125. cap=obj.cap;
  126. head1=obj.head1;
  127. head2=obj.head2;
  128. ind=obj.ind;
  129. std::copy(obj.stack, obj.stack+cap,stack);
  130. }
  131.  
  132. //
  133. #ifndef IMPLEMENTATION_H
  134. #define IMPLEMENTATION_H
  135.  
  136. class V
  137. {
  138. public:
  139. int var,index;
  140. V();
  141. };
  142.  
  143. //template<typename T>
  144. class queue
  145. {
  146. public:
  147. queue();
  148. int size();
  149. int& get(int i);
  150. void enqueue (int i);
  151. int dequeue ();
  152. queue&operator=(const queue &obj);
  153. queue(const queue &obj);
  154. virtual ~queue();
  155. void test (queue);
  156.  
  157. private:
  158. V* stack;
  159. int cap=20, head1=0, head2=19,space=19;
  160. void push1(int x);
  161. void push2(int x);
  162. int pop1 ();
  163. int pop2 ();
  164. int ind=0;
  165.  
  166.  
  167. };
  168.  
  169.  
  170. #endif // DECLARATION_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement