Question #1:
Can someone explain me what is happening in the below code snippet?
Answer #1:
Lets first examine how one would iterate through such a structure and print out the elements in order. I'm using the example from the book in your question:

Here's how it looks in Java:
int i = 0;
while (k[i] != 0) {
System.out.println(a[k[i]]);
i = k[i];
}
If you want to insert a value to such a structure, you have to know how to iterate it. You have to know the i of the first element (a[k[i]]) that is equal to or greater than the element you wish to insert (x):
Procedure insert(x) inserts x to a[free].
Now the indices have to be updated to accommodate an extra link in the ordered chain. That is, to go from this:
| ... | a[k[i]] >= x | ... |
to this:
| ... | x | a[k[i]] >= x | ... |
The element directly after x is a[k[i]]. We have to temporarily detach it from the chain to insert x. Therefore: k[free] = k[i].
Next, do: k[i] = free. a[k[i]] will now be x (remember, we inserted x to a[free]).
Now the magic happens. a[k[i]] == x and the element directly after it is a[k[free]] (which used to be a[k[i]] before).
Then free is incremented by one and we are done.
Question #2:
How should I initialise variable k (as an index array for a array) and what should be the values for a?
Answer #2:
The example from the book looks just fine:
import java.util.Arrays;
class IndirectReference {
static final int Q = 0; // This value is irrelevant; added for clarity
static int a[] = {Q, 44, Q, 22, 55, 33, 66, Q};
static int k[] = {3, 4, Q, 5, 6, 1, 0, Q, Q, Q};
static int free = 7;
public static void main(String[] args) {
insert(50);
System.out.print("a = ");
System.out.println(Arrays.toString(a));
System.out.print("k = ");
System.out.println(Arrays.toString(k));
System.out.println("free = " + free);
}
static void insert(int x) {
int i = 0;
while (k[i] != 0 && a[k[i]] < x) {
i = k[i];
}
a[free] = x;
k[free] = k[i];
k[i] = free++;
}
}
Note: this procedure relies on the fact that free saves the index of the next free location in both tables.
In the example above, it's wrongly set to 8 after insertion of 50 because the procedure assumes there's always enough space at indices free, free+1, free+2, ... in both a and k.
Once you run out of free space you can either resize both arrays or compact them (put non-trailing ?s at the end of both tables), though I can't quite see a clear way how to do that.