Advertisement
Guest User

Untitled

a guest
May 28th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Stack
  6. {
  7. protected:
  8. const int maxSize = 6;
  9. int *elements;
  10. int top;
  11.  
  12. public:
  13. Stack()
  14. {
  15. top = -1;
  16. elements = new int [maxSize];
  17. }
  18. Stack(const Stack &v)
  19. {
  20. top = v.top;
  21. elements = new int[maxSize];
  22. for (int i = 0; i < top; i++)
  23. elements[i] = v.elements[i];
  24. }
  25.  
  26. virtual bool isFull()
  27. {
  28. return top+1>= maxSize;
  29. }
  30.  
  31. bool isEmpty()
  32. {
  33. return top <= -1;
  34. }
  35.  
  36. virtual void push(int value)
  37. {
  38. if (Stack::isFull())
  39. {
  40. cout << "Stack overflow" << endl;
  41. return;
  42. }
  43. elements[++top] = value;
  44. }
  45.  
  46. virtual void pop()
  47. {
  48. if (isEmpty())
  49. {
  50. cout << "Stack is empty" << endl;
  51. return;
  52. }
  53. top--;
  54. }
  55.  
  56. void show()
  57. {
  58. for (int i = 0; i <= top; i++)
  59. cout << elements[i] << " ";
  60. cout << endl;
  61. }
  62.  
  63. };
  64.  
  65.  
  66. class Queue : public Stack
  67. {
  68.  
  69. public:
  70. Queue() : Stack() {}
  71. Queue(const Queue &v) : Stack(v) {}
  72.  
  73. virtual void push(int value)
  74. {
  75. if (isFull())
  76. {
  77. cout << "Queue is full" << endl;
  78. return;
  79. }
  80. Stack::push(value);
  81. }
  82.  
  83. virtual void pop()
  84. {
  85. if (isEmpty())
  86. {
  87. cout << "Queue is empty" << endl;
  88. return;
  89. }
  90. for (int i = 0; i < top; i++)
  91. elements[i] = elements[i+1];
  92. top--;
  93. }
  94.  
  95. };
  96.  
  97. class RingBuffer : public Queue
  98. {
  99. int bottom;
  100. public:
  101. RingBuffer() : Queue()
  102. {
  103. bottom = 0;
  104. }
  105. RingBuffer(const RingBuffer &v) : Queue(v)
  106. {
  107. bottom = v.bottom;
  108. }
  109.  
  110. void push(int value)
  111. {
  112. if (Stack::isFull())
  113. {
  114. elements[bottom] = value;
  115. bottom++;
  116. }
  117. else
  118. Stack::push(value);
  119. }
  120.  
  121. void pop()
  122. {
  123. if (isEmpty())
  124. {
  125. cout << "Ring Buffer is empty" << endl;
  126. }
  127. else if (bottom<=top)
  128. {
  129. for (int i = bottom; i <maxSize-1; i++)
  130. elements[i] = elements[i+1];
  131. top--;
  132. }
  133. else
  134. Queue::pop();
  135.  
  136. }
  137.  
  138. };
  139.  
  140. const int SIZE = 7;
  141.  
  142. int main()
  143. {
  144. int m[SIZE] = { 5, 3, 6, 9, 3, 9 ,0};
  145.  
  146. cout << "---Stack---" << endl;
  147. Stack stack;
  148.  
  149. cout << "Init" << endl;
  150. for (int i = 0; i < SIZE; i++)
  151. stack.push(m[i]);
  152. stack.show();
  153.  
  154. cout << "Pop 2 times" << endl;
  155. stack.pop();
  156. stack.pop();
  157. stack.show();
  158.  
  159. cout << "---Queue---" << endl;
  160. Queue queue;
  161.  
  162. cout << "Init" << endl;
  163. for (int i = 0; i < SIZE; i++)
  164. queue.push(m[i]);
  165. queue.show();
  166.  
  167. cout << "Pop 2 times" << endl;
  168. queue.pop();
  169. queue.pop();
  170. queue.show();
  171.  
  172. cout << "---Ring buffer---" << endl;
  173. RingBuffer ringBuffer;
  174.  
  175. cout << "Init" << endl;
  176. for (int i = 0; i < SIZE-1; i++)
  177. ringBuffer.push(m[i]);
  178. ringBuffer.show();
  179. cout << "Push ";
  180. int n[4] = { 7,8, 2, 1 };
  181. for (int i = 0; i < 4; i++)
  182. cout << n[i] << " ";
  183. cout << endl;
  184. for (int i = 0; i < 4; i++)
  185. ringBuffer.push(n[i]);
  186. ringBuffer.show();
  187.  
  188.  
  189. cout << "Pop 3 times" << endl;
  190. ringBuffer.pop();
  191. ringBuffer.pop();
  192. ringBuffer.pop();
  193. ringBuffer.show();
  194. system("pause");
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement