等长的相对简单,设置三个栈顶指针,考虑栈为空的情况分别push和pop就好
public class threeStacks {
int stackSize = 100;
int[] buffer = new int[ stackSize * 3 ];
int[] stackPointer = {-1, -1, -1}; //pointer to trace the stack element number in every stack
void push(int stackNum, int value) throws Exception {
//check if there's free space
if (stackPointer[stackNum] + 1 >= stackSize) { //the last element
throw new Exception ("Out of space!");
}
//stack pointer increases, update the top element value
stackPointer[stackNum]++;
buffer[absTopOfStack(stackNum)] = value;
}
int pop(int stackNum) throws Exception {
if ( -1 == stackPointer[stackNum] ) {
throw new Exception ("Trying to pop an empty stack!");
}
int value = buffer[absTopOfStack(stackNum)];
buffer[absTopOfStack(stackNum)] = 0; //mark as 0
stackPointer[stackNum]--;
return value;
}
int pe