#include #include struct Node { int item; struct Node *next; }; void printList(struct Node *p); struct Node *addFirst(struct Node *h, int x); struct Node *removeFirst(struct Node *h); int main() { struct Node *head = NULL; int k; // Insert for (k = 1; k < 10; k++ ) { head = addFirst(head, k); // printList(head); } // Delete printList(head); for (k = 1; k < 10; k++ ) { head = removeFirst(head); printList(head); } } // Remove the first node from the list at h struct Node *removeFirst(struct Node *h) { struct Node *ret; if ( h == NULL ) return h; // Return NULL ret = h->next; // Save return value free(h); // De-allocate deleted node return ret; } // Inserts a new node containg x // at the beginning of the list at h struct Node *addFirst(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; // Store x newNode->next = h; // Make it point to h return newNode; } void printList(struct Node *p) { while ( p != NULL ) { printf("%d -> ", p->item); p = p->next; } printf("[NULL]\n"); }