I am doing some work over unions and intersections and I seem to be lost. Ill post the code our instructor gave us. If any1 can help explain how to code unions and intersections that would be much appreciated.
// node object for the linked list
struct Node {
int data;
Node* link;
};
// implement a singly linked list
class LinkedList {
protected:
Node* front; // pointer to the front of the linked list
Node* back; // pointer to the last node in the linked list
public:
// constructs an empty list
}
// deletes the list
~LinkedList() {
// remove objects from the list as long as list is not empty
while(Length() > 0) {
RemoveFront();
}
}
// inserts a node at the front of the list
void InsertFront(int newValue) {
Node* newNode = new Node;
newNode->data = newValue;
if (front == NULL) {
// list must be empty so make front & back point to new node
front = back = newNode;
newNode->link = NULL;
} else {
// list is not empty so insert between front and first node
newNode->link = front;
front = newNode;
}
}
// removes a node from the front of the list
int RemoveFront() {
int returnVal;
Node *temp;
if (front != NULL) {
// list is not empty so remove & return first node
returnVal = front->data;
temp = front;
front = front->link;
} else {
// list is empty just return 0
returnVal = 0;
}
return returnVal;
}
// returns the length of the list
int Length() {
Node* p;
int count = 0;
// loop through each node in the list until we find a null value
for (p = front; p != NULL; p = p->link) {
count++;
}
return count;
}
// outputs a string containing all the data values in the list
void Output() {
Node* p;
// loop through each node in the list until we find a null value
for (p = front; p != NULL; p = p->link) {
cout << p->data << ", ";
}
}
LinkedList() {
front = back = NULL;
// search the list for a target value
// return index if found or -1 if not found
int Search(int targetVal) {
Node* p;
int count = 0;
for (p = front; p != NULL; p = p->link) {
if (p->data == targetVal) {
return count;
}
count++;
}
return -1;
}
// use inheritance to create a Set class from the LinkedList class
class Set : public LinkedList {
public:
// insert a new value only if it is unique (not already in the set)
void Insert(int newValue)
{
LinkedList::Search(newValue);
}
// make this the union of two sets
void Union(Set& a, Set& b)
{
}
// make this the intersection of two sets
void Intersection(Set& a, Set& b)
{
}
};
};
void main() {
Set setA, setB, setUnion, setIntersection;
The union of two sets A and B is the set containing all elements present in either A or B (no duplicates). So, to build the union, start with an empty result set R. Add every element from
A to R. Then, walk every element in B, and add it to R if it isn't already in R (or in A, same thing).
The intersection of two sets A and B is the set containing all elements present in both A and
B. So, to build the intersection, start with an empty result set R. For each element in A,
add it to R if the element also exists in B.
// node object for the linked list
struct Node {
int data;
Node* link;
};
// implement a singly linked list
class LinkedList {
protected:
Node* front; // pointer to the front of the linked list
Node* back; // pointer to the last node in the linked list
public:
// constructs an empty list
LinkedList() {
front = back = NULL;
}
// deletes the list
~LinkedList() {
// remove objects from the list as long as list is not empty
while(Length() > 0) {
RemoveFront();
}
}
// inserts a node at the front of the list
void InsertFront(int newValue) {
Node* newNode = new Node;
newNode->data = newValue;
if (front == NULL) {
// list must be empty so make front & back point to new node
front = back = newNode;
newNode->link = NULL;
} else {
// list is not empty so insert between front and first node
newNode->link = front;
front = newNode;
}
}
// removes a node from the front of the list
int RemoveFront() {
int returnVal;
Node *temp;
if (front != NULL) {
// list is not empty so remove & return first node
returnVal = front->data;
temp = front;
front = front->link;
} else {
// list is empty just return 0
returnVal = 0;
}
return returnVal;
}
// returns the length of the list
int Length() {
Node* p;
int count = 0;
// loop through each node in the list until we find a null value
for (p = front; p != NULL; p = p->link) {
count++;
}
return count;
}
// outputs a string containing all the data values in the list
void Output() {
Node* p;
// loop through each node in the list until we find a null value
for (p = front; p != NULL; p = p->link) {
cout << p->data << ", ";
}
}
};
void main() {
// instantiate a list
LinkedList list;
// insert some values
list.InsertFront(5);
list.InsertFront(4);
list.InsertFront(3);
list.InsertFront(2);
list.InsertFront(1);
// display the list values
list.Output();
cout << "\n\n";
}