Advertisement
muftY

Doubly Link List Full (muftY(bugless))

Feb 25th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.89 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct data
  4. {
  5.     int a;
  6.     struct data *next;
  7.     struct data *prev;
  8. } data;
  9.  
  10. data *head=NULL;
  11. data *tail=NULL;
  12.  
  13.  
  14. void ins_at_first(int x)
  15. {
  16.     data *new_node=(data*)malloc(sizeof(data));
  17.     new_node->a=x;
  18.     new_node->next=NULL;
  19.     new_node->prev=NULL;
  20.  
  21.     if(head==NULL)
  22.  
  23.     {
  24.         head=new_node;
  25.         return;
  26.     }
  27.     new_node->next=head;
  28.     head->prev=new_node;
  29.     head=new_node;
  30.  
  31.     return;
  32. }
  33. void ins_at_end(int x)
  34. {
  35.     data *new_node=(data*)malloc(sizeof(data));
  36.     new_node->a=x;
  37.     new_node->next=NULL;
  38.     new_node->prev=NULL;
  39.  
  40.     if(head==NULL)
  41.     {
  42.         head=new_node;
  43.         return;
  44.     }
  45.     data *temp=head;
  46.     while(temp->next!=NULL)
  47.     {
  48.         temp=temp->next;
  49.     }
  50.     new_node->prev=temp;
  51.     temp->next=new_node;
  52.  
  53.     return;
  54. }
  55.  
  56. void ins_at_nth(int n, int x)
  57. {
  58.     data *new_node=(data*)malloc(sizeof(data));
  59.     new_node->a=x;
  60.     new_node->next=NULL;
  61.     new_node->prev=NULL;
  62.  
  63.     if(head==NULL)
  64.     {
  65.         head=new_node;
  66.         return;
  67.     }
  68.  
  69.     if(n==1)
  70.     {
  71.         new_node->next=head;
  72.  
  73.         head=new_node;
  74.  
  75.         return;
  76.     }
  77.  
  78.     data *temp=head;
  79.     n=n-2;
  80.  
  81.     while(n--)
  82.     {
  83.         temp=temp->next;
  84.         if(temp->next==NULL)
  85.         {
  86.             new_node->prev=temp;
  87.             temp->next=new_node;
  88.             tail=new_node;
  89.             return;
  90.         }
  91.     }
  92.     temp->next->prev=new_node;
  93.     new_node->next=temp->next;
  94.     new_node->prev=temp;
  95.     temp->next=new_node;
  96.  
  97.     return;
  98.  
  99.  
  100.  
  101. }
  102.  
  103. void del_by_pos(int n)
  104. {
  105.  
  106.     if(head==NULL) //jokhn kicui nai
  107.     {
  108.         printf("     List Empty\n");
  109.         return;
  110.     }
  111.     if(n==1) //jokhn n er man 1 dewa hy
  112.     {
  113.         data *del=head;
  114.         if(head->next==NULL) //jokhn n er man 1 dewa hy ebng list e oi ekti matroi node cilo
  115.         {
  116.             head=NULL;
  117.             tail=NULL;
  118.             free(del);
  119.             return;
  120.         }
  121.         head=head->next;
  122.         head->prev=NULL;
  123.         free(del);
  124.         return;
  125.  
  126.  
  127.     }
  128.     data *temp=head;
  129.     n=n-2;
  130.  
  131.     while(n--)
  132.     {
  133.         temp=temp->next;
  134.         if(temp->next==NULL) //jodi node sonkhar besi n er man dewa hy then ei "if" kaj korbe
  135.                             //(mane node ache 5ta . ami n=100 tomo node dlt korte cai then)
  136.         {
  137.             printf("\n    Invalid Position\n");
  138.             return;
  139.         }
  140.  
  141.     }
  142.     data *del=temp->next;
  143.     if(del->next==NULL) // list er last node dlt korar khetre
  144.     {
  145.  
  146.         tail=temp;
  147.         temp->next=NULL;
  148.         free(del);
  149.         return;
  150.     }
  151.     del->next->prev=temp; //list er moddher savabik node dlt korte bakituku.
  152.     temp->next=del->next;
  153.     free(del);
  154. }
  155.  
  156. void del_by_value(int x)
  157. {
  158.     if(head==NULL)
  159.     {
  160.         printf("\n    List was empty previously\n");
  161.         return;
  162.     }
  163.     if(head->a==x)
  164.     {
  165.  
  166.         data *del=head;
  167.  
  168.         if(head->next==NULL)
  169.         {
  170.             head=NULL;
  171.             tail=NULL;
  172.             free(del);
  173.             return;
  174.         }
  175.         head=head->next;
  176.         head->prev=NULL;
  177.         free(del);
  178.         return;
  179.     }
  180.  
  181.  
  182.     data *temp=head;
  183.     while(temp->next->a!=x)
  184.     {
  185.         temp=temp->next;
  186.         if(temp->next==NULL)
  187.         {
  188.             printf("\n    No Such Value in this current List\n");
  189.             return;
  190.         }
  191.     }
  192.     data *del=temp->next;
  193.     if(del->next==NULL)
  194.     {
  195.         tail=temp;
  196.         tail->next=NULL;
  197.         free(del);
  198.         return;
  199.     }
  200.     del->next->prev=temp;
  201.     temp->next=del->next;
  202.     free(del);
  203.     return;
  204. }
  205.  
  206.  
  207. void reset()
  208. {
  209.     data *temp=head;
  210.     while(temp!=NULL)
  211.     {
  212.         data *del=temp;
  213.  
  214.         temp=temp->next;
  215.         free(del);
  216.     }
  217.     head=NULL;
  218.     tail=NULL;
  219.     return;
  220. }
  221.  
  222. void print()
  223. {
  224.     data *temp=head;
  225.     if(head==NULL)
  226.     {
  227.         printf("Empty\n");
  228.  
  229.     }
  230.  
  231.     while(temp!=NULL)
  232.     {
  233.         printf("%d ",temp->a);
  234.         temp=temp->next;
  235.     }
  236.     printf("\n");
  237. }
  238.  
  239. int main()
  240. {
  241.     int a,v,w,x,n,m,o;
  242.     while(1)
  243.     {
  244.         printf("1.Insert At First\n");
  245.         printf("2.Insert At End\n");
  246.         printf("3.Insert At n_th Position\n");
  247.  
  248.         printf("4.Delete By Position\n");
  249.         printf("5.Delete By Value\n");
  250.  
  251.         printf("8.Print\n");
  252.         printf("9.RESET LINK LIST\n");
  253.  
  254.         printf("\n ###0 for Al-Bida###\n");
  255.         printf("\n             Command:");
  256.         scanf("%d",&w);
  257.         if(w==1)
  258.         {
  259.             printf("\nEnter an integer Number:");
  260.             scanf("%d",&x);
  261.             ins_at_first(x);
  262.              printf("\n");
  263.  
  264.         }
  265.         if(w==2)
  266.         {
  267.             printf("\nEnter an integer Number:");
  268.             scanf("%d",&x);
  269.             ins_at_end(x);
  270.              printf("\n");
  271.         }
  272.         if(w==3)
  273.         {
  274.             printf("\nDeclear The Position:");
  275.             scanf("%d",&n);
  276.             printf("Enter an integer Number:");
  277.             scanf("%d",&x);
  278.             ins_at_nth(n,x);
  279.              printf("\n");
  280.         }
  281.  
  282.         if(w==4)
  283.         {
  284.             printf("\nDeclear The Specific Position:");
  285.             scanf("%d",&n);
  286.  
  287.             del_by_pos(n);
  288.              printf("\n");
  289.  
  290.         }
  291.          if(w==5)
  292.         {
  293.             printf("\nDeclear The Value:");
  294.             scanf("%d",&n);
  295.             del_by_value(n);
  296.              printf("\n");
  297.         }
  298.  
  299.  
  300.         if(w==8)
  301.         {
  302.             printf("\n            Result: ");
  303.             print();
  304.             printf("\n");
  305.         }
  306.  
  307.         if(w==9)
  308.         {
  309.             printf("\n      Reseted Successfully\n");
  310.             reset();
  311.              printf("\n");
  312.         }
  313.  
  314.         if(w==0)
  315.         {
  316.             printf("\n    Jajakalla_khayer..[muftY]\n");
  317.              printf("\n");
  318.             break;
  319.         }
  320.     }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement