fork download
  1. /**
  2.  * Implement two stacks in an array of given size
  3.  * @author Prateek
  4.  */
  5. class TwoStacks {
  6.  
  7. //private enum STACKS {STACK1,STACK2}
  8. private int[] arr; // array holding 2 stacks
  9. private int size; // capaccity of 2 stacks
  10. private int top1; // top pointer of 1st stack
  11. private int top2; // top pointer of 2nd stack
  12.  
  13. public TwoStacks(int capacity) {
  14. this.arr=new int[capacity];
  15. this.size= capacity;
  16. this.top1=-1;
  17. this.top2=capacity;
  18. }
  19.  
  20.  
  21. /**
  22. * Push Operation
  23. * @param stackNum 0: 1st stack , 1: 2nd stack
  24. * @param data : data to be pushed
  25. * @throws InterruptedException
  26. */
  27. public void push(int stackNum, int data) throws InterruptedException{
  28. Thread.sleep(10);
  29. if(stackNum==1){
  30. pushToStack1(data);
  31. }
  32. else if(stackNum==2)
  33. pushToStack2(data);
  34. else
  35. System.out.println("Invalid Stack Number");
  36. }
  37.  
  38. public int pop(int stackNum) throws InterruptedException{
  39. Thread.sleep(10);
  40. if(stackNum==1)
  41. return popFromStack1();
  42. else if (stackNum==2)
  43. return popFromStack2();
  44. else{
  45. System.err.println("Invalid Stack");
  46. return -1;
  47. }
  48. }
  49.  
  50. public int peek(int stackNum) throws InterruptedException{
  51. Thread.sleep(10);
  52. if(stackNum==1)
  53. return peekFromStack1();
  54. else if (stackNum==2)
  55. return peekFromStack2();
  56. else{
  57. System.err.println("Invalid Stack");
  58. return -1;
  59. }
  60. }
  61. /**
  62. * Stack maintained from Left hand side of array
  63. * stack beng pushed to stack from left to right
  64. */
  65. private void pushToStack1(int data){
  66. if(top1+1 < top2){
  67. arr[++top1]=data;
  68. System.out.println("S1 Pushed: "+data);
  69. }
  70.  
  71. else
  72. System.err.println("Stack 1 Overflow");
  73. }
  74.  
  75. /**
  76. * Stack maintained from right hand side of array
  77. * stack beng pushed to stack from right to left
  78. * @param data
  79. */
  80. private void pushToStack2(int data){
  81. if(top1 < top2-1){
  82. arr[--top2]=data;
  83. System.out.println("S2 Pushed: "+data);
  84. }
  85. else
  86. System.err.println("Stack 2 Overflow");
  87. }
  88.  
  89. /**
  90. * Element poped from left hand side stack
  91. * @return top element of left stack
  92. */
  93. private int popFromStack1(){
  94. if(top1!=-1){
  95. int val=arr[top1--];
  96. System.out.println("S1 Poped: "+ val);
  97. return val;
  98. }
  99.  
  100. System.err.println("Stack 1 is Empty");
  101. return -1;
  102.  
  103. }
  104.  
  105. /**
  106. * Element poped from right hand side stack
  107. * @return top element of right stack
  108. */
  109. private int popFromStack2(){
  110. if(top2!=size){
  111. int val=arr[top2++];
  112. System.out.println("S2 Poped: "+ val);
  113. return val;
  114. }
  115. System.err.println("Stack 2 is Empty");
  116. return -1;
  117. }
  118.  
  119. /**
  120. * Peek from stack1's top element
  121. * @return
  122. */
  123. public int peekFromStack1(){
  124. if(top1==-1){
  125. System.err.println("Stack is Empty");
  126. return -1;
  127. }
  128. return arr[top1];
  129. }
  130.  
  131. /**
  132. * Peek from stack2's top element
  133. * @return
  134. */
  135. public int peekFromStack2(){
  136. if(top2==size){
  137. System.err.println("Stack is Empty");
  138. return -1;
  139. }
  140. return arr[top2];
  141. }
  142.  
  143. public static void main(String[] args) throws InterruptedException {
  144. TwoStacks obj=new TwoStacks(6);
  145. obj.push(1,1);
  146. obj.push(2,99);
  147. obj.push(1,2);
  148. obj.push(1,3);
  149. obj.push(2,98);
  150. obj.pop(2);
  151. obj.pop(2);
  152. obj.push(2,97);
  153. obj.push(1,4);
  154.  
  155. obj.push(2,96);
  156. obj.push(1,5);
  157.  
  158. obj.pop(1);
  159. obj.pop(2);
  160.  
  161. obj.pop(2);
  162. obj.pop(2);
  163.  
  164. }
  165. }
  166.  
Success #stdin #stdout #stderr 0.07s 380160KB
stdin
Standard input is empty
stdout
S1 Pushed: 1
S2 Pushed: 99
S1 Pushed: 2
S1 Pushed: 3
S2 Pushed: 98
S2 Poped: 98
S2 Poped: 99
S2 Pushed: 97
S1 Pushed: 4
S2 Pushed: 96
S1 Poped: 4
S2 Poped: 96
S2 Poped: 97
stderr
Stack 1 Overflow
Stack 2 is Empty