Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct data
- {
- int a;
- struct data *next;
- } data;
- data *head=NULL;
- //Inser At First
- void fst(int x)
- {
- data *new =(data*)malloc(sizeof(data));
- new->a=x;
- new->next=NULL;
- if(head==NULL)
- {
- head=new;
- return;
- }
- new->next=head;
- head=new;
- }
- //Inser At End
- void end(int x)
- {
- data *new =(data*)malloc(sizeof(data));
- new->a=x;
- new->next=NULL;
- if(head==NULL)
- {
- head=new;
- return;
- }
- data *temp=head;
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- temp->next=new;
- }
- //Inser At N-th Position
- void nth(int n,int x)
- {
- data *new =(data*)malloc(sizeof(data));
- new->a=x;
- new->next=NULL;
- if(n==1 || head==NULL)
- {
- if(head==NULL)
- {
- printf("!!!Caution:Given Position is not avavilable .\nThat's why your input was placed at the 1st Position\n\n");
- }
- new->next=head;
- head=new;
- return;
- }
- data *temp=head;
- n=n-2;
- while(n-- && temp->next!=NULL)
- {
- temp=temp->next;
- }
- new->next=temp->next;
- temp->next=new;
- }
- //Delete By Position(dpb)
- void dbp(int n)
- {
- data *del=NULL;
- data *temp=head;
- if(head==NULL)
- {
- printf("\nSorry, nothing to delete\n\n");
- return;
- }
- else if(n==1)
- {
- del=head;
- head=head->next;
- free(del);
- return;
- }
- n=n-2;
- while(n--)
- {
- temp=temp->next;
- if(temp->next==NULL)
- {
- printf("\nSorry, nothing to delete\n\n");
- return;
- }
- }
- del=temp->next;
- temp->next=del->next;
- free(del);
- }
- //delete by value(dbv)
- void dbv(int x)
- {
- data *del=NULL;
- data *temp=head;
- if(head==NULL)
- {
- printf("\nSorry, nothing to delete\n\n");
- return;
- }
- if(head->a==x)
- {
- del=head;
- head=del->next;
- free(del);
- return;
- }
- while(temp->next->a!=x)
- {
- temp=temp->next;
- if(temp->next==NULL)
- {
- printf("\nSorry, nothing to delete\n\n");
- return;
- }
- }
- del=temp->next;
- temp->next=temp->next->next;
- free(del);
- }
- //sum
- int sum()
- {
- int s=0;
- data *temp=head;
- while(temp!=NULL)
- {
- s+=temp->a;
- temp=temp->next;
- }
- return s;
- }
- // Average
- double avrg()
- {
- double s=0;
- int count=0;
- data *temp=head;
- while(temp!=NULL)
- {
- s+=temp->a;
- count++;
- temp=temp->next;
- }
- if(count==0)
- {
- printf("\nYou have not Input Any Value YET.\n");
- printf("***You may Chooose Another option...***\n");
- return 0;
- }
- return s/count;
- }
- //Print Function
- void print()
- {
- data *temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->a);
- temp=temp->next;
- }
- printf("\n");
- }
- void reset(int x)
- {
- head=NULL;
- return;
- }
- int main()
- {
- //Fixed Input
- /* end(1212);
- fst(9);
- end(10);
- fst(8);
- end(11);
- print();
- nth(1,111);4
- print();
- printf("Sum=%d\n",sum());
- printf("Averaxe=%.2lf \n",avrg());
- dbp(7);
- print();
- dbv(111);
- print();
- dbv(8);
- print();
- dbv(9);
- print();
- printf("Sum=%d \n",sum());
- printf("Average=%.2lf \n",avrg());
- */
- //User Input Interface
- printf("\nHey ,ASSALAMUALAIKUM...\n");
- while(1)
- {
- int n, m,y;
- char p;
- printf("\n\nWhat u wanna do?\n\n");
- printf("1.WANNA INSERT AT FIRST?\n");
- printf("2.WANNA INSERT AT END?\n");
- printf("3.WANNA INSERT AT ANY POSITION?\n");
- printf("4.WANNA DELETE BY POSITION ?\n");
- printf("5.WANNA DELETE BY VALUE?\n");
- printf("6.Wanna Print Sum Of These?\n");
- printf("7.Wanna Print Average Of These All?\n");
- printf("8.Wanna Print These All?\n\n");
- printf("##SELECT ANY OPTION FROM 1 to 8##\n\n");
- printf("OR 9 to Reset All?\n\n");
- printf("###Otherwise Enter 0(zero) TO QUIT !!!###\n");
- scanf("%d",&m);
- if(m==1 || m==2 || m==5)
- {
- printf("\nPlease Enter a Number->");
- if(m==1)
- {
- scanf("%d",&y);
- fst(y);
- }
- else if(m==2)
- {
- scanf("%d",&y);
- end(y);
- }
- else if(m==5)
- {
- scanf("%d",&y);
- dbv(y);
- }
- }
- else if(m==3)
- {
- printf("\nEnter the specific position=> ");
- scanf("%d",&n);
- printf("\nEnter a Number What you wanna insert=> ");
- scanf("%d",&y);
- nth(n,y);
- }
- else if(m==4)
- {
- printf("\nPlease Enter a Position-> ");
- scanf("%d",&y);
- dbp(y);
- }
- else if(m==6)
- {
- printf("Sum is =%d\n",sum());
- }
- else if(m==7)
- {
- printf("Average is=%.2lf\n",avrg());
- }
- else if(m==8)
- {
- printf("Here is the result=> ");
- print();
- }
- else if(m==0)
- {
- printf("Thanks For Staying With Mufty :> \n\n\n");
- break;
- }
- else if(m==9)
- {
- printf("\nReset Successfully\n\n");
- reset(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement