#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
typedefint 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 = newint[maxSize];
length = 0;
}
List2::~List2() {
delete[] data;
}
void List2::PutItem(ItemType num) {
if (length == maxSize) {
maxSize *= 2;
int* newData = newint[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.