| 195 | -------------------------------------------------------------------------------*/ |
| 196 | |
| 197 | node_t* list_AddNodeFirst(list_t* list) |
| 198 | { |
| 199 | if ( !list ) |
| 200 | { |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
| 204 | node_t* node; |
| 205 | |
| 206 | // allocate memory for node |
| 207 | if ( (node = (node_t*) malloc(sizeof(node_t))) == NULL ) |
| 208 | { |
| 209 | printlog( "failed to allocate memory for new node!\n" ); |
| 210 | exit(1); |
| 211 | } |
| 212 | |
| 213 | // initialize data pointers to NULL |
| 214 | node->element = NULL; |
| 215 | node->deconstructor = NULL; |
| 216 | node->size = 0; |
| 217 | node->prev = NULL; |
| 218 | |
| 219 | // integrate it into the list |
| 220 | node->list = list; |
| 221 | if ( list->first != NULL ) |
| 222 | { |
| 223 | // there are prior nodes in the list |
| 224 | node->next = list->first; |
| 225 | list->first->prev = node; |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | // inserting into an empty list |
| 230 | node->next = NULL; |
| 231 | list->last = node; |
| 232 | } |
| 233 | list->first = node; |
| 234 | |
| 235 | return node; |
| 236 | } |
| 237 | |
| 238 | /*------------------------------------------------------------------------------- |
| 239 |
no test coverage detected