#include #include /* ------------------------ List element definition ------------------------ */ struct ListElem { int value; struct ListElem* next; }; void printList( struct ListElem* h ) { while ( h != NULL ) { printf(" %d\n", h->value ); // Print h = h->next; // Go to next element in list } printf("\n"); } struct ListElem* insertHead(struct ListElem* head, struct ListElem* elem) { elem->next = head; // make "elem" point to "head" return(elem); // return address of new head } struct ListElem* deleteHead ( struct ListElem* h ) { struct ListElem* deleted; struct ListElem* newHead; if ( h == NULL ) return(NULL); // Nothing to delete.... deleted = h; // Save the location of the deleted element newHead = h->next; // We will return this value free(deleted); // Un-reserve memory cells return( newHead ); // Make the second element the new head } int main(int argc, char *argv[]) { struct ListElem* head; // Head of list struct ListElem* p; // Help variable to allocate new list element head = NULL; // Empty list /* ======================================================== Insert 123 ======================================================== */ p = malloc( sizeof(struct ListElem) ); p->value = 123; head = insertHead( head, p); /* ======================================================== Insert 444 ======================================================== */ p = malloc( sizeof(struct ListElem) ); p->value = 444; head = insertHead( head, p); /* ======================================================== Insert 789 ======================================================== */ p = malloc( sizeof(struct ListElem) ); p->value = 789; head = insertHead( head, p); printList(head); head = deleteHead( head ); printList(head); }