Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. struct node
  2. {
  3. int data;
  4. struct node* next;
  5. };
  6.  
  7. void push(struct node** head_ref, int new_data) // i understand the need of pointer to pointer here, since we are changing the actual value by adding a node..
  8. {
  9. struct node* new_node = (struct node*) malloc(sizeof(struct node));
  10. new_node->data = new_data;
  11. new_node->next = (*head_ref);
  12. (*head_ref) = new_node;
  13. }
  14.  
  15. void insertAfter(struct node* prev_node, int new_data) // why are we not using pointer to pointer here since even here the pointer data is getting modified..??
  16. {
  17. if (prev_node == NULL)
  18. {
  19.  
  20. return;
  21. }
  22.  
  23. struct node* new_node =(struct node*) malloc(sizeof(struct node));
  24. new_node->data = new_data;
  25. new_node->next = prev_node->next;
  26. prev_node->next = new_node;
  27. }
  28.  
  29. int main()
  30. {
  31. struct node* head = NULL;
  32.  
  33. append(&head, 6);
  34. insertAfter(head->next, 8);
  35.  
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement