Need help with Array Based List, specifically the intersection of two array based list

May 8, 2018 at 10:53pm
My code so far:

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
117
118
119
120
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef int ItemType;

class List2 {
public:
	List2();
	~List2();

	void PutItem(ItemType num);
	void HasItem(ItemType num, bool& found);
	void Print();
	int GetLength() { return length;}
	void DeleteFirst(ItemType num);
	void DeleteAll(ItemType num);
	void Intersection(List2* li);

	int* data;
	int  maxSize;
	int  length;
};

List2::List2() {
	//default constructor
	maxSize = 5;
	data = new int[maxSize];
	length = 0;
}

List2::~List2() {
	delete[] data;
}

void List2::PutItem(ItemType num) {
	if (length == maxSize) { 
							
		maxSize *= 2;
		int* newData = new int[maxSize];
		for (int i = 0; i<length; i++) {
			newData[i] = data[i];
		}
		delete[] data;
		data = newData;
	}
	data[length] = num;
	length++;

}

void List2::HasItem(ItemType num, bool& found) {
	found = false;
	for (int i = 0; i<length; i++) {
		if (data[i] == num) {
			found = true;
			break;
		}
	}
}

void List2::Print() {
	cout << "length = " << length << " maxSize = " << maxSize << endl;
	cout << "---------------------------" << endl;
	for (int i = 0; i<length; i++) {
		cout << "i = " << i << " element = " << data[i] << endl;
	}
	cout << "---------------------------" << endl;
}

void List2::DeleteFirst(ItemType num) {
	bool found = false;
	int i;
	for (i = 0; i<length; i++)
		if (data[i] == num) {
			found = true;
			break;
		}
	if (found) {
		data[i] = data[length - 1];
		length--;
	}
}

void List2::DeleteAll(ItemType num) {
	bool found = false;
	HasItem(num, found);
	while (found) {
		DeleteFirst(num);
		HasItem(num, found);
	}
}

void List2::Intersection(List2* li) {
	

	)
int main() {
	List2 list;
	list.PutItem(5);
	list.PutItem(27);
	list.PutItem(53);
	list.PutItem(7);
	list.PutItem(5);
	list.PutItem(83);
	list.Print();
	list.DeleteAll(5);
	list.Print();

	List2 list2;
	list2.PutItem(42);
	list2.PutItem(3);
	list2.PutItem(27);
	
	list2.Print();
	int a;
	cin >> a;

	return 0;
}

Question that I have trouble with:

4. Write an Intersection function that will output the elements that exist in it's list that match with elements that exist in a list that is passed in. This means that the parameter to the function should be of type List2.
Last edited on May 8, 2018 at 11:29pm
May 8, 2018 at 10:58pm
This is your tenth post. You should know to use code tags by now.
Edit your post and repaste your code within code tags: http://www.cplusplus.com/forum/articles/16853/
May 8, 2018 at 11:31pm
No one told me about it, but I edit it if it help you guys out.
May 9, 2018 at 12:04am
Thanks for doing that. The indentation was missing before which makes it very hard to read.

It would make more sense for HasItem to return a bool instead of needing to have one passed in. Then you could write DeleteAll like this:
1
2
3
4
void List2::DeleteAll(ItemType num) {
	while (HasItem(num))
		DeleteFirst(num);
}


You don't need the "found" flag in DeleteFirst.
1
2
3
4
5
6
7
void List2::DeleteFirst(ItemType num) {
	int i;
	for (i = 0; i < length && data[i] != num; i++)
	    ;
	if (i < length)
		data[i] = data[--length];
}


The intersection function seems simple enough:
1
2
3
4
5
6
7
8
void List2::Intersection(List2& li) {
	for (int i = 0; i < li.length; i++)
		for (int j = 0; j < length; j++)
			if (data[j] == li.data[i]) {
				cout << data[j] << '\n';
				break;
			}
}

Last edited on May 9, 2018 at 12:05am
May 9, 2018 at 1:03am
Thanks this help a lots.
Topic archived. No new replies allowed.