Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //bismilllah hir rahmanir rahim
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct data
- {
- int a;
- struct data *next;
- struct data *prev;
- } data;
- data *head=NULL;
- data *tail=NULL;
- void insert_at_first(int x)
- {
- data *laboni=(data*)malloc(sizeof(data));
- laboni->a=x;
- laboni->next=NULL;
- laboni->prev=NULL;
- if(head==NULL)
- {
- head=laboni;
- return;
- }
- head->prev=laboni;
- head->next=laboni;
- head=laboni;
- }
- void insert_at_end(int x)
- {
- data *laboni=(data*)malloc(sizeof(data));
- laboni->prev=NULL;
- laboni->a=x;
- laboni->next=NULL;
- if(head==NULL)
- {
- head=laboni;
- return;
- }
- data *temp=head;
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- temp->next=laboni;
- laboni->prev=temp;
- }
- void InsertAtN(int x,int d)
- {
- int i;
- data *laboni=(data*)malloc(sizeof(data));
- data*temp1;
- data*temp2;
- printf("\nEnter the Position : ");
- laboni->a=x;
- printf("\nEnter the Data : ");
- laboni->a=d;
- if(x==1 || head==NULL)
- {
- if(head==NULL && x==1)
- {
- laboni->prev=laboni->next=NULL;
- head=laboni;
- return;
- }
- head->prev=laboni;
- laboni->prev=NULL;
- laboni->next=head;
- head=laboni;
- return;
- }
- temp1=head;
- for(i=0; i<x-2; i++)
- {
- temp1=temp1->next;
- }
- laboni->next=temp1->next;
- laboni->prev=temp1;
- temp1->next=laboni;
- temp2=laboni->next;
- temp2->prev=laboni;
- }
- void deletenode(int n)
- {
- data *temp=head;
- data *del=temp->next;
- if(head==NULL)
- {
- return;
- }
- if(n==1)
- {
- head=head->next;
- head->prev=NULL;
- free(temp);
- }
- if(del->next==NULL)
- {
- tail=temp;
- free(del);
- return;
- }
- n=n-2;
- while(n!=0 && temp->next!=NULL)
- {
- temp=temp->next;
- n--;
- }
- del=temp->next;
- temp->next=del->next;
- del->next->prev=temp;
- free(del);
- }
- void deletevalue(int x)
- {
- data *del=NULL;
- data *temp=head;
- if(head==NULL)
- {
- return;
- }
- if(head->a==x)
- {
- head=head->next;
- head->prev=NULL;
- free(temp);
- return;
- }
- while(temp->next->a!=x)
- {
- temp=temp->next;
- if(temp->next==NULL)
- {
- return;
- }
- }
- del=temp->next;
- temp->next=del->next;
- del->next->prev=temp;
- free(del);
- }
- int sum()
- {
- int s=0;
- data *temp=head;
- while(temp!=NULL)
- {
- s=s+temp->a;
- temp=temp->next;
- }
- return s;
- }
- double avg()
- {
- int count=0;
- double s=0;
- data *temp=head;
- while(temp!=NULL)
- {
- s=s+temp->a;
- count++;
- temp=temp->next;
- }
- if(count==0)
- {
- return 0;
- }
- return s/count;
- }
- void print()
- {
- data *temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->a);
- temp=temp->next;
- }
- printf("\n");
- }
- int main()
- {
- printf("entry the first--");
- insert_at_first(3);
- insert_at_first(4);
- insert_at_first(6);
- print();
- printf("entry the end--");
- insert_at_end(7);
- insert_at_end(8);
- insert_at_end(9);
- print();
- InsertAtN(3,3);
- print();
- deletenode(3);
- print();
- printf("the sum is %d \n",sum());
- printf("the avarage is %.2lf\n",avg());
- deletevalue(8);
- print();
- deletevalue(9);
- print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement