Advertisement
muftY

Singly by muftY

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