fork download
  1. import java.util.Arrays;
  2.  
  3. /**
  4.  * divide the array into three stacks
  5.  * @author Prateek
  6.  */
  7. class MakeThreeStacks {
  8.  
  9. private int stackSize=5;
  10. private int[] tops={-1,-1,-1};
  11. private int[] arr=new int[stackSize * 3];
  12.  
  13. /**
  14. * Push operation
  15. * @param stackNum : specifies the stack number
  16. * @return: insertion status
  17. * @throws InterruptedException
  18. */
  19. public boolean push(int stackNum, int item) throws InterruptedException{
  20. if(stackNum==0 || stackNum==1 || stackNum==2){
  21. int index= stackNum * stackSize + tops[stackNum] + 1;
  22. if(tops[stackNum] < stackSize -1 ){
  23. tops[stackNum]++;
  24. arr[index]=item;
  25. Thread.sleep(10);
  26. System.out.println(Arrays.toString(arr));
  27. return true;
  28. }
  29. else{
  30. System.err.println("Stack Full");
  31. return false;
  32. }
  33. }
  34. else
  35. {
  36. System.out.println("Invalid Stack Number");
  37. return false;
  38. }
  39. }
  40.  
  41. /**
  42. * Pop operation
  43. * @param stackNum: specifies the stack number
  44. * @return the poped item
  45. * @throws InterruptedException
  46. */
  47. public int pop(int stackNum) throws InterruptedException{
  48. if(stackNum==0 || stackNum==1 || stackNum==2){
  49. if(tops[stackNum]==-1) {
  50. System.err.println("Stack is Empty");
  51. return -1;
  52. }
  53. int index= stackNum * stackSize + tops[stackNum];
  54. tops[stackNum]--;
  55. int item= arr[index];
  56. arr[index]=0;
  57. Thread.sleep(10);
  58. System.out.println(Arrays.toString(arr));
  59. return item;
  60. }
  61. else
  62. {
  63. System.out.println("Invalid Stack");
  64. return -9999;
  65. }
  66. }
  67.  
  68. /**
  69. * Peek operation
  70. * @param stackNum: specifies the stack number
  71. * @return top element of the specified stack
  72. */
  73. public int peek(int stackNum){
  74. if(stackNum==0 || stackNum==1 || stackNum==2){
  75. if(tops[stackNum]==-1) {
  76. System.out.println("Stack is Empty");
  77. return -1;
  78. }
  79.  
  80. int index= stackNum * stackSize + tops[stackNum];
  81. return arr[index];
  82. }
  83. else
  84. {
  85. System.out.println("Invalid Stack");
  86. return -99999;
  87. }
  88. }
  89.  
  90.  
  91. public static void main(String[] args) throws InterruptedException {
  92. MakeThreeStacks stack=new MakeThreeStacks();
  93. stack.push(1, 1);
  94. stack.push(3, 2);
  95. stack.push(0, 3);
  96. stack.push(1, 4);
  97. stack.push(2, 5);
  98. stack.push(2, 6);
  99. stack.push(3, 7);
  100. stack.pop(0);
  101. stack.pop(1);
  102. stack.pop(0);
  103. stack.pop(2);
  104. }
  105. }
Success #stdin #stdout #stderr 0.07s 380224KB
stdin
Standard input is empty
stdout
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Invalid Stack Number
[3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[3, 0, 0, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0]
[3, 0, 0, 0, 0, 1, 4, 0, 0, 0, 5, 0, 0, 0, 0]
[3, 0, 0, 0, 0, 1, 4, 0, 0, 0, 5, 6, 0, 0, 0]
Invalid Stack Number
[0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 5, 6, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 5, 6, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 5, 0, 0, 0, 0]
stderr
Stack is Empty