Guest User

Untitled

a guest
Aug 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. // Given a reference (pointer to pointer) to the head
  2. // of a list and an int, push a new node on the front of the list.
  3. // Creates a new node with the int, links the list off the .next of the
  4. // new node, and finally changes the head to point to the new node.
  5.  
  6. void Push(node** headRef, int newData) {
  7. node* newNode = (node*) malloc(sizeof(node)); // allocate node
  8. newNode->data = newData; // put in the data
  9. newNode->next = (*headRef); // link the old list off the new node
  10. (*headRef) = newNode; // move the head to point to the new node
  11. }
  12.  
  13.  
  14. Test Driver:
  15. struct lNode {
  16. int data;
  17. lNode *next;
  18. }
  19.  
  20. typedef struct lNode node;
  21.  
  22. // Build and return the list {1, 2, 3}
  23. node* buildList() {
  24. node* head = NULL; // Start with the empty list
  25. Push(&head, 3); // Use Push() to add all the data
  26. Push(&head, 2);
  27. Push(&head, 1);
  28. return(head);
  29. }
Add Comment
Please, Sign In to add comment