Need help in Merging two linked lists alternatively

Create a linked list and add a function to merge 2 singly Linked Lists according to following rules.
Insert nodes of 2nd linked list into 1st linked list at alternate position of first list.
For Example :
List 1 : 5-> 7 -> 17 -> 13 -> 11
List 2: 12 -> 10 -> 2 -> 4 -> 6
.Output :
List 1: 5 -> 12 -> 7 -> 10 -> 17 -> 2 -> 13 -> 4 -> 11 -> 6
List 2: Empty


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  #include <iostream>
using namespace std;
struct abc
{
	int data;
	abc *next;
};
abc *head1 = NULL;
abc *head2 = NULL;
void showdata()
{
	abc *temp = new abc;
	while (temp != NULL)
	{
		cout << temp->data << " ";
		temp = temp->next;
	}


}
void makenodesTWO()
{
	abc *newnode = new abc;
	cout << "Enter the data in node: " << endl;
	cin >> newnode->data;
	newnode->next = NULL;
	if (head2 == NULL)
	{
		head2 = newnode;
	}
	else
	{
		abc *temp2 = head2;
		while (temp2->next != NULL)
		{
			temp2 = temp2->next;
		}
		temp2->next = newnode;

	}
}
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 merge()
{
	abc *temp1 = head1;
	abc *temp2 = head2;
	while (temp1 != NULL && temp2 != NULL)
	{
		head2 = head2->next;
		temp2 = temp1->next;
		temp1->next = temp2;
		temp2 = head2;
		temp1 = temp1->next->next;
	}

}
int main()
{
	int x, y;
	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 << " 2nd LINKED LIST:" << endl;
	cout << "How many nodes you want to make:" << endl;
	cin >> y;
	for (int i = 0; i < x; i++)
	{
		makenodesTWO();
	}
	merge();
	showdata();
	system("pause");


}

The code is breaking when I am inputing the second link list.
Last edited on
show your input
I've managed to reach merge() function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void merge()
{
	abc *temp1 = head1;
	abc *temp2 = head2;
	while (temp1 != NULL && temp2 != NULL)
	{
		head2 = head2->next;
		temp2 = temp1->next; //a = b
		temp1->next = temp2; //b = a, which is b=b, so nothing
		temp2 = head2;
		temp1 = temp1->next->next; //crash here, ¿what if temp1->next == NULL?
	}

}

its not working!I am entering 1,2,3,4,5, for the first link list and 6,7,8,9,10 for the second. the resulting answer must be 1,6,7,3,8,4,9,5,10....BUT ITS BREAKING
Use parameters as Salem C suggested in your other post.

Line 90: x should be y

Print the original lists. You'll discover that there's a bug in showdata().

You need another local variable to point to the unlinked item.

Draw out two lists with the the pointers to the next items.
Now pick a source and destination item from the lists. Suppose you need to insert the source after the destination. Using a different colored marker, draw the new pointers.

Use your diagram to write the code.

There are some special cases to deal with:
1. What if head1 is empty? Just return head2
2. What if you one list is longer than the other?
Topic archived. No new replies allowed.