3 Most Elemental List ADT

The most elemental (basic) ADT of a list is as follows:

In C code, the list of $(a, b, c)$ may be constructed as follows:

struct List *pL;
pL = List_new(void);
List_setfirst(pL,'a');
List_setfirst(List_getrest(pL), 'b');
List_setfirst(List_getrest(List_getrest(pL)),'c');

Elements in a list can be listed as follows:

struct List *pL;
// ...
struct List *tmp;
tmp = pL;
while (!List_isempty(tmp))
{
  printf("%c\n",List_getfirst(tmp));
  tmp = List_getrest(tmp);
}

We can even change a list from $(a, b, c)$ to $(a, b, x, c)$ by inserting an value $x$ between the existing values $b$ and $c$ :

struct List *pL;
// ... get pL to point to a list (a, b, c)
struct List *tmpB, *tmpR, *newone;
tmpB = List_getrest(pL); // (b, c)
tmpR = List_getrest(tmpB); // (c)
newone = List_new(); // ()
List_setfirst(newone, 'x'); // (x)
List_setrest(newone, tmpR); // (x, c)
List_setrest(tmpB, newone); // (b, x, c)
// pL is now (a, b, x, c)

Copyright © 2006-09-27 by Tak Auyeung