0

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

2
Contributors
6
Replies
34
Views
3 Years
Discussion Span
Last Post by Builder_1
0
#include<iostream>
#include<conio.h>
using namespace std;

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

void main(){
    int k;
    node *temp1,*lastnodeptr,*head;
    temp1=new node;
    temp1->data=-1;
    head=temp1;
    temp1->next=NULL;
    temp1->previous=NULL;
    head=temp1;
    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;
    }
    lastnodeptr->next=head;

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

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

    node *sub1,*sub2,*list=NULL;list=head;sub1=head;
    for(int i=1;i<=2*k/2;i++){
        cout<<list->data<<"   ";
        list=list->next;
    }
    list->next=head;
    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
1

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

Edited by tinstaafl

Votes + Comments
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
0

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

1

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;
head1->next = tempnode;
tempnode->next = tail2;
tail2->next = head1;
head1->data = 1;
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;
temp = head1;
for (i = 1; i < k; i++)
{
    temp = temp->next;
}
tail1 = temp;
head2 = temp->next;
tail1->next = head1;
tail2->next = head2;
temp = head1;
cout << "First list values\n";
while (temp->next->data != head1->data)
{
    cout << temp->data << '\n';
    temp = temp->next;
}
cout << temp->data << '\n' << "Next value past the end\n" << temp->next->data;
temp = head2;
cout << "\n\nSecond list values\n";
while (temp->next->data != head2->data)
{
    cout << temp->data << '\n';
    temp = temp->next;
}
cout << temp->data << '\n' << "Next value past the end\n" << temp->next->data;
cin.get();
return 0;
}    

Edited by tinstaafl

0

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

Edited by Builder_1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.