Saturday, 19 November 2016

Split singly linked list into two



  
This Function split singly linked list in to two linked list In this program some data store in linked list. Function used two parameters one is break point and other is 2nd linked list which pass as reference and break point is 100.
 Function
void linkedlist::split(int val ,linkedlist &l)
 {      Node *temp=first;
       if (isEmpty())
{
cout <<"Have no node "<<endl;
}else if (first->next==NULL && first->data==val)
{   cout <<"threr is only one node "<<endl;    }else
{
    while (temp->next!=NULL)
{
           if (temp->data!=val)
  {
              temp=temp->next;
}else
{  
                  break;
 }
     }
    if (temp->next==NULL && temp->data==val)
  {  
        cout <<"this is last node of link list "<<endl;  
}else if(temp->next==NULL && temp->data!=val)
 {
       cout <<"vlaue not found "<<endl;       }else
   {
        l.first=temp->next;    temp->next=NULL; }
   }
     }
 Main
 linkedlist l1,l2;
l1.insertfirst(24);
l1.insertfirst(12);
l1.insertfirst(36);
l1.insertfirst(41);
l1.insertfirst(45);
l1.insertfirst(50);
l1.insertfirst(60);
l1.insertfirst(100);
l1.insertfirst(20);
cout <<"Before split l1 is"<<endl;
l1.print();
cout<<"Split"<<endl;
l1.split(100,l2);
cout <<"L2 -----------------------------"<<endl; l2.print(); cout<<"l1-------------------------------"<<endl;  l1.print();  

No comments: