Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int a;
- struct node*next,*prev;
- } node;
- node*head=NULL,*tail=NULL;
- int main()
- {
- int choice;
- while(1)
- {
- printf("\t\t\t\t\n\nDoubly linked list\n\n");
- printf("\t\t\t\t\n1. Add First\n");
- printf("\t\t\t\t\n2. Add Any\n");
- printf("\t\t\t\t\n3. Delete Elements\n");
- printf("\t\t\t\t\n4. Display\n");
- printf("\t\t\t\t\n5. Delete Position\n");
- printf("\t\t\t\t\n6. Search Data\n");
- printf("\t\t\t\t\n7. Add end\n\n");
- printf("Enter your choice:");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1 :
- Add_first();
- break;
- case 2:
- Add_any();
- break;
- case 3 :
- delete_data();
- break;
- case 4:
- display_first();
- break;
- case 5:
- Delete_Position();
- break;
- case 6:
- Search_Data();
- break;
- case 7:
- Add_End();
- break;
- dafault:
- printf("Invalid choice");
- }
- }
- return 0;}
- void Add_first()
- {
- node *N=(node*)malloc(sizeof(node));
- printf("Enter the number:");
- scanf(" %d",&N->a);
- N->next=NULL;
- N->prev=NULL;
- if(head==NULL)
- {
- head=N;
- tail=N;
- }
- else
- {
- head->prev=N;
- N->next=head;
- head=N;
- }
- }
- void Add_any()
- {
- int m;
- node *list=head;
- node *N=(node*)malloc(sizeof(node));
- printf("Enter the number:");
- scanf(" %d",&N->a);
- N->next=NULL;
- N->prev=NULL;
- printf("Enter Position: ");
- scanf(" %d",&m);
- if(m==1 )
- {
- if(head==NULL)
- {
- head=N;
- tail=N;
- }
- else
- {
- head->prev=N;
- N->next=head;
- head=N;
- }
- }
- else
- {
- m=m-2;
- while(m!=0 && list->next!=NULL)
- {
- list=list->next;
- m--;
- }
- if(list->next==NULL)
- {
- N->prev=tail;
- tail->next=N;
- tail=N;
- }
- else
- {
- list->next->prev=N;
- N->next=list->next;
- N->prev=list;
- list->next=N;
- }
- }
- }
- void Add_End()
- {
- node *N=(node*)malloc(sizeof(node));
- printf("enter the number:");
- scanf(" %d",&N->a);
- N->next=NULL;
- N->prev=NULL;
- node *list=head;
- if(head==NULL)
- {
- head=N;
- tail=N;
- }
- else
- {
- tail->next=N;
- N->prev=tail;
- tail=N;
- }
- }
- void delete_data()
- {
- int d;
- printf("Enter the number:");
- scanf(" %d",&d);
- node*list=head;
- if(head==NULL)
- {
- printf("No Data!!\n\n");
- }
- else if(list->a==d)
- {
- if(list->next==NULL)
- {
- head=NULL;
- tail=NULL;
- printf("Data Deleted\n\n");
- free(list);
- }
- else
- {
- list->next->prev=NULL;
- head=list->next;
- printf("Data Deleted\n\n");
- free(list);
- }
- }
- else
- {
- node*temp;
- while(list->a!=d && list->next!=NULL)
- {
- list=list->next;
- if(list==NULL)
- {
- printf("No Data Found!!\n\n");
- break;
- }
- }
- if(list->a==d && list->next==NULL)
- {
- list=tail;
- temp=list;
- temp->prev->next=temp->next;
- tail=temp->prev;
- printf("Data Deleted\n\n");
- free(temp);
- }
- else if(list->a==d && list->next!=NULL)
- {
- temp=list;
- temp->prev->next=temp->next;
- temp->next->prev=temp->prev;
- temp->next=NULL;
- temp->prev=NULL;
- printf("Data Deleted\n\n");
- free(temp);
- }
- }
- }
- void Delete_Position()
- {
- int loc;
- node *list=head,*temp;
- printf("Enter your desired Position:");
- scanf(" %d",&loc);
- if(head==NULL)
- {
- printf("Invalid Position to delete!!\n\n");
- }
- else if(loc==1)
- {
- if(list->next==NULL)
- {
- printf("Data Deleted\n\n");
- free(list);
- }
- else
- {
- head=list->next;
- head->prev=NULL;
- printf("Data Deleted\n\n");
- free(list);
- }
- }
- else
- {
- loc=loc-2;
- while(loc!=0 && list->next!=NULL)
- {
- list=list->next;
- loc--;
- if(list==NULL)
- {
- printf("No Data Found!!\n\n");
- break;
- }
- }
- if(list->next->next==NULL && loc==0)
- {
- temp=tail;
- tail=tail->prev;
- tail->next=NULL;
- printf("Data Deleted\n\n");
- free(temp);
- }
- else if(list->next->next!=NULL && loc==0)
- {
- temp=list->next;
- temp->next->prev=list;
- list->next=temp->next;
- printf("Data Deleted\n\n");
- free(temp);
- }
- }
- }
- void Search_Data()
- {
- node *list=head;
- int s;
- printf("Enter Your number:");
- scanf("%d",&s);
- if(list->a==s)
- {
- printf(" Data Found: %d\n\n",list->a);
- }
- else
- {
- while(list->a!=s)
- {
- list=list->next;
- if(list==NULL)
- {
- printf("No Data!!\n\n");
- break;
- }
- }
- printf("%d\n\n",list->a);
- }
- }
- void display_first()
- {
- node*list=head;
- if(head==NULL)
- {
- printf("No Data to show\n");
- }
- else
- {
- while(list!=NULL)
- {
- printf("%d\n",list->a);
- list=list->next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment