Advertisement
pkbagchi

singly linked list (All_problem)

Nov 25th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.99 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     int data;
  7.     struct node *next;
  8. }node;
  9.  
  10. node *head;
  11. void update_by_value(int,int);
  12. void update(int,int);
  13. int count();
  14. void input(int);
  15. void show();
  16. void inputat(int,int);
  17. void deletep(int);
  18. void deletev(int);
  19. void deleteall();
  20. void main()
  21. {
  22.     head=NULL;
  23.     int x,p;
  24.     printf("Put Your List: ");
  25.     while(1)
  26.     {
  27.         scanf("%d",&x);
  28.         if(x==0) break;
  29.         input(x);
  30.     }
  31.     show();
  32.  
  33.     printf("\nInsert at First: ");
  34.     scanf("%d",&x);
  35.     input(x);
  36.     show();
  37.  
  38.     printf("\nInsert at 2nd Position: ");
  39.     scanf("%d",&x);
  40.     inputat(2,x);
  41.     show();
  42.      printf("\nInsert at Last Position: ");
  43.      scanf("%d",&x);
  44.      inputat(count(),x);
  45.      show();
  46.  
  47.       printf("\nInsert at Any Position: ");
  48.      scanf("%d %d",&p,&x);
  49.      inputat(p,x);
  50.      show();
  51.  
  52.      printf("\nUpdate Based on Position: ");
  53.      scanf("%d %d",&p,&x);
  54.      update(p,x);
  55.      show();
  56.  
  57.        printf("\nUpdate By Value: ");
  58.         scanf("%d %d",&p,&x);
  59.         update_by_value(p,x);
  60.         show();
  61.  
  62.         printf("\nDelete Based On Position: ");
  63.         scanf("%d",&p);
  64.         deletep(p);
  65.         show();
  66.  
  67.         printf("\nDelete By Value: ");
  68.         scanf("%d",&x);
  69.         deletev(x);
  70.         show();
  71.  
  72.         printf("\nDelete All? Press 1: ");
  73.         scanf("%d",&x);
  74.         if(x==1)
  75.         {
  76.             deleteall();
  77.             printf("\nList is Deleted\n");
  78.         }
  79.  
  80.  
  81. }
  82. void deleteall()
  83. {
  84.  
  85.     while(head!=NULL)
  86.     {
  87.         node *temp=head;
  88.         head=head->next;
  89.         temp->next=NULL;
  90.         free(temp);
  91.  
  92.     }
  93. }
  94. void deletev(int x)
  95. {
  96.      node *temp = head,*temp1;
  97.      int i=1;
  98.      int p;
  99.    while(temp!=NULL)
  100.     {
  101.         if(temp->data==x)
  102.         {
  103.             if(i==1)
  104.             {
  105.  
  106.                head=temp->next;
  107.                 temp->next=NULL;
  108.                 free(temp);
  109.                 return;
  110.             }
  111.  
  112.              p=i;
  113.             temp=head;
  114.             for(i=1;i<p-1;i++)
  115.             {
  116.                 temp=temp->next;
  117.             }
  118.             temp1=temp;
  119.             temp1=temp1->next;
  120.             temp->next=temp1->next;
  121.             temp1->next=NULL;
  122.             free(temp1);
  123.             return;
  124.         }
  125.         temp=temp->next;
  126.         i++;
  127.     }
  128. }
  129.  
  130. void deletep(int p)
  131. {
  132.     node *temp=head,*temp1;
  133.     if(p==1)
  134.     {
  135.         head=temp->next;
  136.         temp->next=NULL;
  137.         free(temp);
  138.         return;
  139.     }
  140.  
  141.     int i;
  142.     temp=head;
  143.     for(i=1;i<p-1;i++)
  144.     {
  145.         temp=temp->next;
  146.     }
  147.     temp1=temp;
  148.     temp1=temp1->next;
  149.     temp->next=temp1->next;
  150.     temp1->next=NULL;
  151.     free(temp1);
  152. }
  153. void update_by_value(int p,int x)
  154. {
  155.      node *temp = head;
  156.    while(temp!=NULL)
  157.     {
  158.         if(temp->data==p)
  159.         {
  160.             temp->data=x;
  161.         }
  162.         temp=temp->next;
  163.     }
  164. }
  165. void update(int p,int x)
  166. {
  167.     if(p==1)
  168.     {
  169.         head->data=x;
  170.         return;
  171.     }
  172.     int i;
  173.     node *temp1=head;
  174.     for(i=1;i<p;i++)
  175.     {
  176.         temp1=temp1->next;
  177.     }
  178.     temp1->data=x;
  179. }
  180.  
  181. int count()
  182. {
  183.     node *temp = head;
  184.     int i=1;
  185.     while(temp!=NULL)
  186.     {
  187.         i++;
  188.         temp=temp->next;
  189.     }
  190.  
  191.     return i;
  192. }
  193. void inputat(int p,int v)
  194. {
  195.     node *temp = (node*)malloc(sizeof(node));
  196.     node *temp2,*temp1;
  197.     temp->data=v;
  198.     temp->next=NULL;
  199.     if(p==1)
  200.     {
  201.         temp->next=head;
  202.         head=temp;
  203.         return;
  204.     }
  205.  
  206.     int i;
  207.     temp1=head;
  208.     for(i=1;i<p-1;i++)
  209.     {
  210.         temp1=temp1->next;
  211.     }
  212.     temp->next=temp1->next;
  213.     temp1->next=temp;
  214. }
  215.  
  216. void input(int x)
  217. {
  218.     node *temp = (node*)malloc(sizeof(node));
  219.     temp->data=x;
  220.     temp->next=NULL;
  221.     if(head!=NULL)
  222.         temp->next=head;
  223.     head=temp;
  224. }
  225. void show()
  226. {
  227.     node *temp = head;
  228.     printf("List is: ");
  229.     while(temp!=NULL)
  230.     {
  231.         printf("%d ",temp->data);
  232.         temp=temp->next;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement