Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Given a reference (pointer to pointer) to the head
- // of a list and an int, push a new node on the front of the list.
- // Creates a new node with the int, links the list off the .next of the
- // new node, and finally changes the head to point to the new node.
- void Push(node** headRef, int newData) {
- node* newNode = (node*) malloc(sizeof(node)); // allocate node
- newNode->data = newData; // put in the data
- newNode->next = (*headRef); // link the old list off the new node
- (*headRef) = newNode; // move the head to point to the new node
- }
- Test Driver:
- struct lNode {
- int data;
- lNode *next;
- }
- typedef struct lNode node;
- // Build and return the list {1, 2, 3}
- node* buildList() {
- node* head = NULL; // Start with the empty list
- Push(&head, 3); // Use Push() to add all the data
- Push(&head, 2);
- Push(&head, 1);
- return(head);
- }
Add Comment
Please, Sign In to add comment