Advertisement
Guest User

ahsanthefather

a guest
Oct 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. //shitcodeXD
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. struct node{
  5.     int number;
  6.     struct node *next;
  7. };
  8. typedef struct node nd;
  9.     nd *head=NULL,*last=NULL;
  10. create()
  11. {
  12.     while(1)
  13.     {
  14.         int value;
  15.         printf("Enter your number(press -1 to exit)\n");
  16.         scanf("%d",&value);
  17.         if(value==-1)
  18.         {
  19.             main();
  20.         }
  21.         last_insart(value);
  22.     }
  23. }
  24. void insert_at_first()
  25. {
  26.     int value;
  27.     printf("\nEnter the value u want to add in list:");
  28.     scanf("%d",&value);
  29.     nd *temp_nd=(nd*)malloc(sizeof(nd));
  30.     temp_nd->number=value;
  31.     temp_nd->next=head;
  32.     head=temp_nd;
  33.  
  34.     print();
  35.     main();
  36. }
  37. last_insart(int value)
  38. {
  39.     nd *temp_data;
  40.     temp_data=(nd*)malloc(sizeof(nd));
  41.     temp_data->number=value;
  42.     temp_data->next=NULL;
  43.     if(head==NULL)
  44.     {
  45.         head=temp_data;
  46.         last=temp_data;
  47.     }
  48.     else
  49.     {
  50.         last->next=temp_data;
  51.         last=temp_data;
  52.     }
  53. }
  54. void insert_at_after()
  55. {
  56.     int key,value;
  57.     printf("\nEnter a KEY(existing item of list),after that you want to insert a value");
  58.     scanf("%d",&key);
  59.     printf("\nInsert new item after %d KEY",key);
  60.     scanf("%d",&value);
  61.     nd*mynd;
  62.     mynd=head;
  63.     int flag=0;
  64.     while(mynd!=NULL)
  65.     {
  66.         if(mynd->number==key)
  67.         {
  68.             nd *newnd=(nd*)malloc(sizeof(nd));
  69.             newnd->number=value;
  70.             newnd->next=mynd->next;
  71.             mynd->next=newnd;
  72.             printf("%d is inserted after%d\n",value,key);
  73.             flag=1;
  74.             break;
  75.         }
  76.           mynd=mynd->next;
  77.     }
  78.     if(flag==0)
  79.         printf("Key not found\n");
  80.     print();
  81.     main();
  82.  
  83. }
  84. last_at_insert()
  85. {
  86.     int x;
  87.     printf("\n Enter the value u want to add in list:");
  88.     scanf("%d",&x);
  89.     last_insart(x);
  90.     print();
  91.     main();
  92. }
  93. void search()
  94. {
  95.     int value;
  96.    printf("\nInsert Search item :");
  97.     scanf("%d",&value);
  98.     nd*mynd;
  99.     mynd=head;
  100.     int flag=0;
  101.     while(mynd!=NULL)
  102.     {
  103.         if(mynd->number==value)
  104.         {
  105.             printf("%d is founded",value);
  106.             flag=1;
  107.             break;
  108.         }
  109.     mynd=mynd->next;
  110.  
  111.     }
  112.     if(flag==0)
  113.         printf("Key not found\n");
  114.     main();
  115.  
  116. }
  117. void delete()
  118. {
  119.         int value;
  120.    printf("\nInsert Search item to delete:");
  121.     scanf("%d",&value);
  122.     nd*mynd=head;
  123.     nd *previous=NULL;
  124.     mynd=head;
  125.     int flag=0;
  126.     while(mynd!=NULL)
  127.     {
  128.         if(mynd->number==value)
  129.         {
  130.             if(previous==NULL){
  131.  
  132.                 head=mynd->next;
  133.             }
  134. else
  135. {
  136.     previous->next = mynd->next;
  137.             printf("%d is deleted\n",value);
  138.  
  139.             flag=1;
  140.             break;
  141.         }}
  142.         previous=mynd;
  143.         mynd=mynd->next;
  144.  
  145.     }
  146.     if(flag==0)
  147.         printf("Key not found\n");
  148.     main();
  149.  
  150.  
  151. }
  152. print()
  153. {
  154.     nd *mylist;
  155.     mylist=head;
  156.     while(mylist!=NULL)
  157.     {
  158.         printf("%d ",mylist->number);
  159.         mylist=mylist->next;
  160.     }
  161.     main();
  162. }
  163. int main()
  164. {
  165.     int a;
  166.     printf("\n\nWelcome to linked list........\n");
  167.  
  168.     printf("press 0 for Exit\n");
  169.     printf("Press 1 for Create\n");
  170.     printf("Press 2 for print\n");
  171.     printf("Press 3 for first insert\n");
  172.     printf("press 4 for insert after\n");
  173.     printf("press 5 for search\n");
  174.     printf("Press 6 for last insert\n");
  175.     printf("Press 7 for delete\n");
  176.     int n;
  177.     scanf("%d",&n);
  178.     switch(n)
  179.     {
  180.     case 0:
  181.         //exit();
  182.         break;
  183.     case 1:
  184.         create();
  185.         break;
  186.         case 2:
  187.         print();
  188.         break;
  189.         case 3:
  190.             insert_at_first();
  191.             break;
  192.         case 4:
  193.             insert_at_after();
  194.             break;
  195.         case 5:
  196.             search();
  197.             break;
  198.         case 6:
  199.             last_at_insert();
  200.             break;
  201.         case 7:
  202.             delete();
  203.             break;
  204.  
  205.     default:
  206.         printf("invalid option\n");
  207.     }
  208.     return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement