DS1_2

//BCA Support //www.bcasupport.xyz #include <iostream.h> #include <conio.h> #include <alloc.h> //ctype.h for toupper() #include <ctype.h> //stdlib.h for exit() #include <stdlib.h> struct list{ int data; struct list *next,*prev; }*start,*list,*temp,*sortvar,*SplitMerge; char ch; int a,b,opt,pos,flag=0; int input(); int display(); int insert(); int insbeg(); int insbtw(); int insend(); int del(); int delbeg(); int delbtw(); int delend(); int count(); int sort(); int split(); int merge(); int main() { while(opt!=9) { clrscr(); cout<<"\nLINKED LIST FUNCTIONS -\n"; cout<<"\n1. Create\n2. Display (Traverse)\n3. Insert"; cout<<"\n4. Delete\n5. Display no. of nodes\n6. Sort"; cout<<"\n7. Split\n8. Merge\n9. Exit\nEnter choice : "; cin>>opt; switch(opt) { case 1: input(); break; case 2: display(); break; case 3: insert(); break; case 4: del(); break; case 5: count(); break; case 6: sort(); break; case 7: split(); break; case 8: merge(); break; default: exit(0); } list=start; } getch(); return 0; } //Input function START int input() { if(start!=NULL) { cout<<"\nList already created!"; getch(); return 0; } start=(struct list *)malloc(sizeof(struct list)); list=start; start->prev=NULL; while(1) { clrscr(); cout<<"\nINPUT ->\n\nEnter a number : "; cin>>list->data; cout<<"Enter more data? (Y/N) : "; cin>>ch; if(toupper(ch)=='Y') { list->next=(struct list *)malloc(sizeof(struct list)); list->next->prev=list; list=list->next; } else { list->next=NULL; break; } } return 0; }//Input function END //Display function START int display() { if(start==NULL) { cout<<"\nNo list exists! Create list to display."; getch(); return 0; } if(flag==0) while(list!=NULL) { clrscr(); gotoxy(1,1); cout<<"\nDISPLAY ->\n\nList data :"; cout<<endl<<list->data; gotoxy(25,24); cout<<"prev <p> || next <n> || exit <e>\n\t\t\tChoice : "; cin>>ch; if((toupper(ch)=='N')) list=list->next; else if((toupper(ch)=='P')) list=list->prev; else break; } else { cout<<"\nWhich list to display? (1/2) : "; cin>>opt; switch(opt) { case 1: list=start; break; case 2: list=SplitMerge; break; default: cout<<"Invalid input"; getch(); return 0; } while(list!=NULL) { clrscr(); gotoxy(1,1); cout<<"\nDISPLAY ->\n\nList data :"; cout<<endl<<list->data; gotoxy(25,24); cout<<"prev <p> || next <n> || exit <e>\n\t\t\tChoice : "; cin>>ch; if((toupper(ch)=='N')) list=list->next; else if((toupper(ch)=='P')) list=list->prev; else break; } } return 0; }//Display function END //Insert function START int insert() { if(start==NULL) { cout<<"\nNo list exists! Create list to insert."; getch(); return 0; } clrscr(); cout<<"\nINSERT ->\n\n1. At beginning\n2. In between\n3. At end"; cout<<"\nEnter choice : "; cin>>opt; switch(opt) { case 1: insbeg(); break; case 2: insbtw(); break; case 3: insend(); break; default: cout<<"Invalid input"; } getch(); return 0; } //Insert at beginning START int insbeg() { temp=(struct list *)malloc(sizeof(struct list)); cout<<"\nInsert at Beginning -\nEnter data to insert : "; cin>>temp->data; temp->next=start; start->prev=temp; start=temp; start->prev=NULL; cout<<"\nInsertion successful!"; return 0; }//Insert at beginning END //Insert in between START int insbtw() { temp=(struct list *)malloc(sizeof(struct list)); cout<<"\nInsert in Between -\nEnter data to insert : "; cin>>temp->data; cout<<"Enter insert position : "; cin>>pos; while((--pos)>1) list=list->next; temp->next=list->next; temp->prev=list; list->next->prev=temp; list->next=temp; cout<<"\nInsertion successful!"; return 0; }//Insert in between END //Insert at end START int insend() { temp=(struct list *)malloc(sizeof(struct list)); cout<<"\nInsert at End -\nEnter data to insert : "; cin>>temp->data; while(list->next!=NULL) list=list->next; list->next=temp; temp->prev=list; temp->next=NULL; cout<<"\nInsertion successful!"; return 0; }//Insert at end END //Insert function END //Delete function START int del() { if(start==NULL) { cout<<"\nNo list exists! Create list to delete."; getch(); return 0; } clrscr(); cout<<"\nDELETE ->\n\n1. First Node\n2. Node in between\n3. Ending node"; cout<<"\nEnter choice : "; cin>>opt; switch(opt) { case 1: delbeg(); break; case 2: delbtw(); break; case 3: delend(); break; default: cout<<"Invalid input"; } getch(); return 0; } //Delete beginning node START int delbeg() { start=list->next; start->prev=NULL; free(list); cout<<"\nDeletion successful!"; return 0; }//Delete beginning node END //Delete node in between START int delbtw() { cout<<"\nEnter delete position : "; cin>>pos; while((--pos)>1) list=list->next; temp=list->next; temp->next->prev=list; list->next=temp->next; free(temp); cout<<"\nDeletion successful!"; return 0; }//Delete node in between START //Delete ending node START int delend() { while(list->next->next!=NULL) list=list->next; temp=list->next; free(temp); list->next=NULL; cout<<"\nDeletion successful!"; return 0; }//Delete ending node END //Delete function END //Count function START int count() { if(start==NULL) { cout<<"\nNo list exists! Create list to count."; getch(); return 0; } clrscr(); int count=0,even=0,odd=0; while(list!=NULL) { if(list->data%2==0) even++; else odd++; list=list->next; count++; } cout<<endl<<"NO. OF NODES ->\n"; cout<<endl<<"Nodes = "<<count<<endl; cout<<"\nEven nodes : "<<even; cout<<"\nOdd nodes : "<<odd; getch(); return 0; }//Count function END //Sort function START int sort() { if(start==NULL) { cout<<"\nNo list exists! Create list to sort."; getch(); return 0; } clrscr(); cout<<endl<<"SORT LIST ->\n"; cout<<"\n1. Ascending\n2. Descending\nEnter choice : "; cin>>opt; sortvar=(struct list *)malloc(sizeof(struct list *)); if(opt==1) { while(list!=NULL) { temp=list->next; while(temp!=NULL) { if((list->data)>(temp->data)) { sortvar->data=temp->data; temp->data=list->data; list->data=sortvar->data; } temp=temp->next; } list=list->next; } cout<<"\nSorting successful!"; } else if(opt==2) { while(list!=NULL) { temp=list->next; while(temp!=NULL) { if((list->data)<(temp->data)) { sortvar->data=temp->data; temp->data=list->data; list->data=sortvar->data; } temp=temp->next; } list=list->next; } cout<<"\nSorting successful!"; } else { cout<<"\nInvalid input."; getch(); } getch(); return 0; }//Sort function END //Split function START int split() { if(start==NULL) { cout<<"\nNo list exists! Create list to split."; getch(); return 0; } clrscr(); cout<<"\nSPLIT LIST ->\n"; cout<<"\nEnter split position : "; cin>>pos; while(pos>1) { list=list->next; pos--; } SplitMerge=list->next; SplitMerge->prev=NULL; list->next=NULL; list=start; cout<<"\nSplitting successful!\n"; cout<<"\nLinked list 1 ->"; while(list->next!=NULL) { cout<<endl<<list->data; list=list->next; } cout<<endl<<list->data; list=SplitMerge; cout<<"\n\nLinked list 2 ->"; while(list->next!=NULL) { cout<<endl<<list->data; list=list->next; } cout<<endl<<list->data; flag=1; getch(); return 0; }//Split function END //Merge function START int merge() { if(start==NULL) { cout<<"\nNo list exists! Create list to merge."; getch(); return 0; } if(SplitMerge->prev!=NULL) { cout<<"\nList not splitted! Split list to merge."; getch(); return 0; } while(list->next!=NULL) list=list->next; list->next=SplitMerge; SplitMerge->prev=list; clrscr(); cout<<"\nMERGE LIST ->\n"; cout<<"\nMerging successful!"; flag=0; getch(); return 0; }//Merge function END

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.