Guest User

Untitled

a guest
Apr 29th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. public class ThreeStack {
  2. private int[] stack;
  3.  
  4. private int first, last, mid;
  5. private final int midPosition;
  6.  
  7. public ThreeStack(int size) {
  8. stack = new int[size];
  9. first = -1; last = size; mid = (first + last) >> 1;
  10. midPosition = mid;
  11. }
  12.  
  13. //push
  14. public boolean pushFirstStack(final int val) {
  15. if (first + 1 < midPosition) {
  16. stack[++first] = val;
  17. return true;
  18. } else {
  19. return false;
  20. }
  21. }
  22.  
  23. public boolean pushSecondStack(final int val) {
  24. if (mid + 1 < last) {
  25. stack[++mid] = val;
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. }
  31.  
  32. public boolean pushThirdStack(final int val) {
  33. if (last - 1 > mid) {
  34. stack[--last] = val;
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. }
  40.  
  41. //pop
  42. public int popFirstStack() {
  43. if (first < 0) {
  44. return Integer.MIN_VALUE;
  45. } else {
  46. int val = stack[first];
  47. stack[first--] = 0;
  48. return val;
  49. }
  50. }
  51.  
  52. public int popSecondStack() {
  53. if (mid < midPosition) {
  54. return Integer.MIN_VALUE;
  55. } else {
  56. int val = stack[mid];
  57. stack[mid--] = 0;
  58. return val;
  59. }
  60. }
  61.  
  62. public int popThirdStack() {
  63. if (last >= stack.length) {
  64. return Integer.MIN_VALUE;
  65. } else {
  66. int val = stack[last];
  67. stack[last++] = 0;
  68. return val;
  69. }
  70. }
  71.  
  72. //size
  73. public int firstStackSize() {
  74. return mid - midPosition;
  75. }
  76.  
  77. public int secondStackSize() {
  78. return last - mid;
  79. }
  80.  
  81. public int lastStackSize() {
  82. return secondStackSize();
  83. }
  84.  
  85. public void printStack() {
  86. for (int i = 0; i < stack.length; i++) {
  87. System.out.print(stack[i]);
  88. System.out.print((i == first || i == mid || i == last) ? "*": "");
  89. System.out.print(" ");
  90. }
  91. System.out.println();
  92. }
  93. }
  94.  
  95. public class MainClass {
  96. public static void main(String[] args) {
  97. Scanner in = new Scanner(System.in);
  98. ThreeStack stack = new ThreeStack(12);
  99.  
  100.  
  101. while (true) {
  102. stack.printStack();
  103. menu();
  104. int c = in.nextInt();
  105. switch (c) {
  106. case 1:
  107. stack.pushFirstStack(in.nextInt());
  108. break;
  109. case 2:
  110. stack.pushSecondStack(in.nextInt());
  111. break;
  112. case 3:
  113. stack.pushThirdStack(in.nextInt());
  114. break;
  115. case 4:
  116. printPopVal(stack.popFirstStack());
  117. break;
  118. case 5:
  119. printPopVal(stack.popSecondStack());
  120. break;
  121. case 6:
  122. printPopVal(stack.popThirdStack());
  123. break;
  124. }
  125. }
  126.  
  127. }
  128.  
  129. static void menu() {
  130. System.out.println("+------------------------------+");
  131. System.out.println("1 -> push first stack");
  132. System.out.println("2 -> push second stack");
  133. System.out.println("3 -> push third stack");
  134. System.out.println("4 -> pop first stack");
  135. System.out.println("5 -> pop second stack");
  136. System.out.println("6 -> pop third stack");
  137. System.out.println("+------------------------------+");
  138. }
  139.  
  140. static void printPopVal(int val) {
  141. System.out.println("Pop : " + val);
  142. }
  143. }
Add Comment
Please, Sign In to add comment