Advertisement
vinocastro

Test for BUILDLINKEDLIST

Oct 6th, 2020 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define newline printf("\n")
  4. typedef struct node Node;
  5. struct node{
  6. int data;
  7. Node* next;
  8. };
  9. void print_linked_list(Node **head){
  10. Node *current;
  11. for(current=*head; current != NULL; current = current->next){
  12. printf("%d ", current->data);
  13. }
  14. newline;
  15. }
  16. void build_linked_list(Node **head_ptr)
  17. {
  18.  
  19. int num;
  20. while(1)
  21. {
  22.  
  23. // new_node->data = num;
  24. // new_node->next = NULL;
  25. // *head_ptr = new_node;
  26. scanf("%d",&num);
  27. if(num == -1)
  28. {
  29. break;
  30. }
  31. else
  32. {
  33. Node* new_node = (Node*)malloc(sizeof(Node));
  34. new_node->data = num;
  35. new_node->next = *head_ptr;
  36. *head_ptr = new_node;
  37. }
  38. }
  39. }
  40. int main()
  41. {
  42. Node* head = NULL;
  43. build_linked_list(&head);
  44. print_linked_list(&head);
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement