Advertisement
muftY

Doubly by Mufty-13329

Feb 14th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct data
  4. {
  5.     int a;
  6.     struct data *prev;
  7.     struct data *next;
  8.  
  9. } data;
  10.  
  11. data *head=NULL;
  12. data *tail=NULL;
  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->prev=NULL;
  19.     new_node->next=NULL;
  20.  
  21.     if(head==NULL)
  22.     {
  23.         head=new_node;
  24.     }
  25.     else if(head!=NULL)
  26.     {
  27.  
  28.         head->prev=new_node;
  29.         new_node->next=head;
  30.         head=new_node;
  31.  
  32.     }
  33.  
  34. }
  35.  
  36. void ins_at_end(int x)
  37. {
  38.     data *new_node=(data*)malloc(sizeof(data));
  39.     new_node->a=x;
  40.     new_node->prev=NULL;
  41.     new_node->next=NULL;
  42.  
  43.     if(head==NULL)
  44.     {
  45.         head=new_node;
  46.     }
  47.     else if(head!=NULL)
  48.     {
  49.         data *temp=head;
  50.  
  51.         while(temp->next!=NULL)
  52.         {
  53.             temp=temp->next;
  54.         }
  55.  
  56.         new_node->prev=temp;
  57.         temp->next=new_node;
  58.  
  59.     }
  60.  
  61. }
  62.  
  63. void ins_at_nth(int n,int x)
  64. {
  65.     data *new_node=(data*)malloc(sizeof(data));
  66.     new_node->a=x;
  67.     new_node->prev=NULL;
  68.     new_node->next=NULL;
  69.  
  70.     if(head==NULL)
  71.     {
  72.         head=new_node;
  73.         return;
  74.     }
  75.     if(n==1 && head!=NULL)
  76.     {
  77.         new_node->next=head;
  78.         head->prev=new_node;
  79.         head=new_node;
  80.         return;
  81.     }
  82.  
  83.     n=n-2;
  84.     data *temp=head;
  85.     while(n--)
  86.     {
  87.         temp=temp->next;
  88.         if(temp->next=NULL)
  89.         {
  90.             new_node->prev=temp;
  91.             temp->next=new_node;
  92.             return;
  93.         }
  94.     }
  95.     temp->next->prev=new_node;
  96.     new_node->next=temp->next;
  97.     new_node->prev=temp;
  98.     temp->next=new_node;
  99.  
  100. }
  101.  
  102. void del_by_pos(int n)
  103.  
  104. {
  105.  
  106.  
  107.     data *temp=head;
  108.     data *delx=NULL;
  109.     if(head==NULL)
  110.     {
  111.         printf("\n==>Wrong Command\n");
  112.         return;
  113.     }
  114.     if(n==1)
  115.     {
  116.         head=head->next;
  117.  
  118.         if(head==NULL)
  119.         {
  120.             tail=NULL;
  121.             free(delx);
  122.             return;
  123.         }
  124.         head->prev=NULL;
  125.         free(delx);
  126.         return;
  127.  
  128.     }
  129.     n=n-2;
  130.  
  131.     while(n--)
  132.     {
  133.         temp=temp->next;
  134.         if(temp->next==NULL)
  135.         {
  136.             printf("\nSorrrrry ,No Node to delete\n");
  137.             return;
  138.         }
  139.  
  140.     }
  141.     data *del=head->next;
  142.     head->next=del->next;
  143.     free(del);
  144. }
  145.  
  146. void del_by_value(int x)
  147. {
  148.     //Sorry Sir,4.00am submit er last Time. Ekhn 3.54 am hoye gece. Time er jonno complete korte parlm na..
  149. }
  150.  
  151. int sum()
  152. {
  153.     int s=0;
  154.     data *temp=head;
  155.     while(temp!=NULL)
  156.     {
  157.         s+=temp->a;
  158.         temp=temp->next;
  159.     }
  160.     return s;
  161.  
  162. }
  163. double avrg()
  164. {
  165.     int count=0;
  166.     double s=0;
  167.     data *temp=head;
  168.  
  169.     while(temp!=NULL)
  170.     {
  171.         s+=temp->a;
  172.         count++;
  173.         temp=temp->next;
  174.     }
  175.     if(count==0)
  176.     {
  177.         printf("\nInput some Value First.\n");
  178.         return 0;
  179.     }
  180.     return s/count;
  181.  
  182. }
  183.  
  184.  
  185. void reset(int x)
  186. {
  187.     head=NULL;
  188.     return;
  189. }
  190.  
  191. void print()
  192. {
  193.     data *temp=head;
  194.  
  195.     while(temp!=NULL)
  196.     {
  197.         printf("%d ",temp->a);
  198.         temp=temp->next;
  199.     }
  200.     if(head==NULL)
  201.     {
  202.         printf("Result is NULL.");
  203.     }
  204.     printf("\n");
  205.  
  206. }
  207.  
  208. int main()
  209. {
  210.     /* ins_at_nth(1,100);
  211.      print();
  212.      ins_at_end(6);
  213.      ins_at_first(5);
  214.      ins_at_first(4);
  215.      ins_at_first(3);
  216.      ins_at_end(6);
  217.      ins_at_nth(2,600);
  218.      print();
  219.  
  220.      del_by_pos(3);
  221.  
  222.      print();
  223.  
  224.  
  225.     */
  226.  
  227.  
  228.     printf("Hey ,ASSALAMUALAIKUM...\n");
  229.     while(1)
  230.     {
  231.         int n, m,y;
  232.         char p;
  233.         printf("\nWhat u wanna do?\n\n");
  234.         printf("1.WANNA INSERT AT FIRST?\n");
  235.         printf("2.WANNA INSERT AT END?\n");
  236.         printf("3.WANNA INSERT AT ANY POSITION?\n");
  237.         printf("4.WANNA DELETE BY POSITION ?\n");
  238.         printf("5.WANNA DELETE BY VALUE?[**incompleted**]\n");
  239.         printf("6.Wanna Print Sum Of These?\n");
  240.         printf("7.Wanna Print Average Of These All?\n");
  241.         printf("8.Wanna Print These All?\n\n");
  242.  
  243.         printf("##SELECT ANY OPTION FROM 1 to 8##\n");
  244.         printf("OR 9 to Reset All?\n");
  245.         printf("###Otherwise Enter 0(zero) TO QUIT !!!###\n");
  246.         scanf("%d",&m);
  247.         if(m==1 || m==2 || m==5)
  248.         {
  249.             printf("\nPlease Enter a Number->");
  250.  
  251.             if(m==1)
  252.             {
  253.                 scanf("%d",&y);
  254.                 ins_at_first(y);
  255.  
  256.             }
  257.             else if(m==2)
  258.             {
  259.                 scanf("%d",&y);
  260.                 ins_at_end(y);
  261.  
  262.             }
  263.  
  264.             /*    else if(m==5)
  265.                {
  266.                 scanf("%d",&y);
  267.                    dbv(y);
  268.  
  269.                }
  270.                */
  271.  
  272.         }
  273.         else if(m==3)
  274.         {
  275.  
  276.             printf("\nEnter the specific position=> ");
  277.  
  278.             scanf("%d",&n);
  279.             printf("\nEnter a Number What you wanna insert=> ");
  280.             scanf("%d",&y);
  281.             ins_at_nth(n,y);
  282.         }
  283.         else if(m==4)
  284.         {
  285.             printf("\nPlease Enter a Position-> ");
  286.             scanf("%d",&y);
  287.             del_by_pos(y);
  288.  
  289.         }
  290.  
  291.                 else if(m==6)
  292.                 {
  293.                     printf("Sum is =%d\n",sum());
  294.                 }
  295.                 else if(m==7)
  296.                 {
  297.                     printf("Average is=%.2lf\n",avrg());
  298.                 }
  299.  
  300.         else if(m==8)
  301.         {
  302.             printf("Here is the result=> ");
  303.             print();
  304.         }
  305.         else if(m==9)
  306.         {
  307.             printf("\nReset Successfully\n\n");
  308.             reset(0);
  309.         }
  310.  
  311.         else if(m==0)
  312.         {
  313.             printf("Thanks For Staying With Mufty :> \n\n\n");
  314.             break;
  315.         }
  316.     }
  317.  
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement