Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to split LinkList by a given threshold value,means if the count of Link List is greater than Threshold value then the Linked List should split from middle,and then recursively if existing 2 linked list count is larger than threshold value then the list will be further split in to 2 lists.
and there will be a display function which will have argument(starting address of all lists).
here is my tried code..

C++
#include<iostream>
 using namespace std;
 struct node
 {
 	int data;
 	struct node *next;
 };
 struct node *insert(struct node*);
 struct node *split(struct node*,int,int);
void disp(struct node []);
 int counter,counter2=0;
struct node *startn=NULL;
 int main()
 { struct node *start=NULL;
    char chh;
 do{
 cout<<"1:"<<"\t"<<"Insert"<<endl;
 cout<<"2:"<<"\t"<<"split"<<endl;
 cout<<"3:"<<"\t"<<"Display"<<endl;
 int ch;
 cin>>ch;
 switch(ch)
 { case 1:
 				start = insert(start);

 				break;
   case 2:

                 int th;
                 cout<<counter;
                cout<<"Enter the threshold value"<<endl;
                cin>>th;

                startn=split(start,th,counter);
                for(int i=0;i<10;i++)
                {
                    cout<<startn<<endl;
                    startn++;
                }
 				break;
 	case 3:

 				disp(startn);

 				break;
 	default:
 				cout<<"Enter proper choice"<<endl;
 }

cout<<endl<<"Do you want to continue"<<endl;
cin>>chh;
 }while(chh=='Y' || chh=='y');
 }

 struct node *insert(struct node *start)
 			{  int n;

 			struct node *new_node=new struct node();
 			cout<<"Enter the data"<<endl;
 				cin>>new_node->data;

 			if(start==NULL)
 				{ new_node->next=NULL;
 							start=new_node;
                            counter=1;
 				}
 			else
 			{ 	new_node->next=start;
 				start=new_node;
 				counter=counter+1;
 			}

cout<<"count is "<<counter;
return(start);
 			}
struct node *split(struct node *start,int th,int counter)
 			{   cout<<endl<<"SPLIT CALLED"<<endl;
 			    int tempval;struct node *temp=NULL;
                     struct node *prev=NULL;
                    struct node *Head1=start;
                     struct node *Head2=NULL;
                if(th<counter)>
                {
                    tempval=counter/2;

                    temp=start;int i=0;
                    Head1=start;
                        startn=Head1;
                    while (i<tempval)>
                    {
                        prev=temp;
                        temp=temp->next;i++;


                    }

                Head2=temp;
                   prev->next=NULL;
                    split(Head1,th,tempval);
                    split(Head2,th,tempval);
                    startn++;
                    counter2++;
cout<<endl<<counter2;
                }


	return(startn);
		}
void display(struct node startn[])
{
    for(int i=counter2;i>0;i++)
    {   cout<<endl<<"count"<<endl;
        struct node *temp=NULL;
        temp=startn;
        while(temp->next!=NULL)
        {
            cout<<temp->data;
            temp=temp->next;
        }
        startn--;
    }


    /*while(counter2!=0)
    { struct node *temp=startn;
        while(temp!=NULL)
        {
            cout<<temp->data;
            temp=temp->next;
        }
        counter2--;
    }
*/
}
Posted
Updated 14-Mar-15 7:24am
v5
Comments
OriginalGriff 14-Mar-15 8:26am    
And?
What does your code do that you didn't expect, or not do that you did?
What help do you need?
Vedant Dave 14-Mar-15 10:18am    
i want to pass array of structure pointer in display function but dont know what is the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900