|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- #include <stdio.h>
- #include "utlist.h"
-
- typedef struct el {
- int id;
- struct el *next, *prev;
- } el;
-
- int main()
- {
- int i;
- int count;
- el els[10], *e;
- el *head = NULL;
- el *zeroptr = NULL;
- for(i=0; i<10; i++) {
- els[i].id=(int)'a'+i;
- }
-
- /* test CDL macros */
- printf("CDL appends\n");
- CDL_APPEND(head,&els[0]);
- CDL_APPEND(head,&els[1]);
- CDL_APPEND(head,&els[2]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- CDL_COUNT(head,e, count);
- printf("count = %d\n", count);
-
- printf("Test CDL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
- CDL_PREPEND_ELEM(head, zeroptr, &els[3]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test CDL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
- CDL_PREPEND_ELEM(head, &els[1], &els[4]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test CDL_APPEND_ELEM %c with elt NULL\n", els[5].id);
- CDL_APPEND_ELEM(head, zeroptr, &els[5]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test CDL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
- CDL_APPEND_ELEM(head, &els[1], &els[6]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- CDL_COUNT(head,e, count);
- printf("count = %d\n", count);
-
- /* point head to head->next */
- printf("advancing head pointer\n");
- head = head->next;
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- /* follow circular loop a few times */
- for(i=0,e=head; e && i<20; i++,e=e->next) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- /* follow circular loop backwards a few times */
- for(i=0,e=head; e && i<10; i++,e=e->prev) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("deleting (b)\n");
- CDL_DELETE(head,&els[1]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (a)\n");
- CDL_DELETE(head,&els[0]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (c)\n");
- CDL_DELETE(head,&els[2]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (g)\n");
- CDL_DELETE(head,&els[6]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (e)\n");
- CDL_DELETE(head,&els[4]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (d)\n");
- CDL_DELETE(head,&els[3]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("deleting (f)\n");
- CDL_DELETE(head,&els[5]);
- CDL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- /* test DL macros */
- printf("DL appends\n");
-
- DL_APPEND(head,&els[0]);
- DL_APPEND(head,&els[1]);
- DL_APPEND(head,&els[2]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- DL_COUNT(head,e, count);
- printf("count = %d\n", count);
-
- printf("Test DL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
- DL_PREPEND_ELEM(head, zeroptr, &els[3]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test DL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
- DL_PREPEND_ELEM(head, &els[1], &els[4]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test DL_APPEND_ELEM %c with elt NULL\n", els[5].id);
- DL_APPEND_ELEM(head, zeroptr, &els[5]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test DL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
- DL_APPEND_ELEM(head, &els[1], &els[6]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- DL_COUNT(head,e, count);
- printf("count = %d\n", count);
-
- printf("deleting (b)\n");
- DL_DELETE(head,&els[1]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (a)\n");
- DL_DELETE(head,&els[0]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (c)\n");
- DL_DELETE(head,&els[2]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (g)\n");
- DL_DELETE(head,&els[6]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (e)\n");
- DL_DELETE(head,&els[4]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (d)\n");
- DL_DELETE(head,&els[3]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("deleting (f)\n");
- DL_DELETE(head,&els[5]);
- DL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
-
- /* test LL macros */
- printf("LL appends\n");
-
- LL_APPEND(head,&els[0]);
- LL_APPEND(head,&els[1]);
- LL_APPEND(head,&els[2]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- LL_COUNT(head,e, count);
- printf("count = %d\n", count);
-
- printf("Test LL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
- LL_PREPEND_ELEM(head, zeroptr, &els[3]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test LL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
- LL_PREPEND_ELEM(head, &els[1], &els[4]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test LL_APPEND_ELEM %c with elt NULL\n", els[5].id);
- LL_APPEND_ELEM(head, zeroptr, &els[5]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- printf("Test LL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
- LL_APPEND_ELEM(head, &els[1], &els[6]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- LL_COUNT(head,e, count);
- printf("count = %d\n", count);
-
- printf("deleting (b)\n");
- LL_DELETE(head,&els[1]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (a)\n");
- LL_DELETE(head,&els[0]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (c)\n");
- LL_DELETE(head,&els[2]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (g)\n");
- LL_DELETE(head,&els[6]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (e)\n");
- LL_DELETE(head,&els[4]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
- printf("deleting (d)\n");
- LL_DELETE(head,&els[3]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("deleting (f)\n");
- LL_DELETE(head,&els[5]);
- LL_FOREACH(head,e) {
- printf("%c ", e->id);
- }
- printf("\n");
-
- return 0;
- }
|