Advertisement
zaman360live

Need Help

Oct 22nd, 2021
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5.     int data;
  6.     struct node *link;
  7. } * head;
  8.  
  9. void create(int n);
  10. void add(int data);
  11. void display();
  12. int main()
  13. {
  14.     struct node *head;
  15.     int n, data;
  16.     printf("HOW MANY NODES DO YOU WANT : ");
  17.     scanf("%d", &n);
  18.     create(n);
  19.     printf("Data in the list : \n");
  20.     display();
  21.  
  22.     printf("\nenter the data you want to inseart in the end: ");
  23.     scanf("%d", &data);
  24.     add(data);
  25.     printf("THE FINAL LIST ");
  26.     display();
  27.     return 0;
  28. }
  29. void create(int n)
  30. {
  31.     head = (struct node *)malloc(sizeof(struct node));
  32.     struct node *temp = (struct node *)malloc(sizeof(struct node));
  33.     struct node *newnode = (struct node *)malloc(sizeof(struct node));
  34.     int i, data;
  35.  
  36.     if (head == NULL)
  37.     {
  38.         printf("memory problem");
  39.     }
  40.     else
  41.     {
  42.         printf("1 NO data : ");
  43.         scanf("%d", &data);
  44.         head->link = NULL;
  45.         head->data = data;
  46.         temp = head;
  47.         if (newnode == NULL)
  48.         {
  49.             printf("memory problem:");
  50.         }
  51.         else
  52.         {
  53.             for (i = 2; i <= n; i++)
  54.             {
  55.                 printf("%d no-- data : ", i);
  56.                 scanf("%d", &data);
  57.                 newnode->data = data;
  58.                 newnode->link = NULL;
  59.  
  60.                 temp->link = newnode;
  61.                 temp = temp->link;
  62.             }
  63.             //printf("data entry successful");
  64.         }
  65.     }
  66. }
  67. void add(int data)
  68. {
  69.     struct node *newnode = (struct node *)malloc(sizeof(struct node));
  70.     struct node *temp;
  71.     newnode->data = data;
  72.     newnode->link = NULL;
  73.     temp = head;
  74.  
  75.     while (temp->link != NULL)
  76.     {
  77.         //printf("%d no data is : %d",i,temp->data);
  78.         temp = temp->link;
  79.     }
  80.     temp->link = newnode;
  81.     printf("DATA ENTWER SUCCECC\n");
  82. }
  83. void display()
  84. {
  85.  
  86.     struct node *temp;
  87.     if (head == NULL)
  88.     {
  89.         printf("display error\n");
  90.     }
  91.     else
  92.     {
  93.         int count = 1;
  94.         temp = head;
  95.         while (temp!= NULL)
  96.         {
  97.             printf("%d no data = %d \n", count, temp->data);
  98.             temp = temp->link;
  99.             count++;
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement