1 /* A small List class HTList.c
4 ** A list is represented as a sequence of linked nodes of type HTList.
5 ** The first node is a header which contains no object.
6 ** New nodes are inserted between the header and the rest of the list.
15 HTList * HTList_new NOARGS
17 HTList *newList = (HTList *)malloc (sizeof (HTList));
18 if (newList == NULL) outofmem(__FILE__, "HTList_new");
19 newList->object = NULL;
24 void HTList_delete ARGS1(HTList *,this)
27 while (current = this) {
33 void HTList_addObject ARGS2(HTList *,this, void *,newObject)
36 HTList *newNode = (HTList *)malloc (sizeof (HTList));
37 if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
38 newNode->object = newObject;
39 newNode->next = this->next;
43 if (TRACE) printf ("HTList: Trying to add object %p to a nonexisting list\n",
47 BOOL HTList_removeObject ARGS2(HTList *,this, void *,oldObject)
54 if (this->object == oldObject) {
55 previous->next = this->next;
57 return YES; /* Success */
61 return NO; /* object not found or NULL list */
64 void * HTList_removeLastObject ARGS1 (HTList *,this)
66 if (this && this->next) {
67 HTList *lastNode = this->next;
68 void * lastObject = lastNode->object;
69 this->next = lastNode->next;
72 } else /* Empty list */
76 void * HTList_removeFirstObject ARGS1 (HTList *,this)
78 if (this && this->next) {
85 firstObject = this->object;
86 prevNode->next = NULL;
89 } else /* Empty list */
93 int HTList_count ARGS1 (HTList *,this)
97 while (this = this->next)
102 int HTList_indexOf ARGS2(HTList *,this, void *,object)
106 while (this = this->next) {
107 if (this->object == object)
112 return -1; /* Object not in the list */
115 void * HTList_objectAt ARGS2 (HTList *,this, int,position)
120 while (this = this->next) {
126 return NULL; /* Reached the end of the list */