Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,644 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,099 people online right now. Registration is fast and FREE... Join Now!




Union and intersection

 
Reply to this topicStart new topic

Union and intersection

Outtey22
post 4 Sep, 2008 - 02:56 PM
Post #1


New D.I.C Head

*
Joined: 23 Sep, 2007
Posts: 27



Thanked 1 times
My Contributions


Hi guys!

I'm having a bit of an issue figuring this out and was hoping to get a push in the right direction. I need to create a method of creating a union of two sets. I also need to make one for the intersection. Any advice you can give would be greatly appreciated as I am WAY over my head.

The area I'm most concerned about is line 100 - 117

cpp

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

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

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

}

// make this the union of two sets
void Union(Set& a, Set& cool.gif {

}

// make this the intersection of two sets
void Intersection(Set& a, Set& cool.gif {

}
};


};

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";
system("PAUSE");
}


This post has been edited by Outtey22: 5 Sep, 2008 - 02:57 AM
User is offlineProfile CardPM

Go to the top of the page

sensui
post 4 Sep, 2008 - 11:27 PM
Post #2


D.I.C Head

Group Icon
Joined: 24 Aug, 2008
Posts: 132



Thanked 20 times

Dream Kudos: 50
My Contributions


To have line numbers you must use
[code=cpp]Your code goes here[/code]
User is offlineProfile CardPM

Go to the top of the page

Outtey22
post 7 Sep, 2008 - 10:52 AM
Post #3


New D.I.C Head

*
Joined: 23 Sep, 2007
Posts: 27



Thanked 1 times
My Contributions


Thanks for the tip Sensui!

I've been banging my head against the wall but I'm very close now. I think that I have all of my code right so it may be a syntax error. If you can see what I've done wrong please tell me.

cpp

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

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

// 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 << ", ";
}
}
// 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) {
int duplicate = Search(newValue);
if (duplicate == -1)
InsertFront(newValue);
}

// make this the union of two sets
void Union(Set& a, Set& cool.gif {
Node*p;
for( p = a.front;p != NULL; p = p->link){
Set::Insert(p->data);
}
for( p = b.front; p != NULL; p = p->link){
Set::Insert(p->data);
}
}

// make this the intersection of two sets
void Intersection(Set& a, Set& cool.gif {
Node* p;
Node* q;
for( p = a.front; p != NULL; p = p->link)
{
for( q = b.front; p != NULL; p = p->link)
{
if (p == q)
{
Insert(p->data);
}
}
}
}
};

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";
system("PAUSE");
}


I think my error lies somewhere between lines 101 and 137.

This post has been edited by Outtey22: 7 Sep, 2008 - 10:55 AM
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 7 Sep, 2008 - 11:01 AM
Post #4


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 508



Thanked 45 times

Dream Kudos: 25
My Contributions


Another DeVry student, eh?

cpp
for( p = a.front;p != NULL; p = p->link){  
Set::Insert(p->data);
}


should be

cpp
for( p = a.front;p != NULL; p = p->link){
Insert(p->data);
}


And here:
cpp
if (p == q)  
{
Insert(p->data);
}


In that code snippet you are comparing the Node pointers, which are never going to be equal. You need to compare the contents of the Node.
User is offlineProfile CardPM

Go to the top of the page

Outtey22
post 7 Sep, 2008 - 11:13 AM
Post #5


New D.I.C Head

*
Joined: 23 Sep, 2007
Posts: 27



Thanked 1 times
My Contributions


You're right about DeVry. I really don't recommend it. I think I'm going to transfer after this session due to the lack of instruction.

Anyway, I made the changes you suggested and changed the "p==q" to "p->data == q->data".

Now it seems like my only hangup is that it won't compile because

"error C2504: 'LinkedList' : base class undefined"

Any idea on how to fix that?
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 7 Sep, 2008 - 11:21 AM
Post #6


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 508



Thanked 45 times

Dream Kudos: 25
My Contributions


You didn't close the end of the LinkedList class with an end brace and semi-colon: };
User is offlineProfile CardPM

Go to the top of the page

Outtey22
post 7 Sep, 2008 - 11:28 AM
Post #7


New D.I.C Head

*
Joined: 23 Sep, 2007
Posts: 27



Thanked 1 times
My Contributions


D'oh!

Thanks for all of the help! Have you ever considered being an instructor? You seem to be about 1000 times more qualified than the one I have.

One last question, what would cause my program to crash after performing the union? It seems like when it tries to perform the intersection it crashes.

More specifically, it's telling me that I have an access violation (segmentation fault).

EDIT: Nevermind, I found it! In the intersection code I didn't check my ps and qs.

Thanks for all of the help! YOU ARE A LIFESAVER!!!!

This post has been edited by Outtey22: 7 Sep, 2008 - 11:37 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 04:41AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month