In C code, the list of
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
to
by inserting
an value
between the existing values
and
:
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