Adds an item to a list Returns 1 if everything is ok, else 0
| 41 | // Adds an item to a list |
| 42 | // Returns 1 if everything is ok, else 0 |
| 43 | int AddListItem(listnode **listp, void *item) { |
| 44 | listnode *newnode, *curr; |
| 45 | |
| 46 | newnode = NewListNode(); |
| 47 | |
| 48 | if (newnode == NULL) { |
| 49 | mprintf(0, "There was a problem mallocing list node memory!\n"); |
| 50 | Int3(); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | if (*listp == NULL) // If this list is empty, construct a new head |
| 55 | { |
| 56 | newnode->data = item; |
| 57 | *listp = newnode; |
| 58 | return 1; |
| 59 | } else { |
| 60 | // Go through until the end of the list and add the new item there |
| 61 | for (curr = *listp; curr->next != NULL; curr = curr->next) { |
| 62 | if (curr->data == item) { |
| 63 | Int3(); |
| 64 | return 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | newnode->data = item; |
| 69 | curr->next = newnode; |
| 70 | newnode->next = NULL; |
| 71 | newnode->prev = curr; |
| 72 | } |
| 73 | |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | // Removes an item from a list |
| 78 | int RemoveListItem(listnode **listp, void *item) { |
no test coverage detected