Advertisement
Amr1998

Untitled

Apr 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class stack
  4. {
  5. public:
  6. int *arraystack;
  7. int arraysize;
  8. int top;
  9.  
  10. stack(int);
  11. void push(int);
  12. int pop();
  13. bool isfull();
  14. bool isempty();
  15. int size();
  16. int peek();
  17. };
  18. int stack::peek()
  19. {
  20. if (!isempty())
  21. return arraystack[top];
  22.  
  23. }
  24. int stack::size()
  25. {
  26. return top + 1;
  27. }
  28. stack::stack(int size)
  29. {
  30. arraystack=new int [size];
  31. arraysize=size;
  32. top=-1;
  33. }
  34. void stack::push(int num)
  35. {
  36. if(isfull())
  37. {
  38. cout<<"the stack is full"<<endl;
  39. }
  40. else
  41. {
  42. top++;
  43. arraystack[top]=num;
  44. }
  45. }
  46. int stack::pop()
  47. {
  48. if(isempty())
  49. {
  50. cout<<"array is empty"<<endl;
  51. }
  52. else
  53. {
  54. return arraystack[top--];
  55. }
  56. }
  57. bool stack::isfull()
  58. {
  59. if(arraysize-1==top)
  60. return true;
  61. else
  62. return false;
  63. }
  64. bool stack::isempty()
  65. {
  66. if(top==-1)
  67. return true;
  68. else
  69. return false;
  70. }
  71. class queue:public stack
  72. {
  73. public:
  74. stack s1,s2;
  75.  
  76. // Enqueue function of Queue
  77. void enqueue(int val)
  78. {
  79. s1.push(val);
  80. cout << "enqueued :" << val << endl;
  81. }
  82.  
  83. // Dequeue function of Queue
  84. void dequeue()
  85. {
  86. if (s2.isempty())
  87. {
  88. while(!s1.isempty())
  89. {
  90. s2.push(s1.peek());
  91. s1.pop();
  92. }
  93. }
  94. if(!s2.isempty())
  95. {
  96. cout << "dequeued :" << s2.peek() << endl;
  97. s2.peek();
  98. }
  99. else
  100. {
  101. cout << "Underflow "<< endl;
  102. }
  103. }
  104. // Front
  105. void front()
  106. {
  107. if (s2.isempty())
  108. {
  109. while(!s1.isempty())
  110. {
  111. s2.push(s1.peek());
  112. s1.pop();
  113. }
  114. }
  115. if(!s2.isempty())
  116. {
  117. cout << "top :" << s2.peek() << endl;
  118. }
  119. else
  120. {
  121. cout << "Underflow "<< endl;
  122. }
  123. }
  124.  
  125. // Empty
  126. bool isEmpty()
  127. {
  128. if(s1.isempty() && s2.isempty())
  129. {
  130. cout << "Empty "<< endl;
  131. return true;
  132. }
  133. cout << "Not Empty "<< endl;
  134. return false;
  135. }
  136.  
  137. // Size
  138. int size()
  139. {
  140. int s = s1.size() + s2.size();
  141. cout << "size :" << s << endl;
  142. return s;
  143. }
  144.  
  145. };
  146.  
  147. int main()
  148. {
  149.  
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement