having trouble with unions and intersections

Sep 6, 2009 at 8:02pm
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.

// Josh Hicks
// Lab 1 GSP290

// list.cpp
// simple linked list program

#include <STDLIB.H>
#include <STRING>
#include <IOSTREAM>

using std::cout;
using std::string;

// 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;

setA.Insert(5);
setA.Insert(2);
setA.Insert(3);
setA.Insert(5);
setA.Insert(2);

cout << "Contents of setA: ";
setA.Output();
cout << "\n\n";

setB.Insert(1);
setB.Insert(2);
setB.Insert(4);

cout << "Contents of setB: ";
setB.Output();
cout << "\n\n";

setUnion.Union(setA, setB);
cout << "Contents of setA union setB: ";
setUnion.Output();
cout << "\n\n";

setIntersection.Intersection(setA, setB);
cout << "Contents of setA intersection setB: ";
setIntersection.Output();
cout << "\n\n";
}

Last edited on Sep 6, 2009 at 9:22pm
Sep 7, 2009 at 3:02pm
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.
Sep 7, 2009 at 9:38pm
Jsmith I understand like what I need to do it is the physical programming I don't get. That were I need the help.
Sep 8, 2009 at 11:59am
If you understand the algorithm, then perhaps you should post your non-working code (for Union and Intersection) and we can go from there.
Sep 8, 2009 at 6:37pm
Ok well here is the orginal code:

// list.cpp
// simple linked list program

#include <STDLIB.H>
#include <STRING>
#include <IOSTREAM>

using std::cout;
using std::string;

// 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";
}

and I turned it into a header and cpp file

-header-
#ifndef H_LINKEDLIST
#define H_LINKEDLIST

#include <STRING>
using namespace std;

// node object for the linked list
struct Node(int);

// implement a singly linked list
class LinkedList
{
protected:

public:

LinkedList();

~LinkedList();

void InsertFront(int newValue);
int RemoveFront();
int Length();
void Output();
};
#endif

-cpp-
#include <iostream>
#include "LinkedList.h" //jmk added to make LinkedList structure available to main

using namespace std; //jmk add to provide environ for cout/cin

void main(void)
{
LinkedList list; // instantiate a 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";
}

Here is the error I get
[ error C2062: type 'int' unexpected] <---has to do with the header file. the int above the class in Struct(Node)
Sep 8, 2009 at 7:51pm
Why did you change

1
2
3
4
5
// node object for the linked list
struct Node {
int data;
Node* link;
};


in the original code to

1
2
// node object for the linked list
struct Node(int);


in the new code? The new version is syntactically incorrect.
Topic archived. No new replies allowed.