rony-Rony_05

doubly full

Oct 13th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.20 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. node*head;
  13.  
  14.  
  15.  
  16.  
  17. void display()
  18. {
  19.  
  20.     node *list=head;
  21.     while(list !=NULL)
  22.     {
  23.         printf("A: %d\n",list->a);
  24.         printf("C: %c\n",list->ch);
  25.         printf("\n");
  26.  
  27.         list=list->next;
  28.  
  29.     }
  30. }
  31.  
  32.  
  33.  
  34. void insert_at_first(int aN, char chN)
  35. {
  36.     node *N=(node*)malloc(sizeof(node));
  37.     N->a=aN;
  38.     N->ch=chN;
  39.     N->next=NULL;
  40.     N->previous=NULL;
  41.     if(head==NULL)
  42.     {
  43.         head=N;
  44.         return;
  45.     }
  46.     N->next=head;
  47.     head->previous=N;
  48.     head=N;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. void insert_at_end(int aN,char chN)
  55. {
  56.  
  57.     node*N=(node*)malloc(sizeof(node));
  58.     N->a=aN;
  59.     N->ch=chN;
  60.     N->next=NULL;
  61.     N->previous=NULL;
  62.     node*list=head;
  63.     if(head==NULL)
  64.     {
  65.         head=N;
  66.     }
  67.     else
  68.     {
  69.         while(list->next!=NULL)
  70.         {
  71.             list=list->next;
  72.             N->previous=list;
  73.         }
  74.         list->next=N;
  75.         N->next=NULL;
  76.         N->previous=list;
  77.  
  78.  
  79.     }
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86. void insert_nth(int n,int aN,char chN)
  87. {
  88.     node *N=(node*)malloc(sizeof(node));
  89.     N->a=aN;
  90.     N->ch=chN;
  91.     N->next=NULL;
  92.     N->previous=NULL;
  93.     if(n==1)
  94.     {
  95.         N->previous=NULL;
  96.         N->next=head;
  97.         head=N;
  98.  
  99.         return;
  100.     }
  101.     else
  102.     {
  103.         node*list=head;
  104.         n=n-2;
  105.         while(n!=0&&list->next!=NULL)
  106.         {
  107.             list=list->next;
  108.             n--;
  109.         }
  110.         N->next=list->next;
  111.         if(list->next !=NULL)
  112.         {
  113.             list->next->previous=N;
  114.         }
  115.  
  116.         list->next=N;
  117.     }
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. int search(int n)
  126. {
  127.     node*list=head;
  128.     int x=0;
  129.     while(list!=NULL)
  130.     {
  131.         if(list->a==n)
  132.         {
  133.             printf("\n%d is Found\n\n",n);
  134.             x=1;
  135.             break;
  136.         }
  137.         else
  138.  
  139.             list=list->next;
  140.     }
  141.     if(x==0)
  142.     {
  143.         printf("\n%d is  Not Found\n\n",n);
  144.     }
  145. }
  146.  
  147.  
  148.  
  149. void create()
  150. {
  151.     int value;
  152.     char ch;
  153.     while(1)
  154.     {
  155.         printf("   \nENTER  NUMBER & Character(-1 for exit)\n");
  156.         scanf("%d",&value);
  157.         if(value==-1)
  158.         {
  159.             break;
  160.         }
  161.         scanf(" %c",&ch);
  162.  
  163.  
  164.         insert_at_end(value,ch);
  165.  
  166.     }
  167.  
  168.  
  169. }
  170.  
  171.  
  172.  
  173. void delete_pos(int pos)
  174. {
  175.     node *list=head,*temp=NULL,*tail;
  176.  
  177.  
  178.     if(pos==1)
  179.     {
  180.         if(list->next==NULL)
  181.         {
  182.             printf("DATA DELETED\n\n");
  183.             free(list);
  184.         }
  185.         else
  186.         {
  187.             head=list->next;
  188.             head->previous=NULL;
  189.             printf("DATA DELETED\n\n");
  190.             free(list);
  191.         }
  192.     }
  193.     else
  194.     {
  195.         pos=pos-2;
  196.         while(pos!=0 && list->next!=NULL)
  197.         {
  198.             list=list->next;
  199.             pos--;
  200.             if(list==NULL)
  201.             {
  202.                 printf("NO DATA FOUND!!\n\n");
  203.                 break;
  204.             }
  205.         }
  206.         if(list->next==NULL)
  207.         {
  208.             printf("You Choose Wrong Position!!\n\n");
  209.         }
  210.         else if(list==NULL && pos==0)
  211.         {
  212.             temp=tail;
  213.             tail=temp->previous;
  214.             tail->next=NULL;
  215.             printf("DATA DELETED\n\n");
  216.             free(temp);
  217.         }
  218.         else if(list!=NULL && pos==0)
  219.         {
  220.             temp=list->next;
  221.             temp->next->previous=list;
  222.             list->next=temp->next;
  223.             printf("DATA DELETED\n\n");
  224.             free(temp);
  225.         }
  226.     }
  227. }
  228.  
  229.  
  230.  
  231. void delete_item(int value)
  232. {
  233.     node *list = head, *temp=NULL;
  234.     int x = 0;
  235.  
  236.     while(list!=NULL)
  237.     {
  238.         if(list->a==value)
  239.         {
  240.             if(temp==NULL)
  241.             {
  242.                 head = list->next;
  243.                 head->previous=NULL;
  244.             }
  245.             else
  246.                 temp->next = list->next;
  247.  
  248.  
  249.             printf("%d is deleted from list\n", value);
  250.             x = 1;
  251.             free(list);
  252.             break;
  253.         }
  254.         temp = list;
  255.         list = list->next;
  256.         list->previous=temp;
  257.     }
  258.     if(x==0)
  259.         printf("Key not found!\n");
  260. }
  261.  
  262.  
  263.  
  264. void MENU()
  265. {
  266.     int x,af,l;
  267.     char cf;
  268.  
  269.     printf("   \n<<<<<<<<Create Doubly Linked List>>>>>>>>\n");
  270.     create();
  271.     display();
  272.     printf("   \n<<<<<<<<Insert At FIRST>>>>>>>>\n");
  273.     scanf("%d",&af);
  274.     scanf(" %c",&cf);
  275.     insert_at_first(af,cf);
  276.     display();
  277.     printf("   \n<<<<<<<<Insert At END>>>>>>>>\n");
  278.     scanf("%d",&af);
  279.     scanf(" %c",&cf);
  280.     insert_at_end(af,cf);
  281.     display();
  282.     printf("   \n<<<<<<<<Insert At Nth(pos,int,char)>>>>>>>>\n");
  283.     scanf("%d",&x);
  284.     scanf("%d",&af);
  285.     scanf(" %c",&cf);
  286.     insert_nth(x,af,cf);
  287.     display();
  288.     printf("   \n<<<<<<<<SEARCH VALUE>>>>>>>>\n");
  289.     scanf("%d",&l);
  290.     search(l);
  291.     printf("   \n<<<<<<<<DELETE BY POSITION>>>>>>>>\n");
  292.     scanf("%d",&x);
  293.     delete_pos(x);
  294.     display();
  295.     printf("   \n<<<<<<<<DELETE BY VALUE>>>>>>>>\n");
  296.     scanf("%d",&l);
  297.     delete_item(l);
  298.     printf("   \n<<<<<<<<FINAL VALUE>>>>>>>>\n");
  299.     display();
  300.     return 0;
  301.  
  302. }
  303.  
  304.  
  305.  
  306.  
  307. int main()
  308. {
  309.     head=NULL;
  310.     MENU();
  311.  
  312. }
Add Comment
Please, Sign In to add comment