3.1 Describe how you could use a single array to implement three stacks.
3 stacks? separate the array into 3 sections.
Use 3 index.
push(int stack, T t)
{
validateStackNum(stack); // Validate(stack >= 0 && stack < 2);
int pos = stackPos[stack];
check(pos + 1); // if exceeds, enlarge the array.
array[pos + 1] = t;
stackPos[stack] = pos + 1;
}
T peek(int stack)
{
validateStackNum(stack);
return stackPos[stack];
}
T poll(int stack)
{
validateStackNum(stack);
int pos = stackPos[stack];
T t = array[pos];
stackPos[stack] = pos - 1;
}
int[] stackPos;原文:http://7371901.blog.51cto.com/7361901/1581740