Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- char a[15];
- char ch[30];
- struct node *next, *prev;
- }node;
- node *head=NULL, *tail=NULL;
- void display_last_to_first()
- {
- node *list=tail;
- if(head==NULL)
- {
- printf("No DATA Available!!\n\n");
- }
- else
- {
- while(list!=NULL)
- {
- printf("Name: %s\n", list->ch);
- printf("Roll: %s\n", list->a);
- list=list->prev;
- }
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void display_first_to_last()
- {
- node*list=head;
- if(head==NULL)
- {
- printf("There's no data available!!\n");
- }
- else
- {
- while(list!=NULL)
- {
- printf("Name: %s\n", list->ch);
- printf("Roll: %s\n", list->a);
- list=list->next;
- }
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void display()
- {
- printf("1. Display first to last\n");
- printf("2. Display last to first\n");
- printf("Choose an option:\n");
- int n;
- scanf("%d", &n);
- switch(n)
- {
- case 1:
- {
- display_first_to_last();
- break;
- }
- case 2:
- {
- display_last_to_first();
- break;
- }
- default:
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void search_by_pos()
- {
- int n;
- printf("Enter the Position:\n");
- scanf("%d", &n);
- node *list=head;
- if(n==1)
- {
- printf("Name: %s\n", list->ch);
- printf("Roll: %s\n\n", list->a);
- }
- else
- {
- n=n-2;
- while(n!=0 && list->next!=NULL)
- {
- list=list->next;
- n--;
- }
- if(list->next==NULL)
- {
- printf("There's No Information!!\n");
- }
- else
- {
- printf("Name: %s\n", list->next->ch);
- printf("Roll: %s\n", list->next->a);
- }
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int y;
- scanf("%d",&y);
- switch(y)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void search_by_value()
- {
- int d;
- printf("Enter the Roll:\n");
- scanf("%d", &d);
- node *list=head;
- if(list->a==d)
- {
- printf("Name: %s\n", list->ch);
- printf("Roll: %s\n\n", list->a);
- }
- else
- {
- while(list->a!=d)
- {
- list=list->next;
- if(list==NULL)
- {
- printf("There's No Information!!\n");
- break;
- }
- }
- printf("Name: %s\n", list->ch);
- printf("Roll: %s\n", list->a);
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void search()
- {
- printf("1. Search by Value\n");
- printf("2. Search by Position\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- search_by_value();
- break;
- }
- case 2:
- {
- search_by_pos();
- break;
- }
- case 3:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void delete_by_pos()
- {
- int n;
- node *list=head, *temp;
- printf("Enter the Position:\n");
- scanf(" %d", &n);
- if(head==NULL)
- {
- printf("There's No Information!!\n");
- }
- else if(n==1)
- {
- if(list->next==NULL)
- {
- printf("The data has been deleted!!\n");
- free(list);
- }
- else
- {
- head=list->next;
- head->prev=NULL;
- printf("The data has been deleted!!\n");
- free(list);
- }
- }
- else
- {
- n=n-2;
- while(n!=0 && list->next!=NULL)
- {
- list=list->next;
- n--;
- if(list==NULL)
- {
- printf("There's no data found!!\n");
- break;
- }
- }
- if(list->next==NULL)
- {
- printf("You've Chosen the Wrong Position!!\n");
- }
- else if(list->next->next==NULL && n==0)
- {
- temp=tail;
- tail=tail->prev;
- tail->next=NULL;
- printf("The data has been deleted!!\n");
- free(temp);
- }
- else if(list->next->next!=NULL && n==0)
- {
- temp=list->next;
- temp->next->prev=list;
- list->next=temp->next;
- printf("The data has been deleted!!\n");
- free(temp);
- }
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void delete_the_value()
- {
- int n;
- printf("Enter the Roll:\n");
- scanf(" %d", &n);
- node*list=head;
- if(head==NULL)
- {
- printf("There's No Information!!\n");
- }
- else if(list->a==n)
- {
- if(list->next==NULL)
- {
- head=NULL;
- tail=NULL;
- printf("The data has been deleted!!\n");
- free(list);
- }
- else
- {
- list->next->prev=NULL;
- head=list->next;
- printf("The data has been deleted!!\n");
- free(list);
- }
- }
- else
- {
- node*temp;
- while(list->a!=n && list->next!=NULL)
- {
- list=list->next;
- if(list==NULL)
- {
- printf("There's no data found!!\n");
- break;
- }
- }
- if(list->a==n && list->next==NULL)
- {
- list=tail;
- temp=list;
- temp->prev->next=temp->next;
- tail=temp->prev;
- printf("The data has been deleted!!\n");
- free(temp);
- }
- else if(list->a==n && list->next!=NULL)
- {
- temp=list;
- temp->prev->next=temp->next;
- temp->next->prev=temp->prev;
- temp->next=NULL;
- temp->prev=NULL;
- printf("The data has been deleted!!\n");
- free(temp);
- }
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void Delete()
- {
- printf("1. Delete by value\n");
- printf("2. Delete by position\n");
- printf("Choose a option:");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- delete_the_value();
- break;
- }
- case 2:
- {
- delete_by_pos();
- break;
- }
- case 3:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void insert_at_pos()
- {
- int n;
- node *list=head;
- node *N=(node*)malloc(sizeof(node));
- printf("Enter the Name:\n");
- scanf(" %[^\n]", N->ch);
- printf("Enter the Roll:\n");
- scanf(" %[^\n]", N->a);
- N->next=NULL;
- N->prev=NULL;
- printf("Enter the position:\n");
- scanf(" %d", &n);
- if(n==1)
- {
- if(head==NULL)
- {
- head=N;
- tail=N;
- }
- else
- {
- head->prev=N;
- N->next=head;
- head=N;
- }
- }
- else
- {
- n=n-2;
- while(n!=0 && list->next!=NULL)
- {
- list=list->next;
- n--;
- }
- 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;
- }
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void insert_end()
- {
- node *N=(node*)malloc(sizeof(node));
- printf("Enter the Name:\n");
- scanf(" %[^\n]", N->ch);
- printf("Enter the Roll:\n");
- scanf(" %[^\n]", N->a);
- N->next=NULL;
- N->prev=NULL;
- node *list=head;
- if(head==NULL && tail==NULL)
- {
- head=N;
- tail=N;
- }
- else
- {
- tail->next=N;
- N->prev=tail;
- tail=N;
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void insert_first()
- {
- node *N=(node*)malloc(sizeof(node));
- printf("Enter the Name:\n");
- scanf(" %[^\n]", N->ch);
- printf("Enter the Roll:\n");
- scanf(" %[^\n]", N->a);
- N->next=NULL;
- N->prev=NULL;
- if(head==NULL && tail==NULL)
- {
- head=N;
- tail=N;
- }
- else
- {
- head->prev=N;
- N->next=head;
- head=N;
- }
- printf("1. Menu\n");
- printf("2. Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void add()
- {
- printf("1.Add at frist\n");
- printf("2.Add at end\n");
- printf("3.Add by position\n");
- printf("4.Exit\n");
- printf("Choose an option:\n");
- int x;
- scanf("%d", &x);
- switch(x)
- {
- case 1:
- {
- insert_first();
- break;
- }
- case 2:
- {
- insert_end();
- break;
- }
- case 3:
- {
- insert_at_pos();
- break;
- }
- case 4:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- void menu()
- {
- printf("1.Add\n");
- printf("2.Delete\n");
- printf("3.Search\n");
- printf("4.Display\n");
- printf("5.Exit\n");
- printf("Choose an option:\n");
- int y;
- scanf("%d", &y);
- switch(y)
- {
- case 1:
- {
- add();
- break;
- }
- case 2:
- {
- Delete();
- break;
- }
- case 3:
- {
- search();
- break;
- }
- case 4:
- {
- display();
- break;
- }
- case 5:
- {
- exit(0);
- break;
- }
- default :
- {
- printf("You've chosen a wrong option!! Choose the correct one:\n");
- menu();
- break;
- }
- }
- }
- int main()
- {
- printf("..........!!Doubly Link list!!........\n");
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement