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 ch;
- while(1)
- {
- printf("\n\nDoubly linked list operations\n\n");
- printf("1.Insert First\n");
- printf("2.Insert Any\n");
- printf("3.Delete Elements\n");
- printf("4.display\n");
- printf("5.Delete Position\n");
- printf("6.Search Data\n");
- printf("7.Insert at end\n");
- printf("8.display_last\n");
- printf("Enter your choice:");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1 :
- Insert_first();
- break;
- case 2:
- insert_any();
- break;
- case 3 :
- delete_data();
- break;
- case 4:
- display_first();
- break;
- case 5:
- Delete_Position();
- break;
- case 6:
- Search_Data();
- break;
- case 7:
- Insert_End();
- break;
- case 8:
- display_last();
- break;
- dafault:
- printf("Invalid choice");
- }
- }
- return 0;}
- void Insert_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 insert_any()
- {
- int p;
- 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",&p);
- if(p==1 )
- {
- if(head==NULL)
- {
- head=N;
- tail=N;
- }
- else
- {
- head->prev=N;
- N->next=head;
- head=N;
- }
- }
- else
- {
- p=p-2;
- while(p!=0 && list->next!=NULL)
- {
- list=list->next;
- p--;
- }
- 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 Insert_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 p;
- node *list=head,*temp;
- printf("Enter Position:");
- scanf(" %d",&p);
- if(head==NULL)
- {
- printf("No Position!!\n\n");
- }
- else if(p==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
- {
- p=p-2;
- while(p!=0 && list->next!=NULL)
- {
- list=list->next;
- p--;
- if(list==NULL)
- {
- printf("No Data Found!!\n\n");
- break;
- }
- }
- if(list->next==NULL)
- {
- printf("Wrong Position!!\n\n");
- }
- else if(list->next->next==NULL && p==0)
- {
- temp=tail;
- tail=tail->prev;
- tail->next=NULL;
- printf("Data Deleted\n\n");
- free(temp);
- }
- else if(list->next->next!=NULL && p==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("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_last()
- {
- node*list=tail;
- if(head==NULL)
- {
- printf("No Data Available!!\n\n");
- }
- else
- {
- while(list!=NULL)
- {
- printf("%d\n\n",list->a);
- list=list->prev;
- }
- }
- }
- void display_first()
- {
- node*list=head;
- if(head==NULL)
- {
- printf("No Data Available\n");
- }
- else
- {
- while(list!=NULL)
- {
- printf("%d\n",list->a);
- list=list->next;
- }
- }
- }
Add Comment
Please, Sign In to add comment