Sunday 27 January 2013

Write a program to split one linked list into two lists


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
               int data;
               struct node *link;
}*start=NULL,*start2;

               void create_list()
               {
               char ch;
               do
               {
                               if(start==NULL)
                               {
                               struct node *temp;
                                              temp=new node;
                                              cout<<"enter data:--";
                                              cin>>temp->data;
                                              temp->link=NULL;
                                              start=temp;
                               }
                               else
                               {
                                              struct node *temp,*ptr;
                                              temp=new node;
                                              cout<<"enter data:-";
                                              cin>>temp->data;
                                              temp->link=NULL;
                                              ptr=start;
                                              while(ptr->link!=NULL)
                                              {
                                              ptr=ptr->link;
                                              }
                                              ptr->link=temp;

                               }
                               cout<<"do u want to continue(y/n):-";
                               cin>>ch;
               }while(ch=='y');
               }

               void spilt()
               {
                               struct node *ptr;
                               int count=0,temp=0;
                               if(start==NULL)
                               {
                                              cout<<" No linked list";
                               }
                               else if(start->link==NULL)
                               {
                                              cout<<"Can't split node because only one node in linked list";
                               }
                               else
                               {
                                              ptr=start;
                                              while(ptr!=NULL)
                                              {
                                                             count++;
                                                             ptr=ptr->link;
                                              }//end of while
                               temp=count/2;
                               int count1=1;
                               ptr=start;
                                              while(ptr!=NULL && count1!=temp)
                                              {
                                                             ptr=ptr->link;
                                                             count1++;
                                              }//end of while
                               start2=ptr->link;
                               ptr->link=NULL;
                                  }
                 }//end of function


                void full_display()
                {
                               struct node *ptr;
                               ptr=start;
                               cout<<"linked list is="<<endl;
                               while(ptr!=NULL)
                               {
                                              cout<<"|"<<ptr->data<<"|->";
                                              ptr=ptr->link;
                               }//end of while loop
               }

               void first_display()
               {
                               struct node *ptr;
                               ptr=start;
                               cout<<"the first linked list is"-"<<endl;
                               while(ptr!=NULL)
                               {
                                              cout<<" "<<ptr->data<<" ";
                                              ptr=ptr->link;
                               }//end of while loop
               }//end of function

               void second_display()
               {
                               struct node *ptr1;
                               ptr1=start2;
                               cout<<"second linked list ="<<endl;
                               while(ptr1!=NULL)
                               {
                                              cout<<" "<<ptr1->data<<" ";
                                              ptr1=ptr1->link;
                               }//end of while loop
               }//end of function

void main()
{
               clrscr();
               int choice;
               int n,loc,c=0;
               cout<<" press 1. create linked list"<<endl;
               cout<<" press 2. display linked list"<<endl;
               cout<<" press 3. the first linked list is"<<endl;
               cout<<" press 4. the second linked list is"<<endl;
               cout<<" press 5. spilt a linked list"<<endl;
               cout<<" press 6. EXIT"<<endl;
               while(1)
               {
                               cout<<" enter your choice:-";
                               cin>>choice;
                               switch(choice)
                               {
               case 1:
                               create_list();
                               break;

               case 2:
                               full_display();
                               break;
               case 3:
                               first_display();
                               break;
               case 4:
                               second_display();
                               break;
               case 5:
                               cout<<"A linked list divided into two part"<<endl;
                               spilt();
                               break;
               case 6:
                               exit(0);
                               break;
               default:
                               cout<<"try again";
                               break;
               }//END OF SWITCH CASE

               getch();
               }//END OF WHILE
}//END OF MAIN

No comments:

Post a Comment