Write a function ListSplit that will split a circular list containing an even number of nodes, say 2k, into two circular lists each of which contains k nodes. The function should have three parameters and should work so that the function call

ListSplit(list,sub1,sub2);

will create new lists pointed to by parameters sub1, and sub2 from the list initially referenced by parameter list. After ListSplit has executed, the first parameter should be 0/NULL, i.e., new nodes are not created, but are redistributed evenly between the second and third parameters. It doesn't matter how you divide the nodes between the two lists. Note that you should make no assumptions about the initial values of first and second in the body of ListSplit..

any help/guideline for the subject code

## All 6 Replies

If you research c++ circular list you'll find there are quite a few articles/posts/examples available.

#include<iostream>
#include<conio.h>
using namespace std;

struct node{
int data;
node * next,*previous;
};

void main(){
int k;
temp1=new node;
temp1->data=-1;
temp1->next=NULL;
temp1->previous=NULL;
lastnodeptr=temp1;

cout<<"enter an int for the number of nodes to be 2 multiply by that number\n";
cin>>k;

for(int i=2;i<=2*k;i++){
temp1=new node;
temp1->data=i;
temp1->previous=NULL;
temp1->next=NULL;
lastnodeptr->next=temp1;
temp1->previous=lastnodeptr;
lastnodeptr=temp1;
}

node *tempptr=NULL;
cout<<"the data in nodes is\n";

for(int i=0;i<=2*k;i++){
cout<<tempptr->data<<endl;
tempptr=tempptr->next;
}

for(int i=1;i<=2*k/2;i++){
cout<<list->data<<"   ";
list=list->next;
}
list->previous=NULL;

sub2=list;
for(int i=1;i<=2*k/2;i++){
cout<<sub2->data; // stuck here its not printing the data of //successive nodes
sub2=sub2->next;
}

cout<<endl<<endl<<endl<<sub1->data;

getche();
}

//someone help

your problem appears to be lines 49 & 50. They are truncating list. Remove them and sub2 prints out the right values.

commented: but how can i establish the link of tail to head in the link lists which i;ve got after after dividing the linked list into 2 PARTS +1

that problem will be resolved but ive make the sub lists circular again...if i remove those lines they no longer remain circular..help me

Part of your problem appears to be that your struct actuall creats a doubly-linked list. This needlessly complicates things. By making it a singly linked list with pointers to represent the 2 head nodes and the 2 tail nodes and a couple of temp nodes, your code becomes quite a lot simpler:

#include<iostream>

using namespace std;

struct node
{
int data;
node * next;
};
int main()
{
int k = 1;
cout << "enter an int for the number of nodes to be multiplied by 2\n";
cin >> k;
node *head1 = new node, *tail2 = new node, *tempnode = new node;
node *head2 = new node, *tail1 = new node;
tempnode->next = tail2;
int limit = k * 2;
tail2->data = limit;
node *temp = new node;
int i = 2;
for (; i < limit-1; i++)
{
tempnode->data = i;
temp = new node;
tempnode->next = temp;
temp->next = tail2;
tempnode = temp;
}
temp->data = i;
for (i = 1; i < k; i++)
{
temp = temp->next;
}
tail1 = temp;
cout << "First list values\n";
{
cout << temp->data << '\n';
temp = temp->next;
}
cout << temp->data << '\n' << "Next value past the end\n" << temp->next->data;
cout << "\n\nSecond list values\n";
{
cout << temp->data << '\n';
temp = temp->next;
}
cout << temp->data << '\n' << "Next value past the end\n" << temp->next->data;
cin.get();
return 0;
}

thanks mate for your help...im extremely grateful...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.