intersection function in single linked list cannot work

#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
};
void print(node* head)
{
while (head != NULL)
{
cout << head->data << endl;
head = head->next;
}
}
int length(node* head)
{
int s = 0;
while (head != NULL)
{
s++;
head = head->next;
}
return s;
}
void intersection(node* sea, node* ad)
{
//node* p;
int d;
int r, t;
r = length(sea);
t = length(ad);
cout << "The first node length:" << r << endl;
cout << "The second node length:" << t << endl;
d = r - t;
for (int i = 0; i < d; i++)
{
sea = sea->next;
}
for (int i = 0; i < t; i++)
{
if (sea->data == ad->data)
{
cout << sea->data << endl;
sea = sea->next;
ad = ad->next;
}
else
sea = sea->next;
ad = ad->next;
}
}
void increase(node* head)
{
node *current, *pre, *shead;
//head = new node();
int s, d;
cout << "Enter the total node:" << endl;
cin >> s;
cout << "Enter the data first node:" << endl;
cin >> d;
head->data = d;
head->next = NULL;
current = head;
pre = head;
for (int i = 1; i < s; i++)
{
cout << "Enter the element:" << endl;
cin >> d;
head = new node();
head->data = d;
head->next = NULL;
pre->next = head;
pre = head;
}
head = current;
}
int main()
{
node* ead,*shead;
ead = new node();
shead = new node();
increase(ead);
increase(shead);
system("CLS");
//print(ead);
cout << endl << endl;
// print(shead);
intersection(ead, shead);
system("pause");
return 0;
}
Last edited on
This is better format of code to be seen.

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
#include<iostream>
using namespace std;
class node
{
public:
	int data;
	node* next;
};
void print(node* head)
{
	while (head != NULL)
	{
		cout << head->data << endl;
		head = head->next;
	}
}
int length(node* head)
{
	int s = 0;
	while (head != NULL)
	{
		s++;
		head = head->next;
	}
	return s;
}
void intersection(node* sea, node* ad)
{
	//node* p;
	int d;
	int r, t;
	r = length(sea);
	t = length(ad);
	cout << "The first node length:" << r << endl;
	cout << "The second node length:" << t << endl;
	d = r - t;
	for (int i = 0; i < d; i++)
	{
		sea = sea->next;
	}
	for (int i = 0; i < t; i++)
	{
		if (sea->data == ad->data)
		{
			cout << sea->data << endl;
			sea = sea->next;
			ad = ad->next;
		}
		else
			sea = sea->next;
		ad = ad->next;
	}
}
void increase(node* head)
{
	node* current, * pre, * shead;
	//head = new node();
	int s, d;
	cout << "Enter the total node:" << endl;
	cin >> s;
	cout << "Enter the data first node:" << endl;
	cin >> d;
	head->data = d;
	head->next = NULL;
	current = head;
	pre = head;
	for (int i = 1; i < s; i++)
	{
		cout << "Enter the element:" << endl;
		cin >> d;
		head = new node();
		head->data = d;
		head->next = NULL;
		pre->next = head;
		pre = head;
	}
	head = current;
}
int main()
{
	node* ead, * shead;
	ead = new node();
	shead = new node();
	increase(ead);
	increase(shead);
	system("CLS");
	//print(ead);
	cout << endl << endl;
	// print(shead);
	intersection(ead, shead);
	system("pause");
	return 0;
}
Last edited on
Are you want this:

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream>
using namespace std;
class node
{
public:
	int data;
	node* next;
};
void print(node* head)
{
	while (head != NULL)
	{
		cout << head->data << endl;
		head = head->next;
	}
}
int length(node* head)
{
	int s = 0;
	while (head != NULL)
	{
		s++;
		head = head->next;
	}
	return s;
}
void intersection(node* sea, node* ad)
{
	//node* p;
	node* seaI;
	node* adI;
	int r, t;
	r = length(sea);
	t = length(ad);
	cout << "The first node length:" << r << endl;
	cout << "The second node length:" << t << endl;
	//the first node length r is biger or equal to secod node t
	if (r >= t)
	{	
		for (int i = 0; i < r; i++)
		{
			adI = ad;
			for (int j = 0; j < t; j++)
			{
				if (sea->data == adI->data)
				{
					cout << sea->data << endl;
					adI = adI->next;
				}
				else
					adI = adI->next;
			}
			sea = sea->next;
		}
	}
	else
	{
		for (int i = 0; i < t; i++)
		{
			seaI = sea;
			for (int j = 0; j < r; j++)
			{
				if (seaI->data == ad->data)
				{
					cout << ad->data << endl;
					seaI = seaI->next;
				}
				else
					seaI = seaI->next;
			}
			ad = ad->next;
		}
	}
}
void increase(node* head)
{
	node* current, * pre, * shead;
	//head = new node();
	int s, d;
	cout << "Enter the total node:" << endl;
	cin >> s;
	cout << "Enter the data first node:" << endl;
	cin >> d;
	head->data = d;
	head->next = NULL;
	current = head;
	pre = head;
	for (int i = 1; i < s; i++)
	{
		cout << "Enter the element:" << endl;
		cin >> d;
		head = new node();
		head->data = d;
		head->next = NULL;
		pre->next = head;
		pre = head;
	}
	head = current;
}
int main()
{
	node* ead, * shead;
	ead = new node();
	shead = new node();
	increase(ead);
	increase(shead);
	system("CLS");
	//print(ead);
	cout << endl << endl;
	// print(shead);
	intersection(ead, shead);
        delete ead;
	delete shead;
	system("pause");
	return 0;
}


Tell me if there is some problem or you want something else. Also it is not optimize. And this code don't remove repeated elements :-)

http://www.cplusplus.com/reference/algorithm/set_intersection/

Why you need this * shead when you don't use it in
void increase(node* head)?

If you ask me where you wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// What will happen if first node is smaller one?
// And why you want to make two nodes equal on first place?!
// You will lose elements to compare.
d = r - t;
	for (int i = 0; i < d; i++)
	{
		sea = sea->next;
	}
// how do you expect to go through all the elements of two lists, arrays,  
// containers an so on in one cycle?
for (int i = 0; i < t; i++)
	{
		if (sea->data == ad->data)
		{
			cout << sea->data << endl;
			sea = sea->next;
			ad = ad->next;
		}
		else
			sea = sea->next;
		ad = ad->next;
	}


Do you understand exactly what

1
2
sea = sea->next;
ad = ad->next;


do?

Why you deal with this shit in this way? I see such codes in intro C++ books, when author try to make riders to respect him. "Do you see what kind of tangled code can I make?" I can make broken stack :-)

P.s. Advice, start to read The C++ Standard Template Library (STL).
Last edited on
bro i am beginner in data structure so i make some simple mistake,so thank you for helping me. i also make remove function to remove the same data in the node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void remove(node* head)
{
	node*p = head;
	node*q;
	while (p != NULL && p->next != NULL)
	{
		if (p->data == p->next->data)
		{
			q = p->next->next;
			if (q == NULL)
			{
				p->next = NULL;
				break;
			}
			p->next = q;
		}
		if (p->data != p->next->data)
		{
			p = p->next;
		}
	}
	print(head);
}
Last edited on
asad butt, don't worry and do not be ashamed to ask if something is unclear to you. The smart people ask questions, the stupid ones think they are know everything. When you post code here, click "<>" button on the right, to make code Format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void remove(node* head)
{
	node* p = head;
	node* q;
	while (p != NULL && p->next != NULL)
	{
		if (p->data == p->next->data)
		{
			q = p->next->next;
			if (q == NULL)
			{
				p->next = NULL;
				break;
			}
			p->next = q;
		}
		if (p->data != p->next->data)
		{
			p = p->next;
		}
	}
	print(head);
}


P.s. By the way, I'm newbie C++ too.
Last edited on
Topic archived. No new replies allowed.