Begin with parallel arrays and initialize array next as shown in class.
#define N 8
char data[N][21]; // array of up to eight strings
int next[N], // holds "pointers" for two linked lists
first = -1, // "points" to first alphabetic string, -1 if empty
avail = 0; // "points" to next available slot, -1 if full
int i; // loop control variable
/* Initialize array with "pointers" to next available slot in data */
for(i = 0; i < N-1; i++)
next[i] = i + 1;
next[N - 1] = -1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Data Abstraction Operations
operation result
+-----+---+
add Mel 0 | Mel | 1 | first: 3
add Ron +-----+---+
add Gab 1 | Ron | 7 | avail: -1 (data array is full)
add Ann +-----+---+
add Cal 2 | Gab | 0 |
add Zeb +-----+---+
add Tam 3 | Ann | 4 |
add Sue +-----+---+
4 | Cal | 2 |
+-----+---+
5 | Zeb | -1|
+-----+---+
6 | Tam | 5 |
+-----+---+
7 | Sue | 6 |
+-----+---+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
operation result
+-----+---+
delete Mel 0 | Mel | -1| first: 3
delete Ron +-----+---+
delete Ann 1 | Ron | 0 | avail: 1
add Bea +-----+---+
2 | Gab | 7 |
+-----+---+
3 | Bea | 4 |
+-----+---+
4 | Cal | 2 |
+-----+---+
5 | Zeb | -1|
+-----+---+
6 | Tam | 5 |
+-----+---+
7 | Sue | 6 |
+-----+---+
In order, the data indexes are: | In order, the available slots are:
|
3 4 2 7 6 5 | 1 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Study the example above; add and delete other names to the above list.
Practice until you are convinced that you understand the algorithm.