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 *prev;
- struct data *next;
- } data;
- data *head=NULL;
- data *tail=NULL;
- void ins_at_first(int x)
- {
- data *new_node=(data*)malloc(sizeof(data));
- new_node->a=x;
- new_node->prev=NULL;
- new_node->next=NULL;
- if(head==NULL)
- {
- head=new_node;
- }
- else if(head!=NULL)
- {
- head->prev=new_node;
- new_node->next=head;
- head=new_node;
- }
- }
- void ins_at_end(int x)
- {
- data *new_node=(data*)malloc(sizeof(data));
- new_node->a=x;
- new_node->prev=NULL;
- new_node->next=NULL;
- if(head==NULL)
- {
- head=new_node;
- }
- else if(head!=NULL)
- {
- data *temp=head;
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- new_node->prev=temp;
- temp->next=new_node;
- }
- }
- void ins_at_nth(int n,int x)
- {
- data *new_node=(data*)malloc(sizeof(data));
- new_node->a=x;
- new_node->prev=NULL;
- new_node->next=NULL;
- if(head==NULL)
- {
- head=new_node;
- return;
- }
- if(n==1 && head!=NULL)
- {
- new_node->next=head;
- head->prev=new_node;
- head=new_node;
- return;
- }
- n=n-2;
- data *temp=head;
- while(n--)
- {
- temp=temp->next;
- if(temp->next=NULL)
- {
- new_node->prev=temp;
- temp->next=new_node;
- return;
- }
- }
- temp->next->prev=new_node;
- new_node->next=temp->next;
- new_node->prev=temp;
- temp->next=new_node;
- }
- void del_by_pos(int n)
- {
- data *temp=head;
- data *delx=NULL;
- if(head==NULL)
- {
- printf("\n==>Wrong Command\n");
- return;
- }
- if(n==1)
- {
- head=head->next;
- if(head==NULL)
- {
- tail=NULL;
- free(delx);
- return;
- }
- head->prev=NULL;
- free(delx);
- return;
- }
- n=n-2;
- while(n--)
- {
- temp=temp->next;
- if(temp->next==NULL)
- {
- printf("\nSorrrrry ,No Node to delete\n");
- return;
- }
- }
- data *del=head->next;
- head->next=del->next;
- free(del);
- }
- void del_by_value(int x)
- {
- //Sorry Sir,4.00am submit er last Time. Ekhn 3.54 am hoye gece. Time er jonno complete korte parlm na..
- }
- int sum()
- {
- int s=0;
- data *temp=head;
- while(temp!=NULL)
- {
- s+=temp->a;
- temp=temp->next;
- }
- return s;
- }
- double avrg()
- {
- int count=0;
- double s=0;
- data *temp=head;
- while(temp!=NULL)
- {
- s+=temp->a;
- count++;
- temp=temp->next;
- }
- if(count==0)
- {
- printf("\nInput some Value First.\n");
- return 0;
- }
- return s/count;
- }
- void reset(int x)
- {
- head=NULL;
- return;
- }
- void print()
- {
- data *temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->a);
- temp=temp->next;
- }
- if(head==NULL)
- {
- printf("Result is NULL.");
- }
- printf("\n");
- }
- int main()
- {
- /* ins_at_nth(1,100);
- print();
- ins_at_end(6);
- ins_at_first(5);
- ins_at_first(4);
- ins_at_first(3);
- ins_at_end(6);
- ins_at_nth(2,600);
- print();
- del_by_pos(3);
- print();
- */
- printf("Hey ,ASSALAMUALAIKUM...\n");
- while(1)
- {
- int n, m,y;
- char p;
- printf("\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?[**incompleted**]\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");
- printf("OR 9 to Reset All?\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);
- ins_at_first(y);
- }
- else if(m==2)
- {
- scanf("%d",&y);
- ins_at_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);
- ins_at_nth(n,y);
- }
- else if(m==4)
- {
- printf("\nPlease Enter a Position-> ");
- scanf("%d",&y);
- del_by_pos(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==9)
- {
- printf("\nReset Successfully\n\n");
- reset(0);
- }
- else if(m==0)
- {
- printf("Thanks For Staying With Mufty :> \n\n\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement