public class OneArrayThreeStack { int[] array ; int top1; int top2; int top3left; int top3right; boolean isleft ; public OneArrayThreeStack(int size){ array = new int[size]; top1= 0; top2 = size -1; top3left = size/2; top3right = size/2 +1; isleft =true; } public void push(int data, int type) throws Exception{ if(type == 1){ if(top1 == top3left + 1){ throw new Exception (" full"); } array[top1++] = data; } if(type == 2){ if(top2== top3right -1){ throw new Exception ("1 full"); } array[top2--] = data; } if(type ==3){ if(isleft){ if(top1-1 == top3left){ throw new Exception ("2 full"); } array[top3left--] = data; } else{ if(top2+1 == top3right){ throw new Exception ("3 full"); } array[top3right++] = data; } isleft =! isleft; } } public int pop(int type) throws Exception{ int res = -1; if(type == 1){ if(top1 == 0){ throw new Exception ("1 empty"); } res = array[--top1]; array[top1] = 0; } else if(type == 2){ if(top2 ==array.length-1){ throw new Exception ("2 empty"); } res = array[++top2]; array[top2] = 0; } else if(type == 3){ if(top3left == top3right -1){ throw new Exception ("3 empty"); } if(isleft){ res = array[++top3left]; array[top3left] = 0; } else{ res = array[--top3right]; array[top3right] =0; } } return res; } public void print() { System.out.print("Array:{"); for(int i=0;i<array.length;i++) System.out.print(array[i]); System.out.print("}"); System.out.println(); } public void printValue() { System.out.println(top1+"\t"+top2+"\t"+top3left+"\t"+top3right+"\t"+isleft); } public static void main(String[] args) throws Exception { int len = 9; OneArrayThreeStack ats = new OneArrayThreeStack(len); ats.push(1, 1); ats.printValue(); ats.push(2, 2); ats.printValue(); ats.push(3, 3); ats.printValue(); ats.print(); ats.pop(1); ats.printValue(); ats.pop(2); ats.print(); // ats.push(1, 1); // ats.printValue(); // ats.push(2, 2); // ats.printValue(); // ats.push(3, 3); // ats.printValue(); // ats.push(1, 1); // ats.print(); // ats.printValue(); // ats.push(2, 2); // ats.print(); // ats.printValue(); // ats.push(3, 3); // ats.printValue(); // ats.print(); // ats.print(); // // ats.pop(1); // ats.printValue(); // ats.pop(2); // ats.printValue(); // ats.pop(3); // ats.printValue(); // ats.print(); // ats.pop(1); // ats.printValue(); // ats.pop(2); // ats.printValue(); // ats.pop(3); // ats.printValue(); // ats.print(); // ats.pop(1); // ats.printValue(); // ats.pop(2); // ats.printValue(); // ats.pop(3); // ats.printValue(); // ats.print(); // ats.print(); } }