Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int a;
- char ch;
- struct node*next;
- struct node*previous;
- } node;
- node*head=NULL,*tail=NULL;
- void insert_at_first(int aN, char chN)
- {
- node *N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- N->previous=NULL;
- if(head==NULL)
- {
- head=N;
- return;
- }
- N->next=head;
- head->previous=N;
- head=N;
- }
- void insert_at_end(int aN,char chN)
- {
- node*N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- N->previous=NULL;
- node*list=head;
- if(head==NULL)
- {
- head=N;
- }
- else
- {
- while(list->next!=NULL)
- {
- list=list->next;
- N->previous=list;
- }
- list->next=N;
- N->next=NULL;
- N->previous=list;
- }
- }
- void insert_nth(int n,int aN,char chN)
- {
- node *N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- N->previous=NULL;
- if(n==1)
- {
- N->previous=NULL;
- N->next=head;
- head=N;
- return;
- }
- else
- {
- node*list=head;
- n=n-2;
- while(n!=0&&list->next!=NULL)
- {
- list=list->next;
- n--;
- }
- N->next=list->next;
- if(list->next !=NULL)
- {
- list->next->previous=N;
- }
- list->next=N;
- }
- }
- void create()
- {
- int value;
- char ch;
- while(1)
- {
- printf(" \n\tENTER NUMBER & Character (int,char) (-1 for exit)\n");
- printf("\t");
- scanf("%d",&value);
- if(value==-1)
- {
- display();
- printf("\tNode Create Completed!!");
- MENU();
- }
- scanf(" %c",&ch);
- insert_at_end(value,ch);
- }
- }
- void delete_item(int value)
- {
- node *list = head, *temp=NULL;
- int x = 0;
- while(list!=NULL)
- {
- if(list->a==value)
- {
- if(temp==NULL)
- {
- head = list->next;
- head->previous=NULL;
- }
- else
- temp->next = list->next;
- printf("%d is deleted from list\n", value);
- x = 1;
- free(list);
- break;
- }
- temp = list;
- list = list->next;
- list->previous=temp;
- }
- if(x==0)
- printf("Key not found!\n");
- }
- int search()
- {
- int n;
- printf(" \n\t<<<<<<<<SEARCH VALUE>>>>>>>>\n\n");
- printf("\tENTER ANY VALUE FOR SERACH :");
- scanf("%d",&n);
- node*list=head;
- int x=0;
- while(list!=NULL)
- {
- if(list->a==n)
- {
- printf("\n\t%d is Found\n\n",n);
- x=1;
- break;
- }
- else
- list=list->next;
- }
- if(x==0)
- {
- printf("\n\t%d is Not Found\n\n",n);
- }
- MENU();
- }
- void addnode()
- {
- int af,y;
- char cf;
- printf("\n\n\n");
- printf("\t1.Insert At First.\n");
- printf("\t2.Insert At LAST.\n");
- printf("\t3.Insert At Nth Position.\n");
- printf("\t4.GO TO MENU\n");
- printf("\n\tChoose Any Option (1-4):");
- int x;
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- printf("\n\t<<<<<<<<Insert At FIRST(int,char)>>>>>>>>\n");
- printf("\t");
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_at_first(af,cf);
- display();
- addnode();
- }
- case 2:
- {
- printf("\n\t<<<<<<<<Insert At END(int,char)>>>>>>>>\n");
- printf("\t");
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_at_end(af,cf);
- display();
- addnode();
- }
- case 3:
- {
- printf("\n\t<<<<<<<<Insert At Nth(pos,int,char)>>>>>>>>\n");
- printf("\t");
- scanf("%d",&y);
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_nth(y,af,cf);
- display();
- addnode();
- }
- case 4:
- {
- MENU();
- break;
- }
- default:
- {
- printf("\tCHOOSE A CORRECT OPTION\n");
- addnode();
- }
- }
- }
- void delete()
- {
- printf("\n\t<<<<<<<<<DELETE NODE>>>>>>>>>\n");
- printf("\t1.Delete By Position \n");
- printf("\t2.Delete By Value \n");
- printf("\t3.GO TO MENU \n\n");
- printf("\tChoose Any Option (1-3):");
- int x,s,l;
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- printf(" \n\t<<<<<<<<DELETE BY POSITION>>>>>>>>\n");
- printf("\t");
- printf("ENTER POSITION:");
- scanf("%d",&s);
- delete_pos(s);
- delete();
- }
- case 2:
- {
- printf(" \n\t<<<<<<<<DELETE BY VALUE>>>>>>>>\n");
- printf("\t");
- printf("ENTER VALUE:");
- scanf("%d",&l);
- delete_item(l);
- delete();
- }
- case 3:
- {
- MENU();
- break;
- }
- default:
- {
- printf("\tCHOOSE A CORRECT OPTION\n");
- delete();
- }
- }
- }
- void delete_pos(int pos)
- {
- node *list=head,*temp=NULL,*tail;
- if(pos==1)
- {
- if(list->next==NULL)
- {
- printf("DATA DELETED\n\n");
- free(list);
- }
- else
- {
- head=list->next;
- head->previous=NULL;
- printf("DATA DELETED\n\n");
- free(list);
- }
- }
- else
- {
- pos=pos-2;
- while(pos!=0 && list->next!=NULL)
- {
- list=list->next;
- pos--;
- if(list==NULL)
- {
- printf("NO DATA FOUND!!\n\n");
- break;
- }
- }
- if(list->next==NULL)
- {
- printf("You Choose Wrong Position!!\n\n");
- }
- else if(list==NULL && pos==0)
- {
- temp=tail;
- tail=temp->previous;
- tail->next=NULL;
- printf("DATA DELETED\n\n");
- free(temp);
- }
- else if(list!=NULL && pos==0)
- {
- temp=list->next;
- temp->next->previous=list;
- list->next=temp->next;
- printf("DATA DELETED\n\n");
- free(temp);
- }
- }
- }
- void display()
- {
- node *list=head;
- if(head==NULL)
- {
- printf("\n\n\tNode is empty!!\n\n");
- printf("\tENTER DATA PLEASE!!\n");
- MENU();
- }
- while(list !=NULL)
- {
- printf("\n\n\tA= %d ,C= %c\n",list->a,list->ch);
- list=list->next;
- }
- }
- void MENU()
- {
- printf("\n\n\t<<<<<<<<<< WELCOME TO DOUBLY LINK LIST >>>>>>>>>>>>>\n\n");
- printf("\t1.Create Nodes\n");
- printf("\t2.ADD Nodes\n");
- printf("\t3.Search Nodes\n");
- printf("\t4.DELETE Nodes\n");
- printf("\t5.Display\n");
- printf("\t6.EXIT \n\n\n");
- printf("\tChoose Any Option (1-5):");
- int x;
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- create();
- break;
- }
- case 2:
- {
- addnode();
- break;
- }
- case 3:
- {
- search();
- break;
- }
- case 4:
- {
- delete();
- break;
- }
- case 5:
- {
- dis();
- break;
- }
- case 6:
- {
- break;
- }
- default:
- {
- printf("CHOOSE A CORRECT OPTION\n");
- MENU();
- }
- }
- }
- void dis()
- {
- node *list=head;
- if(head==NULL)
- {
- printf("\n\n\tNode is empty!!\n\n");
- printf("\tENTER DATA PLEASE!!\n");
- MENU();
- }
- while(list !=NULL)
- {
- printf("\n\n\tA= %d ,C= %c\n",list->a,list->ch);
- list=list->next;
- }
- MENU();
- }
- int main()
- {
- head=NULL;
- MENU();
- }
Advertisement
Add Comment
Please, Sign In to add comment