SAADQUAMER

Doubly_linklist

Oct 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.  
  7.     int a;
  8.     char ch;
  9.     struct node*next;
  10.     struct node*previous;
  11. } node;
  12.  
  13. node*head=NULL,*tail=NULL;
  14.  
  15.  
  16. void insert_at_first(int aN, char chN)
  17. {
  18.     node *N=(node*)malloc(sizeof(node));
  19.     N->a=aN;
  20.     N->ch=chN;
  21.     N->next=NULL;
  22.     N->previous=NULL;
  23.     if(head==NULL)
  24.     {
  25.         head=N;
  26.         return;
  27.     }
  28.     N->next=head;
  29.     head->previous=N;
  30.     head=N;
  31. }
  32.  
  33.  
  34. void insert_at_end(int aN,char chN)
  35. {
  36.  
  37.     node*N=(node*)malloc(sizeof(node));
  38.     N->a=aN;
  39.     N->ch=chN;
  40.     N->next=NULL;
  41.     N->previous=NULL;
  42.     node*list=head;
  43.     if(head==NULL)
  44.     {
  45.         head=N;
  46.     }
  47.     else
  48.     {
  49.         while(list->next!=NULL)
  50.         {
  51.             list=list->next;
  52.             N->previous=list;
  53.         }
  54.         list->next=N;
  55.         N->next=NULL;
  56.         N->previous=list;
  57.  
  58.  
  59.     }
  60.  
  61. }
  62.  
  63.  
  64. void insert_nth(int n,int aN,char chN)
  65. {
  66.     node *N=(node*)malloc(sizeof(node));
  67.     N->a=aN;
  68.     N->ch=chN;
  69.     N->next=NULL;
  70.     N->previous=NULL;
  71.     if(n==1)
  72.     {
  73.         N->previous=NULL;
  74.         N->next=head;
  75.         head=N;
  76.  
  77.         return;
  78.     }
  79.     else
  80.     {
  81.         node*list=head;
  82.         n=n-2;
  83.         while(n!=0&&list->next!=NULL)
  84.         {
  85.             list=list->next;
  86.             n--;
  87.         }
  88.         N->next=list->next;
  89.         if(list->next !=NULL)
  90.         {
  91.             list->next->previous=N;
  92.         }
  93.  
  94.         list->next=N;
  95.     }
  96. }
  97.  
  98.  
  99. void create()
  100. {
  101.     int value;
  102.     char ch;
  103.     while(1)
  104.     {
  105.         printf("   \n\tENTER  NUMBER & Character (int,char) (-1 for exit)\n");
  106.         printf("\t");
  107.         scanf("%d",&value);
  108.         if(value==-1)
  109.         {
  110.             display();
  111.             printf("\tNode Create Completed!!");
  112.             MENU();
  113.         }
  114.         scanf(" %c",&ch);
  115.  
  116.  
  117.         insert_at_end(value,ch);
  118.  
  119.     }
  120.  
  121.  
  122. }
  123.  
  124. void delete_item(int value)
  125. {
  126.     node *list = head, *temp=NULL;
  127.     int x = 0;
  128.  
  129.     while(list!=NULL)
  130.     {
  131.         if(list->a==value)
  132.         {
  133.             if(temp==NULL)
  134.             {
  135.                 head = list->next;
  136.                 head->previous=NULL;
  137.             }
  138.             else
  139.                 temp->next = list->next;
  140.  
  141.  
  142.             printf("%d is deleted from list\n", value);
  143.             x = 1;
  144.             free(list);
  145.             break;
  146.         }
  147.         temp = list;
  148.         list = list->next;
  149.         list->previous=temp;
  150.     }
  151.     if(x==0)
  152.         printf("Key not found!\n");
  153. }
  154.  
  155.  
  156. int search()
  157. {
  158.     int n;
  159.     printf("   \n\t<<<<<<<<SEARCH VALUE>>>>>>>>\n\n");
  160.     printf("\tENTER ANY VALUE FOR SERACH :");
  161.     scanf("%d",&n);
  162.  
  163.     node*list=head;
  164.     int x=0;
  165.     while(list!=NULL)
  166.     {
  167.         if(list->a==n)
  168.         {
  169.             printf("\n\t%d is Found\n\n",n);
  170.             x=1;
  171.             break;
  172.         }
  173.         else
  174.  
  175.             list=list->next;
  176.     }
  177.     if(x==0)
  178.     {
  179.         printf("\n\t%d is  Not Found\n\n",n);
  180.     }
  181.     MENU();
  182. }
  183.  
  184.  
  185. void addnode()
  186. {
  187.     int af,y;
  188.     char cf;
  189.     printf("\n\n\n");
  190.     printf("\t1.Insert At First.\n");
  191.     printf("\t2.Insert At LAST.\n");
  192.     printf("\t3.Insert At Nth Position.\n");
  193.     printf("\t4.GO TO MENU\n");
  194.     printf("\n\tChoose Any Option (1-4):");
  195.     int x;
  196.  
  197.     scanf("%d",&x);
  198.     switch(x)
  199.     {
  200.     case 1:
  201.     {
  202.         printf("\n\t<<<<<<<<Insert At FIRST(int,char)>>>>>>>>\n");
  203.  
  204.         printf("\t");
  205.         scanf("%d",&af);
  206.         scanf(" %c",&cf);
  207.         insert_at_first(af,cf);
  208.         display();
  209.         addnode();
  210.  
  211.     }
  212.     case 2:
  213.     {
  214.         printf("\n\t<<<<<<<<Insert At END(int,char)>>>>>>>>\n");
  215.  
  216.  
  217.         printf("\t");
  218.         scanf("%d",&af);
  219.         scanf(" %c",&cf);
  220.         insert_at_end(af,cf);
  221.         display();
  222.         addnode();
  223.  
  224.     }
  225.     case 3:
  226.     {
  227.         printf("\n\t<<<<<<<<Insert At Nth(pos,int,char)>>>>>>>>\n");
  228.  
  229.         printf("\t");
  230.         scanf("%d",&y);
  231.         scanf("%d",&af);
  232.         scanf(" %c",&cf);
  233.         insert_nth(y,af,cf);
  234.         display();
  235.         addnode();
  236.  
  237.     }
  238.     case 4:
  239.     {
  240.         MENU();
  241.         break;
  242.     }
  243.     default:
  244.     {
  245.         printf("\tCHOOSE A CORRECT OPTION\n");
  246.         addnode();
  247.     }
  248.     }
  249.  
  250. }
  251.  
  252.  
  253. void delete()
  254. {
  255.  
  256.     printf("\n\t<<<<<<<<<DELETE NODE>>>>>>>>>\n");
  257.     printf("\t1.Delete By Position \n");
  258.     printf("\t2.Delete By Value \n");
  259.     printf("\t3.GO TO MENU \n\n");
  260.     printf("\tChoose Any Option (1-3):");
  261.     int x,s,l;
  262.  
  263.  
  264.     scanf("%d",&x);
  265.     switch(x)
  266.     {
  267.     case 1:
  268.     {
  269.         printf("   \n\t<<<<<<<<DELETE BY POSITION>>>>>>>>\n");
  270.         printf("\t");
  271.         printf("ENTER POSITION:");
  272.         scanf("%d",&s);
  273.         delete_pos(s);
  274.         delete();
  275.     }
  276.     case 2:
  277.     {
  278.         printf("   \n\t<<<<<<<<DELETE BY VALUE>>>>>>>>\n");
  279.         printf("\t");
  280.         printf("ENTER VALUE:");
  281.         scanf("%d",&l);
  282.         delete_item(l);
  283.         delete();
  284.     }
  285.     case 3:
  286.     {
  287.         MENU();
  288.         break;
  289.     }
  290.     default:
  291.     {
  292.         printf("\tCHOOSE A CORRECT OPTION\n");
  293.         delete();
  294.     }
  295.     }
  296.  
  297.  
  298. }
  299.  
  300.  
  301. void delete_pos(int pos)
  302. {
  303.     node *list=head,*temp=NULL,*tail;
  304.  
  305.  
  306.     if(pos==1)
  307.     {
  308.         if(list->next==NULL)
  309.         {
  310.             printf("DATA DELETED\n\n");
  311.             free(list);
  312.         }
  313.         else
  314.         {
  315.             head=list->next;
  316.             head->previous=NULL;
  317.             printf("DATA DELETED\n\n");
  318.             free(list);
  319.         }
  320.     }
  321.     else
  322.     {
  323.         pos=pos-2;
  324.         while(pos!=0 && list->next!=NULL)
  325.         {
  326.             list=list->next;
  327.             pos--;
  328.             if(list==NULL)
  329.             {
  330.                 printf("NO DATA FOUND!!\n\n");
  331.                 break;
  332.             }
  333.         }
  334.         if(list->next==NULL)
  335.         {
  336.             printf("You Choose Wrong Position!!\n\n");
  337.         }
  338.         else if(list==NULL && pos==0)
  339.         {
  340.             temp=tail;
  341.             tail=temp->previous;
  342.             tail->next=NULL;
  343.             printf("DATA DELETED\n\n");
  344.             free(temp);
  345.         }
  346.         else if(list!=NULL && pos==0)
  347.         {
  348.             temp=list->next;
  349.             temp->next->previous=list;
  350.             list->next=temp->next;
  351.             printf("DATA DELETED\n\n");
  352.             free(temp);
  353.         }
  354.     }
  355. }
  356.  
  357.  
  358.  
  359.  
  360.  
  361. void display()
  362. {
  363.  
  364.     node *list=head;
  365.  
  366.     if(head==NULL)
  367.     {
  368.         printf("\n\n\tNode is empty!!\n\n");
  369.         printf("\tENTER DATA PLEASE!!\n");
  370.         MENU();
  371.  
  372.     }
  373.     while(list !=NULL)
  374.     {
  375.         printf("\n\n\tA= %d ,C= %c\n",list->a,list->ch);
  376.  
  377.  
  378.         list=list->next;
  379.  
  380.     }
  381.  
  382. }
  383.  
  384.  
  385. void MENU()
  386. {
  387.  
  388.     printf("\n\n\t<<<<<<<<<<  WELCOME TO DOUBLY LINK LIST  >>>>>>>>>>>>>\n\n");
  389.     printf("\t1.Create Nodes\n");
  390.     printf("\t2.ADD Nodes\n");
  391.     printf("\t3.Search Nodes\n");
  392.     printf("\t4.DELETE Nodes\n");
  393.     printf("\t5.Display\n");
  394.     printf("\t6.EXIT \n\n\n");
  395.  
  396.     printf("\tChoose Any Option (1-5):");
  397.     int x;
  398.  
  399.  
  400.     scanf("%d",&x);
  401.     switch(x)
  402.     {
  403.     case 1:
  404.     {
  405.         create();
  406.         break;
  407.     }
  408.     case 2:
  409.     {
  410.         addnode();
  411.         break;
  412.     }
  413.     case 3:
  414.     {
  415.         search();
  416.         break;
  417.     }
  418.     case 4:
  419.     {
  420.         delete();
  421.         break;
  422.     }
  423.     case 5:
  424.     {
  425.         dis();
  426.         break;
  427.     }
  428.     case 6:
  429.     {
  430.         break;
  431.     }
  432.     default:
  433.     {
  434.         printf("CHOOSE A CORRECT OPTION\n");
  435.         MENU();
  436.     }
  437.     }
  438. }
  439.  
  440.  
  441. void dis()
  442. {
  443.  
  444.     node *list=head;
  445.  
  446.     if(head==NULL)
  447.     {
  448.         printf("\n\n\tNode is empty!!\n\n");
  449.         printf("\tENTER DATA PLEASE!!\n");
  450.         MENU();
  451.  
  452.     }
  453.     while(list !=NULL)
  454.     {
  455.         printf("\n\n\tA= %d ,C= %c\n",list->a,list->ch);
  456.  
  457.  
  458.         list=list->next;
  459.  
  460.  
  461.     }
  462.  
  463.     MENU();
  464. }
  465.  
  466. int main()
  467. {
  468.     head=NULL;
  469.     MENU();
  470.  
  471.  
  472. }
Advertisement
Add Comment
Please, Sign In to add comment