Advertisement
Guest User

linked list create,insert&visualize

a guest
Oct 14th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5.     int data;
  6.     node* next;
  7. }*head;
  8. void create_node(int n);
  9. void display_node();
  10. int main()
  11. {
  12.     int n;
  13.     printf("Please enter an positive integer for the number of node\n");
  14.     scanf("%d",&n);
  15.     create_node(n);
  16.     display_node();
  17.     return 0;
  18. }
  19. void create_node(int n)
  20. {
  21.     int value,i;
  22.     struct node *current,*temp;
  23.     head=(struct node*)(malloc(sizeof(node)));
  24.     printf("Please enter value for node : 1\n");
  25.     scanf("%d",&value);
  26.     head->data=value;
  27.     head->next= nullptr;
  28.     temp=head;
  29.     for(i=2;i<=n;i++)
  30.     {
  31.         current=(struct node*)(malloc(sizeof(node)));
  32.         printf("Please enter value for node : %d\n",i);
  33.         scanf("%d",&value);
  34.         current->data=value;
  35.         current->next= nullptr;
  36.         temp->next=current;
  37.         temp=temp->next;
  38.     }
  39. }
  40. void display_node()
  41. {
  42.     struct node *temp;
  43.     temp=head;
  44.     while (temp!= nullptr)
  45.     {
  46.         printf("%d\t",temp->data);
  47.         temp=temp->next;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement