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*tempe;
- data*tempf;
- laboni->a=x;
- 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;
- }
- tempe=head;
- for(i=0; i<x-2; i++)
- {
- tempe=tempe->next;
- }
- laboni->next=tempe->next;
- laboni->prev=tempe;
- tempe->next=laboni;
- tempf=laboni->next;
- tempf->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("asssalamualaikum sir,,,\n");
- 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();
- printf("the nth position--");
- InsertAtN(3,3);
- print();
- printf("the delete position--");
- deletenode(3);
- print();
- printf("the sum is %d \n",sum());
- printf("the avarage is %.2lf\n",avg());
- printf("the delete value position--");
- deletevalue(8);
- print();
- printf("thank you sir ,,,,,,,,,alllah hafej");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement