#include // for NULL #include // for malloc(), free() #include // for assert() #include "ll.h" Node * Push(Node *head, int element) { Node *newnode; // allocate the new node newnode = (Node *) malloc(sizeof(Node)); assert(newnode != NULL); // initialize the new node newnode->element = element; newnode->next = head; return newnode; } int Pop(Node *old_head, Node **new_head) { int tmp; // Corner case: trying to Pop from an empty list. // Hack: return without doing anything (a better design // would have some way to indicate an error) if (old_head == NULL) return 0; // There's something in the list; let's save // what we need to return in a temporary variable. tmp = old_head->element; // Let's return (through the return parameter) // the new head of the list. *new_head = old_head->next; // free old head element free(old_head); // Return the payload we previously stashed in tmp. return tmp; }