Splitting linked list

Create a function to split 2 singly Linked Lists according to following rules.
Split 1 linked list into 2 linked lists with alternate nodes.

Input :
List : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
Output :
List 1 : 1 -> 3 -> 5 -> 7 --> 9
List 2 : 2 -> 4 -> 6 --> 8 -> 10

Note: There is no use of Even and Odd (Data), Alternate nodes mean First linked list will have nodes starting from head with skipping next node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
struct abc
{
int data;
abc *next;
};
abc *head1 = NULL;
abc *temp1 = head1;
void showdata()
{
abc *temp = head1;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
void makenodesONE()
{

abc *newnode = new abc;
cout << "Enter the data in node: " << endl;
cin >> newnode->data;
newnode->next = NULL;
if (head1 == NULL)
{
head1 = newnode;
}
else
{
abc *temp1 = head1;
while (temp1->next != NULL)
{
temp1 = temp1->next;
}
temp1->next = newnode;
}
}
void split()
{
while (temp1->next != NULL)
{
cout << temp1->data << "->";
temp1 = temp1->next->next;
}
cout << temp1->data << endl;
}
int main()
{
int x;
cout << " 1ST LINKED LIST:" << endl;
cout << "How many nodes you want to make:" << endl;
cin >> x;
for (int i = 0; i < x; i++)
{
makenodesONE();
}
cout << endl;
split();
system("pause");
}

The problem is that the temp1 will become null for either odd number of linked list nodes or even. What changes should I make so that it prints correct linked list in both cases? Thanks
THIS FUNCTION WILL ONLY WORK FOR EVEN NUMBER OF NODES! WHAT TO DO?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void split()
{
	abc *temp1 = head1;
	abc *temp2 = head1;
	while (temp1->next->next!= NULL)
	{
		cout << temp1->data << "->";
		temp1 = temp1->next->next;
	}
	cout << temp1->data << "->";

	cout << "LINKED LIST 2" << endl;
	temp2 = temp2->next;

	while (temp2->next != NULL)
	{
		cout << temp2->data << "->";
		temp2 = temp2->next->next;
	}
	cout << temp2->data ;
	
}
You should be able to traverse both odd and even elements by

while (temp->next != NULL && temp->next->next != NULL )
etc.

For one sequence start with temp=head and for the other sequence start with temp=head->next.

You aren't strictly splitting anything; you are just outputting alternate elements.
Please PLEASE indent your code.

Your split() function relies on temp1 being set to .... something upon entry. You should set it inside the function. Ideally the function should also take whatever parameters it needs.

Building (and deleting from) singly linked lists is easy if you use a pointer to a pointer. This points to the head pointer, or to a node's "next" pointer.

First, let's change showdata() so it can print any list. After all, we have several:
1
2
3
4
5
6
7
8
9
10
void
showdata(abc *head)
{
    abc *temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << '\n';
}


Then create split(), but pass references to the heads of all three lists:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Split inHead so every other item goes into evenHead or
// oddHead. The first item goes to oddHead, second to evenHead, etc.
void
split(abc * &inHead, abc * &evenHead, abc* &oddHead )
{
    abc **epp=&evenHead, **opp = &oddHead;
    // initialize the lists to NULL. Ideally they should be cleared.
    *epp = *opp = NULL;
    bool odd = true;
    for (abc *tmp1 = inHead; tmp1; tmp1 = tmp1->next) {
        if (odd) {
            *opp = tmp1;
            opp = &tmp1->next;
        } else {
            *epp = tmp1;
            epp = &tmp1->next;
        }
        odd = !odd;
    }
    // Clean up the lists
    inHead = NULL;
    *epp = *opp = NULL;
}


Finally, print the before and after lists in main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int
main()
{
    int x;
    cout << " 1ST LINKED LIST:" << endl;
    cout << "How many nodes you want to make:" << endl;
    cin >> x;
    for (int i = 0; i < x; i++) {
        makenodesONE();
    }
    cout << endl;
    cout << "Original list: ";
    showdata(head1);
    abc *oddHead=NULL, *evenHead=NULL;
    split(head1, evenHead, oddHead);
    cout << "odd list: ";
    showdata(oddHead);
    cout << "even list: ";
    showdata(evenHead);

    system("pause");
}

I have the same problem of you..,i tried the previous code there is a problem when you input a diffrent non arder numbers ,the code dosn't work in a right way..how i can fix this?
i tried the previous code there is a problem when you input a diffrent non arder numbers
Please post the code that you're using the input that you're giving. I don't know what "non arder numbers" means.
Topic archived. No new replies allowed.