/* CSE 333 Su12 Lecture 6 demo: push_list.c */ /* Gribble/Perkins */ /* Dynamic linked list demo with integer list data */ #include #include #include typedef struct Node { // Linked list node int element; // Data in this node struct Node *next; // Ptr to next node or NULL if none } Node; // Given a pointer head to a linked list node and an integer value e, // create a node refering to e with head as the next node and // return a pointer to the newly allocated node. // Exit the program with failure if unable to allocate a new node. Node *Push(Node *head, int e) { Node *n = (Node *) malloc(sizeof(Node)); if (n == NULL) exit(EXIT_FAILURE); n->element = e; n->next = head; return n; } // Create a new linked list holding two numbers int main(int argc, char **argv) { Node *list = NULL; list = Push(list, 1); list = Push(list, 2); // omitted: production code should free allocated heap data return 0; }