Mim527

Mim's doubly link list

Oct 13th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.11 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     int a;
  7.     struct node*next,*prev;
  8. } node;
  9.  
  10.  
  11. node*head=NULL,*tail=NULL;
  12.  
  13. int main()
  14. {
  15.     int choice;
  16.     while(1)
  17.     {
  18.         printf("\t\t\t\t\n\nDoubly linked list\n\n");
  19.  
  20.  
  21.         printf("\t\t\t\t\n1. Add First\n");
  22.         printf("\t\t\t\t\n2. Add Any\n");
  23.         printf("\t\t\t\t\n3. Delete Elements\n");
  24.         printf("\t\t\t\t\n4. Display\n");
  25.         printf("\t\t\t\t\n5. Delete Position\n");
  26.         printf("\t\t\t\t\n6. Search Data\n");
  27.         printf("\t\t\t\t\n7. Add end\n\n");
  28.  
  29.         printf("Enter your choice:");
  30.  
  31.         scanf("%d",&choice);
  32.  
  33.         switch(choice)
  34.         {
  35.         case 1 :
  36.             Add_first();
  37.             break;
  38.         case 2:
  39.             Add_any();
  40.             break;
  41.         case 3 :
  42.             delete_data();
  43.             break;
  44.         case 4:
  45.             display_first();
  46.             break;
  47.         case 5:
  48.             Delete_Position();
  49.             break;
  50.         case 6:
  51.             Search_Data();
  52.             break;
  53.         case 7:
  54.             Add_End();
  55.             break;
  56.  
  57.            dafault:
  58.             printf("Invalid choice");
  59.  
  60.         }
  61.     }
  62. return 0;}
  63.  
  64. void Add_first()
  65. {
  66.     node *N=(node*)malloc(sizeof(node));
  67.     printf("Enter the number:");
  68.     scanf(" %d",&N->a);
  69.     N->next=NULL;
  70.     N->prev=NULL;
  71.     if(head==NULL)
  72.     {
  73.         head=N;
  74.         tail=N;
  75.     }
  76.     else
  77.     {
  78.         head->prev=N;
  79.         N->next=head;
  80.         head=N;
  81.  
  82.     }
  83. }
  84. void Add_any()
  85. {
  86.     int m;
  87.     node *list=head;
  88.     node *N=(node*)malloc(sizeof(node));
  89.     printf("Enter the number:");
  90.     scanf(" %d",&N->a);
  91.     N->next=NULL;
  92.     N->prev=NULL;
  93.     printf("Enter Position: ");
  94.     scanf(" %d",&m);
  95.     if(m==1 )
  96.     {
  97.         if(head==NULL)
  98.         {
  99.             head=N;
  100.             tail=N;
  101.         }
  102.         else
  103.         {
  104.             head->prev=N;
  105.             N->next=head;
  106.             head=N;
  107.         }
  108.     }
  109.     else
  110.     {
  111.         m=m-2;
  112.         while(m!=0 && list->next!=NULL)
  113.         {
  114.             list=list->next;
  115.             m--;
  116.         }
  117.         if(list->next==NULL)
  118.         {
  119.             N->prev=tail;
  120.             tail->next=N;
  121.             tail=N;
  122.  
  123.         }
  124.         else
  125.         {
  126.             list->next->prev=N;
  127.             N->next=list->next;
  128.             N->prev=list;
  129.             list->next=N;
  130.         }
  131.     }
  132.  
  133.  
  134. }
  135. void Add_End()
  136. {
  137.     node *N=(node*)malloc(sizeof(node));
  138.     printf("enter the number:");
  139.     scanf(" %d",&N->a);
  140.     N->next=NULL;
  141.     N->prev=NULL;
  142.     node *list=head;
  143.     if(head==NULL)
  144.     {
  145.         head=N;
  146.         tail=N;
  147.     }
  148.     else
  149.     {
  150.         tail->next=N;
  151.         N->prev=tail;
  152.         tail=N;
  153.     }
  154.  
  155. }
  156.  
  157.  
  158.  
  159. void  delete_data()
  160. {
  161.     int d;
  162.     printf("Enter the number:");
  163.     scanf(" %d",&d);
  164.     node*list=head;
  165.     if(head==NULL)
  166.     {
  167.         printf("No Data!!\n\n");
  168.     }
  169.     else if(list->a==d)
  170.     {
  171.         if(list->next==NULL)
  172.         {
  173.             head=NULL;
  174.             tail=NULL;
  175.             printf("Data Deleted\n\n");
  176.             free(list);
  177.         }
  178.         else
  179.         {
  180.             list->next->prev=NULL;
  181.             head=list->next;
  182.             printf("Data Deleted\n\n");
  183.             free(list);
  184.         }
  185.     }
  186.     else
  187.     {
  188.         node*temp;
  189.         while(list->a!=d && list->next!=NULL)
  190.         {
  191.             list=list->next;
  192.             if(list==NULL)
  193.             {
  194.                 printf("No Data Found!!\n\n");
  195.                 break;
  196.             }
  197.         }
  198.         if(list->a==d && list->next==NULL)
  199.         {
  200.             list=tail;
  201.             temp=list;
  202.             temp->prev->next=temp->next;
  203.             tail=temp->prev;
  204.             printf("Data Deleted\n\n");
  205.             free(temp);
  206.  
  207.         }
  208.         else if(list->a==d && list->next!=NULL)
  209.         {
  210.             temp=list;
  211.             temp->prev->next=temp->next;
  212.             temp->next->prev=temp->prev;
  213.             temp->next=NULL;
  214.             temp->prev=NULL;
  215.             printf("Data Deleted\n\n");
  216.             free(temp);
  217.         }
  218.  
  219.     }
  220.  
  221. }
  222. void Delete_Position()
  223. {
  224.  
  225.     int loc;
  226.     node *list=head,*temp;
  227.     printf("Enter your desired Position:");
  228.     scanf(" %d",&loc);
  229.     if(head==NULL)
  230.     {
  231.         printf("Invalid Position to delete!!\n\n");
  232.     }
  233.     else if(loc==1)
  234.     {
  235.         if(list->next==NULL)
  236.         {
  237.             printf("Data Deleted\n\n");
  238.             free(list);
  239.         }
  240.         else
  241.         {
  242.             head=list->next;
  243.             head->prev=NULL;
  244.             printf("Data Deleted\n\n");
  245.             free(list);
  246.         }
  247.     }
  248.     else
  249.     {
  250.         loc=loc-2;
  251.         while(loc!=0 && list->next!=NULL)
  252.         {
  253.             list=list->next;
  254.             loc--;
  255.             if(list==NULL)
  256.             {
  257.                 printf("No Data Found!!\n\n");
  258.                 break;
  259.             }
  260.         }
  261.  
  262.          if(list->next->next==NULL && loc==0)
  263.         {
  264.             temp=tail;
  265.             tail=tail->prev;
  266.             tail->next=NULL;
  267.             printf("Data Deleted\n\n");
  268.             free(temp);
  269.         }
  270.         else if(list->next->next!=NULL && loc==0)
  271.         {
  272.             temp=list->next;
  273.             temp->next->prev=list;
  274.             list->next=temp->next;
  275.             printf("Data Deleted\n\n");
  276.             free(temp);
  277.         }
  278.     }
  279.  
  280. }
  281.  
  282. void Search_Data()
  283. {
  284.     node *list=head;
  285.     int s;
  286.     printf("Enter Your number:");
  287.     scanf("%d",&s);
  288.     if(list->a==s)
  289.     {
  290.         printf(" Data Found: %d\n\n",list->a);
  291.     }
  292.     else
  293.     {
  294.         while(list->a!=s)
  295.         {
  296.             list=list->next;
  297.             if(list==NULL)
  298.             {
  299.                 printf("No Data!!\n\n");
  300.                 break;
  301.             }
  302.         }
  303.         printf("%d\n\n",list->a);
  304.     }
  305. }
  306.  
  307.  
  308. void display_first()
  309. {
  310.     node*list=head;
  311.     if(head==NULL)
  312.     {
  313.         printf("No Data to show\n");
  314.  
  315.  
  316.     }
  317.     else
  318.     {
  319.         while(list!=NULL)
  320.         {
  321.             printf("%d\n",list->a);
  322.             list=list->next;
  323.         }
  324.     }
  325.     }
Advertisement
Add Comment
Please, Sign In to add comment