Advertisement
Mukit1234

Untitled

Oct 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node
  4. {
  5.     int data;
  6.     struct Node *next;
  7.  
  8. }*head;
  9.  
  10. void createlinklist(int n);
  11. void display();
  12. void insertfirst(int data);
  13. void insertend(int data1);
  14. int main()
  15. {
  16.     int n,data,data1;
  17.     printf("Enter the number of nodes: ");
  18.     scanf("%d",&n);
  19.     createlinklist(n);
  20.     printf("\nData entered in the list are:\n");
  21.     display();
  22.     printf("Enter data to insert at the first of the list: ");
  23.     scanf("%d",&data);
  24.     insertfirst(data);
  25.     display(data);
  26.     printf("Enter data to insert at the end of the list: ");
  27.     scanf("%d",&data1);
  28.     insertend(data1);
  29.     display(data1);
  30.     return 0;
  31. }
  32. void createlinklist(int n)
  33. {
  34.     struct Node *p, *temp;
  35.     int data, i;
  36.     head= (struct Node*)malloc(sizeof(struct Node));
  37.     printf("Input data for node 1: ");
  38.     scanf("%d",&data);
  39.     head->data=data;
  40.     head->next=NULL;
  41.     p=head;
  42.     temp=head;
  43.     for(i=2; i<=n; i++)
  44.     {
  45.         printf("Enter data for node %d: ",i);
  46.         scanf("%d",&data);
  47.         temp=(struct Node*)malloc(sizeof(struct Node));
  48.         temp->data=data;
  49.         temp->next=NULL;
  50.         p->next=temp;
  51.         p=p->next;
  52.  
  53.     }
  54. }
  55. void display()
  56. {
  57.     struct Node *temp;
  58.     temp=head;
  59.     while(temp!=NULL)
  60.     {
  61.         printf("%d\n",temp->data);
  62.         temp=temp->next;
  63.     }
  64. }
  65. void insertfirst(int data)
  66. {
  67.     struct Node *newnode;
  68.     newnode=(struct Node*)malloc(sizeof(struct Node));
  69.     newnode->data=data;
  70.     newnode->next=head;
  71.     head=newnode;
  72. }
  73. void insertend(int data1)
  74. {
  75.     struct Node *newnode,*temp;
  76.     newnode=(struct Node*)malloc(sizeof(struct Node));
  77.     newnode->data=data1;
  78.     newnode->next=NULL;
  79.     temp=head;
  80.     while(temp->next!=NULL)
  81.  
  82.         temp=temp->next;
  83.         temp->next=newnode;
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement