10 Replies - 750 Views - Last Post: 03 January 2012 - 11:39 PM Rate Topic: -----

#1 A4 2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-January 11

Trying to get the correct output with linked list code.

Posted 02 January 2012 - 10:05 PM

Any advice or hints would be wonderful. I need to complete the Set class in order to come up with a desired output. I'm not very good at this and my mind is blank on what to do. Thanks in advance.

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

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


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to get the correct output with linked list code.

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Trying to get the correct output with linked list code.

Posted 02 January 2012 - 10:09 PM

1.What is the desired output?
2.What are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 A4 2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-January 11

Re: Trying to get the correct output with linked list code.

Posted 02 January 2012 - 10:19 PM

View PostjimmyBo, on 02 January 2012 - 10:09 PM, said:

1.What is the desired output?
2.What are you getting?



Thanks for the quick response.

This is the example provided to me.

http://vizedhtmlcont...ab1_output2.gif

I'm supposed to have the correct numbers come out for the different outputs. Right now everything executes, but I have no numbers since I'm not telling the code how to get the numbers in the Set class.
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Trying to get the correct output with linked list code.

Posted 02 January 2012 - 10:24 PM

Is what I'm getting (an error message) what I'm supposed to be seeing? Starts with A Problem Has Occurred and has a message in three languages.

Have you got a problem with the link?

I like your username, BTW!

This post has been edited by jimmyBo: 02 January 2012 - 10:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 A4 2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-January 11

Re: Trying to get the correct output with linked list code.

Posted 02 January 2012 - 10:32 PM

View PostjimmyBo, on 02 January 2012 - 10:24 PM, said:

Is what I'm getting (an error message) what I'm supposed to be seeing? Starts with A Problem Has Occurred and has a message in three languages.

Have you got a problem with the link?

I like your username, BTW!


Thanks, I use the same name wherever I go lol. I checked the link and it worked ok for me. It looks like this, but in dos mode:

Contents of setA: 3, 2, 5,

Contents of setB: 4, 2, 1,

Contents of setA union setB: 1, 4, 5, 2, 3,

Contents of setA intersection setB: 2,

Press any key to continue..........

I have all of this except for the numbers.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Trying to get the correct output with linked list code.

Posted 03 January 2012 - 03:44 AM

Would help if you actually implemented the set class:

// 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& B)/> {

    }

    // make this the intersection of two sets
    void Intersection(Set& a, Set& B)/> {

    }
};


Was This Post Helpful? 0
  • +
  • -

#7 A4 2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-January 11

Re: Trying to get the correct output with linked list code.

Posted 03 January 2012 - 12:16 PM

View PostJackOfAllTrades, on 03 January 2012 - 03:44 AM, said:

Would help if you actually implemented the set class:

// 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& B)/> {

    }

    // make this the intersection of two sets
    void Intersection(Set& a, Set& B)/> {

    }
};




If I knew how to implement the set class, I wouldn't have posted this thread. That's why I'm asking for some help.

This post has been edited by A4 2000: 03 January 2012 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Trying to get the correct output with linked list code.

Posted 03 January 2012 - 12:51 PM

You need to show some evidence of at least trying to implement this class, we will not do the work for you. If you do not understand sets you may want to review your textbook and course notes. After that you may find these links useful: Representing and Implementing Sets and The Set Abstract Data Type. The first link seems to explain things fairly well, even though the code is java. The second link is a Power Point slide show that may explain the basics.


Jim

This post has been edited by jimblumberg: 03 January 2012 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Trying to get the correct output with linked list code.

Posted 03 January 2012 - 03:09 PM

I sort of took your misleading title at face value. My bad.
Was This Post Helpful? 1
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Trying to get the correct output with linked list code.

Posted 03 January 2012 - 06:55 PM

It could help if you first fixed up your list class ... or get one that works and doesn't leak. You could see the examples at DIC for starters ...
Or check here ...
http://developers-heaven.net/forum/index.php/topic,310.0.html

http://developers-he...index.php/topic,310.0.html

This post has been edited by David W: 04 January 2012 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#11 A4 2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-January 11

Re: Trying to get the correct output with linked list code.

Posted 03 January 2012 - 11:39 PM

I'm waiting for my book to arrive in a couple of days, so hopefully I can see some c++ examples in there. The java examples are throwing me off.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1