% gcc -o list list.c [> dos.bat] > tcc list.c % list > h h(elp; q(uit; H(ead x; D(elete x; d(isplay; s(how_array > q
Data File with Test Cases: list.dat
h s H a s d H b s d H c s d D b s d H d s d H e s d q
Run Test Cases: %list list.dat
List: a List: b a List: c b a List: c a List: d c a List: e d c a
Array (CURSOR) Lists: s(how_array [9]
FREE LIST CREATE HEADER H(ead a H(ead b Slot Element Next Slot Element Next Slot Element Next Slot Element Next 0 - 1 0 - 2 0 - 3 0 - 4 1 - 2 1 * 0 1 * 2 1 * 3 2 - 3 2 - 3 2 a 0 2 a 0 3 - 4 3 - 4 3 - 4 3 b 2 4 - 5 4 - 5 4 - 5 4 - 5 5 - 6 5 - 6 5 - 6 5 - 6 6 - 7 6 - 7 6 - 7 6 - 7 7 - 8 7 - 8 7 - 8 7 - 8 8 - 9 8 - 9 8 - 9 8 - 9 9 - 10 9 - 10 9 - 10 9 - 10 10 - 0 10 - 0 10 - 0 10 - 0 H(ead c D(elete b H(ead d H(ead e 0 - 5 0 - 3 0 - 5 0 - 6 1 * 4 1 * 4 1 * 3 1 * 5 2 a 0 2 a 0 2 a 0 2 a 0 3 b 2 3 - 5 3 d 4 3 d 4 4 c 3 4 c 2 4 c 2 4 c 2 5 - 6 5 - 6 5 - 6 5 e 3 6 - 7 6 - 7 6 - 7 6 - 7 7 - 8 7 - 8 7 - 8 7 - 8 8 - 9 8 - 9 8 - 9 8 - 9 9 - 10 9 - 10 9 - 10 9 - 10 10 - 0 10 - 0 10 - 0 10 - 0
Array Lists: list.c [10]
#define NIL 0
#define FREE_HEADER 0
#define SPACE_SIZE 11
typedef char element_type;
typedef unsigned int node_ptr;
struct node {
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
struct node CURSOR_SPACE[SPACE_SIZE];
position cursor_alloc() {
position p;
p = CURSOR_SPACE[FREE_HEADER].next;
CURSOR_SPACE[FREE_HEADER].next = CURSOR_SPACE[p].next;
return p;
}
void cursor_free(position p) {
CURSOR_SPACE[p].next = CURSOR_SPACE[FREE_HEADER].next;
CURSOR_SPACE[p].element = '-';
CURSOR_SPACE[FREE_HEADER].next = p;
}
void init_list() {
int i;
for (i=0; i<SPACE_SIZE; i++) {
CURSOR_SPACE[i].element = '-';
CURSOR_SPACE[i].next = i+1;
}
CURSOR_SPACE[SPACE_SIZE-1].next = NIL;
}
LIST create_list() {
LIST header;
header = cursor_alloc();
CURSOR_SPACE[header].next = NIL;
return(header);
}
int is_empty(LIST L) { return(CURSOR_SPACE[L].next == NIL); }
int is_last(position p,LIST L) { return(CURSOR_SPACE[p].next == NIL); }
void display_list(LIST L) {
position pos;
pos = CURSOR_SPACE[L].next;
while (pos != NIL) {
printf("%c ",CURSOR_SPACE[pos].element);
pos = CURSOR_SPACE[pos].next;
}
}
position find_previous(element_type x, LIST L) {
position p;
p = L;
while ( (CURSOR_SPACE[p].next != NIL) &&
(CURSOR_SPACE[CURSOR_SPACE[p].next].element != x) )
p = CURSOR_SPACE[p].next;
return p;
}
void delete(element_type x, LIST L) {
position p, tmp_cell;
p = find_previous(x,L);
if (!is_last(p,L)) {
tmp_cell = CURSOR_SPACE[p].next;
CURSOR_SPACE[p].next = CURSOR_SPACE[tmp_cell].next;
cursor_free(tmp_cell);
} }
void insert(element_type x, LIST L, position p) { /* H(ead: insert(x,L,L) */
position tmp_cell;
tmp_cell = cursor_alloc();
if (tmp_cell == 0) printf("Out of space!\n");
else {
CURSOR_SPACE[tmp_cell].element = x;
CURSOR_SPACE[tmp_cell].next = CURSOR_SPACE[p].next;
CURSOR_SPACE[p].next = tmp_cell;
} }
Array Lists: Analysis [13]
Given position p, to insert(x,L,p): 3 steps
This is O(1) [pronounced Big-Oh of 1, Oh of 1, Order 1].
Constants don't matter:
1, 3, 10: O(1)
n, 2n, 1000n+100: O(n)
n*n, 100n*n, 10n*n + 1000n+3: O(n*n)
To insert(x,L,L) at the H(ead of list: O( )
To tailinsert(x,L) at the t(ail of list:
keep a pointer to tail: insert(x,L,tail): O( )
scan from the head to tail: O(n) (number in list)
To D(elete an element using delete(x,L): O( )
To insert by o(rder sorted on ``x'': O( )
Upper bound on performance or rate of growth.