Q: Need help to understand CTC book answer This if from "Cracking the Coding Interview":
Question:
Describe how you could use a single array to implement three stacks
Answer:
In this approach, any stack can grow as long as there is any free space in the array
We sequentially allocate space to the stacks and we link new blocks to the previous block This means any new element in a stack keeps a pointer to the previous top element of that particular stack
In this implementation, we face a problem of unused space For example, if a stack deletes some of its elements, the deleted elements may not necessarily appear at the end of the ar- ray So, in that case, we would not be able to use those newly freed spaces
To overcome this de ciency, we can maintain a free list and the whole array space would be given initially to the free list For every insertion, we would delete an entry from the free list In case of deletion, we would simply add the index of the free cell to the free list
In this implementation we would be able to have  exibility in terms of variable space utiliza- tion but we would need to increase the space complexity.
And code for answer:
{
     int stackSize = 300;
     int indexUsed = 0;
     int[] stackPointer = {-1,-1,-1};
     StackNode[] buffer = new StackNode[stackSize * 3];

    void push(int stackNum, int value) {     
    int lastIndex = stackPointer[stackNum];
    stackPointer[stackNum] = indexUsed;
    indexUsed++;
    buffer[stackPointer[stackNum]]=new StackNode(lastIndex,value);
    }

    int pop(int stackNum) {
    int value = buffer[stackPointer[stackNum]].value;
    int lastIndex = stackPointer[stackNum];
    stackPointer[stackNum] = buffer[stackPointer[stackNum]].previous;
    buffer[lastIndex] = null;
    indexUsed--;
    return value;
    }

    int peek(int stack) { return buffer[stackPointer[stack]].value; }
    boolean isEmpty(int stackNum) { return stackPointer[stackNum] == -1; }

 class StackNode {
 public int previous;
 public int value;
 public StackNode(int p, int v){
 value = v;
 previous = p;
 }
}

So at the beginning I would like to say I come from C++ and sorry if it is some simple java trick or something I don't know about java, but anyway will learn something new.
The problem is that when we will try to solve it by simply link stack elements in array to keep pointer to previous top el of each stack, and then we will pop some elements our array will get fragmented and if these elements are not from front of the list there will be no way to retrieve those freed chunks or array i.e.:
array:     1 2 3 2 4 6 4 8 12
stack no:  1 2 3 1 2 3 1 2 3

After we will pop from stack 2 twice we will get:
array:     1 2 3 2 - 6 4 - 12
stack no:  1 2 3 1 2 3 1 2 3

So we are loosing references to deleted chunks, and we can only add elements on the "right" side of array. Author says that this can be overcome by the code provided above. But I can't see how he suppose to do that if
he allocated array StackNode and then popping each element by setting top of stack to  appropriate previous top(stackPointer) and popped value to null then decreasing current largest element in array?? For me it will still fragment array and additionally skip some other stack top element. The only reasonable way it could work is if our array worked as list and assignment "buffer[lastIndex] = null" would delete that node from list then we could say that we have one element less in array by indexUsed--.
