| 1 | #ifndef LL_H |
|---|
| 2 | #define LL_H |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | /*********************************************************************** |
|---|
| 7 | Linked Lists! (Doubly-Linked Lists) |
|---|
| 8 | ******************************************************************* |
|---|
| 9 | |
|---|
| 10 | To create a list, do the following: |
|---|
| 11 | |
|---|
| 12 | LL *list; |
|---|
| 13 | list = LL_new(); |
|---|
| 14 | if(!list) handle_an_error(); |
|---|
| 15 | |
|---|
| 16 | The list can hold any type of data. You will need to typecast your |
|---|
| 17 | datatype to a "void *", though. So, to add something to the list, |
|---|
| 18 | the following would be a good way to start: |
|---|
| 19 | |
|---|
| 20 | typedef struct my_data { |
|---|
| 21 | char string[16]; |
|---|
| 22 | int number; |
|---|
| 23 | } my_data; |
|---|
| 24 | |
|---|
| 25 | my_data *thingie; |
|---|
| 26 | |
|---|
| 27 | for(something to something else) |
|---|
| 28 | { |
|---|
| 29 | thingie = malloc(sizeof(my_data)); |
|---|
| 30 | LL_AddNode(list, (void *)thingie); // typecast it to a "void *" |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | For errors, the general convention is that "0" means success, and |
|---|
| 34 | a negative number means failure. Check LL.c to be sure, though. |
|---|
| 35 | |
|---|
| 36 | ******************************************************************* |
|---|
| 37 | |
|---|
| 38 | To change the data, try this: |
|---|
| 39 | |
|---|
| 40 | thingie = (my_data *)LL_Get(list); // typecast it back to "my_data" |
|---|
| 41 | thingie->number = another_number; |
|---|
| 42 | |
|---|
| 43 | You don't need to "Put" the data back, but it doesn't hurt anything. |
|---|
| 44 | |
|---|
| 45 | LL_Put(list, (void *)thingie); |
|---|
| 46 | |
|---|
| 47 | However, if you want to point the node's data somewhere else, you'll |
|---|
| 48 | need to get the current data first, keep track of it, then set the data |
|---|
| 49 | to a new location: |
|---|
| 50 | |
|---|
| 51 | my_data * old_thingie, new_thingie; |
|---|
| 52 | |
|---|
| 53 | old_thingie = (my_data *)LL_Get(list); |
|---|
| 54 | LL_Put(list, (void *)new_thingie); |
|---|
| 55 | |
|---|
| 56 | // Now, do something with old_thingie. (maybe, free it?) |
|---|
| 57 | |
|---|
| 58 | Or, you could just delete the node entirely and then add a new one: |
|---|
| 59 | |
|---|
| 60 | my_data * thingie; |
|---|
| 61 | |
|---|
| 62 | thingie = (my_data *)LL_DeleteNode(list); |
|---|
| 63 | free(thingie); |
|---|
| 64 | |
|---|
| 65 | thingie->number = 666; |
|---|
| 66 | |
|---|
| 67 | LL_InsertNode(list, (void *)thingie); |
|---|
| 68 | |
|---|
| 69 | ******************************************************************* |
|---|
| 70 | |
|---|
| 71 | To operate on each list item, try this: |
|---|
| 72 | |
|---|
| 73 | LL_Rewind(list); |
|---|
| 74 | do { |
|---|
| 75 | my_data = (my_data *)LL_Get(list); |
|---|
| 76 | ... do something to it ... |
|---|
| 77 | } while(LL_Next(list) == 0); |
|---|
| 78 | |
|---|
| 79 | ******************************************************************* |
|---|
| 80 | |
|---|
| 81 | You can also treat the list like a stack, or a queue. Just use the |
|---|
| 82 | following functions: |
|---|
| 83 | |
|---|
| 84 | LL_Push() // Regular stack stuff: add, remove, peek, rotate |
|---|
| 85 | LL_Pop() |
|---|
| 86 | LL_Top() |
|---|
| 87 | LL_Roll() |
|---|
| 88 | |
|---|
| 89 | LL_Shift() // Other end of the stack (like in perl) |
|---|
| 90 | LL_Unshift() |
|---|
| 91 | LL_Look() |
|---|
| 92 | LL_UnRoll() |
|---|
| 93 | |
|---|
| 94 | LL_Enqueue() // Standard queue operations |
|---|
| 95 | LL_Dequeue() |
|---|
| 96 | |
|---|
| 97 | There are also other goodies, like sorting and searching. |
|---|
| 98 | |
|---|
| 99 | ******************************************************************* |
|---|
| 100 | |
|---|
| 101 | Array-like operations will come later, to allow numerical indexing: |
|---|
| 102 | |
|---|
| 103 | LL_nGet(list, 3); |
|---|
| 104 | LL_nSwap(list, 6, 13); |
|---|
| 105 | LL_nPut(list, -4, data); // Puts item at 4th place from the end.. |
|---|
| 106 | |
|---|
| 107 | More ideas for later: |
|---|
| 108 | |
|---|
| 109 | LL_MoveNode(list, amount); // Slides a node to another spot in the list |
|---|
| 110 | -- LL_MoveNode(list, -1); // moves a node back one toward the head |
|---|
| 111 | |
|---|
| 112 | ... um, more? |
|---|
| 113 | |
|---|
| 114 | ******************************************************************* |
|---|
| 115 | That's about it, for now... Be sure to free the list when you're done! |
|---|
| 116 | ***********************************************************************/ |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | // See LL.c for more detailed descriptions of these functions. |
|---|
| 120 | |
|---|
| 121 | typedef struct LL_node |
|---|
| 122 | { |
|---|
| 123 | struct LL_node *next, *prev; |
|---|
| 124 | void *data; |
|---|
| 125 | } LL_node; |
|---|
| 126 | |
|---|
| 127 | typedef struct LL |
|---|
| 128 | { |
|---|
| 129 | LL_node head, tail; |
|---|
| 130 | LL_node *current; |
|---|
| 131 | } LL; |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | // Creates a new list... |
|---|
| 136 | LL * LL_new(); |
|---|
| 137 | // Destroying lists... |
|---|
| 138 | int LL_Destroy(LL *list); |
|---|
| 139 | int LL_node_Destroy(LL_node *node); |
|---|
| 140 | int LL_node_Unlink(LL_node *node); |
|---|
| 141 | int LL_node_DestroyData(LL_node *node); |
|---|
| 142 | |
|---|
| 143 | // Returns to the beginning of the list... |
|---|
| 144 | int LL_Rewind(LL *list); |
|---|
| 145 | // Goes to the end of the list... |
|---|
| 146 | int LL_End(LL *list); |
|---|
| 147 | // Go to the next node |
|---|
| 148 | int LL_Next(LL *list); |
|---|
| 149 | // Go to the previous node |
|---|
| 150 | int LL_Prev(LL *list); |
|---|
| 151 | |
|---|
| 152 | // Data manipulation |
|---|
| 153 | void * LL_Get(LL *list); |
|---|
| 154 | int LL_Put(LL *list, void *data); |
|---|
| 155 | // Don't use these next two unless you really know what you're doing. |
|---|
| 156 | LL_node * LL_GetNode(LL *list); |
|---|
| 157 | int LL_PutNode(LL *list, LL_node *node); |
|---|
| 158 | |
|---|
| 159 | void * LL_GetFirst(LL *list); // gets data from first node |
|---|
| 160 | void * LL_GetNext (LL *list); // ... next node |
|---|
| 161 | void * LL_GetPrev (LL *list); // ... prev node |
|---|
| 162 | void * LL_GetLast (LL *list); // ... last node |
|---|
| 163 | |
|---|
| 164 | int LL_AddNode(LL *list, void * add); // Adds node AFTER current one |
|---|
| 165 | int LL_InsertNode(LL *list, void * add);// Adds node BEFORE current one |
|---|
| 166 | // Removes a node from the link; returns the data from the node |
|---|
| 167 | void * LL_DeleteNode(LL *list); |
|---|
| 168 | // Removes a specific node... |
|---|
| 169 | void * LL_Remove(LL *list, void * data); |
|---|
| 170 | |
|---|
| 171 | // Stack operations |
|---|
| 172 | int LL_Push(LL *list, void *add); // Add node to end of list |
|---|
| 173 | void * LL_Pop(LL *list); // Remove node from end of list |
|---|
| 174 | void * LL_Top(LL *list); // Peek at end node |
|---|
| 175 | void * LL_Shift(LL *list); // Remove node from start of list |
|---|
| 176 | void * LL_Look(LL *list); // Peek at first node |
|---|
| 177 | int LL_Unshift(LL *list, void *add); // Add node to beginning of list |
|---|
| 178 | |
|---|
| 179 | int LL_Roll(LL *list); // Make first node last |
|---|
| 180 | int LL_UnRoll(LL *list);// Roll the other way... |
|---|
| 181 | |
|---|
| 182 | // Queue operations... |
|---|
| 183 | //int LL_Enqueue(LL *list, void *add); |
|---|
| 184 | //void * LL_Dequeue(LL *list); |
|---|
| 185 | ////////////////////////////////////////////////////////////////////// |
|---|
| 186 | // Queue operations... |
|---|
| 187 | #define LL_Enqueue(list,add) LL_Push(list,add) |
|---|
| 188 | |
|---|
| 189 | #define LL_Dequeue(list) LL_Shift(list) |
|---|
| 190 | |
|---|
| 191 | int LL_PriorityEnqueue(LL *list, void *add, int compare(void *, void *)); |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | int LL_SwapNodes(LL_node *one, LL_node *two); // Switch two nodes positions... |
|---|
| 195 | int LL_nSwapNodes(int one, int two); // Switch two nodes positions... |
|---|
| 196 | |
|---|
| 197 | int LL_Length(LL *list); // Returns # of nodes in entire list |
|---|
| 198 | |
|---|
| 199 | // Searching... |
|---|
| 200 | void * LL_Find(LL *list, int compare(void *, void *), void *value); |
|---|
| 201 | |
|---|
| 202 | // Sorts the list... |
|---|
| 203 | int LL_Sort(LL *list, int compare(void *, void *)); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | // Debugging... |
|---|
| 207 | void LL_dprint(LL *list); |
|---|
| 208 | |
|---|
| 209 | #endif |
|---|