// D. Scott // CCI - Problem 3.1 // Implement 3 stacks using one array public static void main (String[] args) { // create array, set indices (st.1 at start, 3 at end, 2 at midpoint) int[] array = new int[75]; int i1 = -1; int i3 = array.size(); int i2 = array.size()/2; // Push 5 values onto stack 2 System.out.println("Push 5 values onto stack 2:\n"); for (int i = 1; i < 6; i++) { i2 = push2(array,i,i1,i2); } printArray(array,i1,i2,i3); // Push 11 values onto stack 3 System.out.println("Push 11 values onto stack 3:\n"); for (int i = 1; i < 12; i++) { i3 = push3(array,i*2,i2,i3); } printArray(array,i1,i2,i3); // Push 22 value onto stack 1: System.out.println("Push 22 values onto stack 1:\n"); for (int i = 1; i < 23; i++) { i1 = push1(array,i+5,i1,i2); } printArray(array,i1,i2,i3); // Pop 1 value from stack 1: System.out.println("Pop a value from stack 1:\n"); int value1 = pop1(array,i1,i2); if (value1 > 0){ i1++; System.out.println("\tPopped value from stack 1: " + value1 + "\n"); } printArray(array,i1,i2,i3); // Pop 4 values from stack 2: System.out.println("Pop 4 values from stack 2:\n") for (int i = 0; i < 4; i++) { int value2 = pop2(array,i2,i1); if (value2 > 0){ i2++; System.out.println("\tPopped value from stack 2: " + value2 + "\n"); } } printArray(array,i1,i2,i3); // Pop 7 values from stack 3: System.out.println("Pop 7 values from stack 3:\n") for (int i = 0; i < 7; i++) { int value3 = pop3(array,i3,i2); if (value3 > 0){ i3++; System.out.println("\tPopped value from stack 3: " + value3 + "\n"); } } printArray(array,i1,i2,i3); // Push 3 values onto stack 3: System.out.println("Push 3 values onto stack 3 (123,45,256):\n") i3 = push3(array,123,i2,i3); i3 = push3(array,45,i2,i3); i3 = push3(array,256,i2,i3); printArray(array,i1,i2,i3); // Pop 1 value from stack 3: System.out.println("Pop 1 value from stack 3:\n") int value3 = pop3(array,i3,i2); if (value3 > 0){ i3++; System.out.println("\tPopped value from stack 3: " + value3 + "\n"); } printArray(array,i1,i2,i3); } // Display the array(stacks w/separators) void printArray(int[] a, int i1, int i2, int i3) { for (int i = 0; i < a.size(); i++) { if (i == i2 || i == i3) System.out.print(" | "); System.out.print(a[i]+" "); } System.out.println("\n"); } int pop1(int[] a, int i1, int i2) { if (i1 >= 0 && i1 < i2) { int res = a[i1]; a[i1++] = 0; return res; } else { return -1; } } int pop2(int[] a, int i2, int i1) { if (i2 < a.size()/2) { int res = a[i2]; a[i2++] = 0; return res; } else { return -1; } } int pop3(int[] a, int i3, int i2) { if (i3 > i2) { int res = a[i3]; a[i3++] = 0; return res; } else { return -1; } } int push1(int[] a, int v, int i1, int i2) { if (i1+1 < i2) { a[++i1] = v; return i1; } else { System.out.println("Stack 1 is full. Pop something first."); return i1; } } int push3(int[] a, int v, int i2, int i3) { if (i3-1 > a.size()/2) { a[--i3] = v; return i3; } else { System.out.println("Stack 3 is full. Pop something first."); return i3; } } int push2(int[] a, int v, int i1, int i2) { if (i2-1 > i1) { a[--i2] = v; return i2; } else { System.out.println("Stack 2 is full. Pop something first."); return i2; } }