Advertisement
ruhul0

Link List - creation, display, insertion, deletion

Aug 23rd, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct NODE
  5. {
  6.     int data;
  7.     struct NODE *next;
  8. };
  9.  
  10. typedef struct NODE node;
  11. node *insertion(node *start,int newitem);
  12. node *deletion(node *start,int item);
  13. void display (node *start);
  14. int main()
  15. {
  16.     node *start,*prev,*temp,*temp1;
  17.     int ans,j=0,item,loop;
  18.     start = NULL;
  19.     while(j<5)
  20.     {
  21.         printf("Enter 1 for creation:\nEnter 2 for display:\nEnter 3 for deletion:\nEnter 4 for insertion:\nExit:\n");
  22.         scanf("%d",&j);
  23.         printf("\n");
  24.         if(j==1)
  25.         {
  26.             do
  27.             {
  28.                 printf("press 1 to create and 0 to stop\n");
  29.                 scanf("%d", &ans);
  30.                 if(ans == 1)
  31.                 {
  32.                     if(start == NULL)
  33.                     {
  34.                         start =(node*)malloc(sizeof(node));
  35.                         start->next = NULL;
  36.                         printf("Enter data for node: ");
  37.                         scanf("%d", &start->data);
  38.                         prev = start;
  39.                     }
  40.                     else
  41.                     {
  42.                         temp = (node*)malloc(sizeof(node));
  43.                         temp->next = NULL;
  44.                         prev->next = temp;
  45.                         printf("Enter data for Node: ");
  46.                         scanf("%d", &temp->data);
  47.                         prev=temp;
  48.                     }
  49.                 }
  50.             }
  51.             while(ans == 1);
  52.         }
  53.         if(j==2)
  54.             display(start);
  55.         if(j==3)
  56.         {
  57.             while(1)
  58.             {
  59.                 node *deletion(node *start,int item);
  60.                 while(1)
  61.                     {
  62.                     printf("\n Enter the item to be deleted:=");
  63.                     scanf("%d",&item);
  64.                     start=deletion(start,item);
  65.                     printf("\n");
  66.                     display(start);
  67.                     printf("\n");
  68.                     printf("Do you want to return to previous function: (1=yes 2=no)");
  69.                     scanf("%d",&loop);
  70.                     if(loop==1)
  71.                         break;
  72.                 }
  73.                 if(loop==1)
  74.                     break;
  75.  
  76.             }
  77.  
  78.         }
  79.         if(j==4)
  80.         {
  81.             while(1)
  82.             {
  83.                 printf("\n Enter a item to be inserted:=");
  84.                 scanf("%d",&item);
  85.                 start=insertion(start,item);
  86.                 display(start);
  87.                 printf("\n");
  88.  
  89.                 printf("Do you want to return to previous function: (1=yes 2=no)");
  90.                 scanf("%d",&loop);
  91.                 if(loop==1)
  92.                     break;
  93.             }
  94.         }
  95.     }
  96.     return 0;
  97. }
  98.  
  99. node *deletion(node *start, int item)
  100. {
  101.     node *temp1,*temp;
  102.     if(start==NULL)
  103.     {
  104.         printf("\nNo linked list!");
  105.     }
  106.     else if (start->data==item)
  107.     {
  108.         temp1=start;
  109.         start=start->next;
  110.         free(temp1);
  111.     }
  112.     else
  113.     {
  114.         temp=start;
  115.         while((temp->next!=NULL)&&(temp->next->data!=item))
  116.         {
  117.             temp=temp->next;
  118.         }
  119.         if(temp->next==NULL)
  120.         {
  121.             printf("\n Not Found");
  122.         }
  123.         else
  124.         {
  125.             temp1=temp->next;
  126.             temp->next=temp1->next;
  127.             free(temp1);
  128.         }
  129.     }
  130.     return start;
  131. }
  132. node *insertion(node *start, int newitem)
  133. {
  134.     node *temp,*temp1;
  135.     if((start==NULL)||(start->data>=newitem))
  136.     {
  137.         temp1=(node*)malloc(sizeof(node));
  138.         temp1->data=newitem;
  139.         temp1->next=start;
  140.         start=temp1;
  141.     }
  142.     else
  143.     {
  144.         temp=start;
  145.         while((temp->next!=NULL)&&(temp->next->data<=newitem))
  146.         {
  147.             temp=temp->next;
  148.         }
  149.         temp1=(node*)malloc(sizeof(node));
  150.         temp1->data=newitem;
  151.         temp1->next=temp->next;
  152.         temp->next=temp1;
  153.     }
  154.     return start;
  155. }
  156.  
  157. void display(node *head)
  158. {
  159.     node *temp;
  160.     printf("\n Created list:=\n");
  161.     temp=head;
  162.     while(temp!=NULL)
  163.     {
  164.         printf("  %d",temp->data);
  165.         temp=temp->next;
  166.     }
  167.     printf("\n");
  168.     return;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement