When I run it starts with a junk value in the first node. I can't get the getValue function to work properly either I get error that says to few arguments when I try to call it in main. Also the minValue and maxValue doesn't output anything and I tried it several different ways now. Any help is appeciated I have been on this all weekend.

//SimpleLinkedList.h
#include <iostream>
using namespace std;

struct Node 
{
char cvalue;
double dvalue;
int keyvalue;
Node *next;
};

//SimpleLinkedList.cpp
#include "SimplelinkedListLab_2.h"

class SimpleLinkedList
{
private:
Node *first;	// This is the only member variable your are allowed!
public:
SimpleLinkedList();	// Default constructor
~SimpleLinkedList();	// Destructor must delete all nodes in the list
bool empty();// returns true if list is empty
void insert (char, double, int);
void remove(int);
int listCount();
bool getValue(int, char & c, double & d);
void minValue( char & cval, double & dval);
void maxValue(char & cval, double & dval);
void displayList( ostream & out);
};
//default constructor
SimpleLinkedList :: SimpleLinkedList()
{
first = new Node;
first -> keyvalue = 0;
first -> next = NULL;
}
//destructor deletes all nodes in list
SimpleLinkedList :: ~SimpleLinkedList()
{
Node *temp = first;
while (temp != NULL)
{
temp = first -> next;
delete temp;
}
}
//returns true if list is empty
bool SimpleLinkedList :: empty()
{
if (first -> next == NULL)
return true;
else
return false;
}
//inserts new node in sorted order
void SimpleLinkedList :: insert( char c, double d, int key)
{
Node *tmp = new Node;
tmp -> dvalue = d;
tmp -> cvalue = c;
tmp -> keyvalue = key;
if (tmp == NULL)
return;
if (first == NULL || key < first -> keyvalue)
{
first = tmp;
}
else
{
Node *pred = first;
while (pred -> next != NULL && pred -> next -> keyvalue < key)
{
pred = pred -> next;
}
tmp -> next = pred -> next;
pred -> next = tmp;
}
}
// Traverse the list, keeping track of where the precedessor for each node
// is located. When the value is found in some node in the list, make the 
// predecessor node bypass the node which holds the value. Then delete
// the node which was removed from the list.
void SimpleLinkedList :: remove(int key)
{
Node *pred = NULL;
Node *tmp = first;
if (tmp == NULL)
return;
if (tmp -> keyvalue == key)
{
first = tmp -> next;
delete tmp;
return;
}
tmp = tmp -> next;
while (tmp != NULL && tmp -> keyvalue != key)
{
pred = tmp;
tmp = tmp -> next;
}
if (tmp != NULL)
{
pred -> next = tmp -> next;
delete tmp;
}
}

// Returns the number of nodes in the list.
int SimpleLinkedList :: listCount()
{
Node *tmp = first;
int count = 0;
while (tmp != NULL)
{
count++;
tmp = tmp -> next;
}
delete tmp;
return count;
}

//displays list
void SimpleLinkedList :: displayList( ostream & out)
{
Node *tmp = first;
while(tmp != NULL)
{
out << tmp -> cvalue << " ";  
out << tmp -> dvalue << " ";
out << tmp -> keyvalue << " ";
tmp = tmp -> next;
out << endl;
 }
out << endl;
}

// Traverse the list until you locate the requested key value. Return the
// character and double values from the node which has the requested key 
// value. If successful return true, otherwise return false.
bool SimpleLinkedList :: getValue(int key, char & c, double & d)
{
Node *tmp = first;
while (tmp != NULL && tmp -> keyvalue != key)
{
tmp = tmp -> next;
}
c = tmp -> cvalue;
d = tmp -> dvalue;
if (tmp == NULL)
return false;
else
return true;
}

// The methods are used to find the minimum and
// maximum values in the list. If the list is empty, simply 		 			
// set cval and dval  to zero. For the minimum cval, search
// the “c” field in the nodes of the list. For the minimum dval, 
// search the “d” field in the nodes of the list.
void SimpleLinkedList :: minValue( char & cval, double & dval)
{
if (first == NULL)
{
cval = 0;
dval = 0;
}
else
{
Node *tmp = first;
char min = first -> cvalue;
double dmin = first -> dvalue;
tmp = tmp -> next; 
while (tmp != NULL)
{
if (min > tmp -> cvalue)
min = tmp -> cvalue;
else if (dmin > tmp -> dvalue)
{
dmin = tmp -> dvalue;
tmp = tmp -> next;
}
}
cval=min;
dval=dmin; 
cout << "Minimum character value is " << min;
cout << "Minimum double value is " << dmin;
}
}

void SimpleLinkedList :: maxValue(char & cval, double & dval)
{
Node *tmp = first;
char max = cval;
double dmax = dval;
while (tmp != NULL)
{
if (cval > tmp -> cvalue)
tmp = tmp -> next;
else
max = cval;
}
cout << "Maximum character value is " << max;
delete tmp;
Node *tmp2 = first;
while (tmp != NULL)
 {
if (dval > tmp -> dvalue)
tmp = tmp -> next;
else
dmax = dval;
}
cout << "Maximum double value is " << dmax;
delete tmp2;
};

int main ()
{
SimpleLinkedList list;
cout << list.empty() << endl;
list.insert('A', 1, 1);
list.insert('B', 2, 2);
list.insert('E', 5, 5);
list.insert('C', 3, 3);
list.insert('D', 4, 4);
list.displayList(cout);
list.remove(5);
cout << list.getValue(1) << endl;
list.minValue();
list.maxValue();
list.displayList(cout);
cout << list.empty() << endl;
cout << list.listCount() << endl;
system ("PAUSE");
}

Recommended Answers

All 5 Replies

Your code formatted.

//SimpleLinkedList.h
#include <iostream>
using namespace std;

struct Node 
{
    char cvalue;
    double dvalue;
    int keyvalue;
    Node *next;
};

//SimpleLinkedList.cpp
#include "SimplelinkedListLab_2.h"

class SimpleLinkedList
{
private:
    Node *first;	// This is the only member variable your are allowed!
public:
    SimpleLinkedList();	// Default constructor
    ~SimpleLinkedList();	// Destructor must delete all nodes in the list
    bool empty();// returns true if list is empty
    void insert (char, double, int);
    void remove(int);
    int listCount();
    bool getValue(int, char & c, double & d);
    void minValue( char & cval, double & dval);
    void maxValue(char & cval, double & dval);
    void displayList( ostream & out);
};
//default constructor
SimpleLinkedList :: SimpleLinkedList()
{
    first = new Node;
    first -> keyvalue = 0;
    first -> next = NULL;
}
//destructor deletes all nodes in list
SimpleLinkedList :: ~SimpleLinkedList()
{
    Node *temp = first;
    while (temp != NULL)
    {
        temp = first -> next;
        delete temp;
    }
}
//returns true if list is empty
bool SimpleLinkedList :: empty()
{
    if (first -> next == NULL)
        return true;
    else
        return false;
}
//inserts new node in sorted order
void SimpleLinkedList :: insert( char c, double d, int key)
{
    Node *tmp = new Node;
    tmp -> dvalue = d;
    tmp -> cvalue = c;
    tmp -> keyvalue = key;
    if (tmp == NULL)
        return;
    if (first == NULL || key < first -> keyvalue)
    {
        first = tmp;
    }
    else
    {
        Node *pred = first;
        while (pred -> next != NULL && pred -> next -> keyvalue < key)
        {
            pred = pred -> next;
        }
        tmp -> next = pred -> next;
        pred -> next = tmp;
    }
}
// Traverse the list, keeping track of where the precedessor for each node
// is located. When the value is found in some node in the list, make the 
// predecessor node bypass the node which holds the value. Then delete
// the node which was removed from the list.
void SimpleLinkedList :: remove(int key)
{
    Node *pred = NULL;
    Node *tmp = first;
    if (tmp == NULL)
        return;
    if (tmp -> keyvalue == key)
    {
        first = tmp -> next;
        delete tmp;
        return;
    }
    tmp = tmp -> next;
    while (tmp != NULL && tmp -> keyvalue != key)
    {
        pred = tmp;
        tmp = tmp -> next;
    }
    if (tmp != NULL)
    {
        pred -> next = tmp -> next;
        delete tmp;
    }
}

// Returns the number of nodes in the list.
int SimpleLinkedList :: listCount()
{
    Node *tmp = first;
    int count = 0;
    while (tmp != NULL)
    {
        count++;
        tmp = tmp -> next;
    }
    delete tmp;
    return count;
}

//displays list
void SimpleLinkedList :: displayList( ostream & out)
{
    Node *tmp = first;
    while(tmp != NULL)
    {
        out << tmp -> cvalue << " ";  
        out << tmp -> dvalue << " ";
        out << tmp -> keyvalue << " ";
        tmp = tmp -> next;
        out << endl;
    }
    out << endl;
}

// Traverse the list until you locate the requested key value. Return the
// character and double values from the node which has the requested key 
// value. If successful return true, otherwise return false.
bool SimpleLinkedList :: getValue(int key, char & c, double & d)
{
    Node *tmp = first;
    while (tmp != NULL && tmp -> keyvalue != key)
    {
        tmp = tmp -> next;
    }
    c = tmp -> cvalue;
    d = tmp -> dvalue;
    if (tmp == NULL)
        return false;
    else
        return true;
}

// The methods are used to find the minimum and
// maximum values in the list. If the list is empty, simply 		 			
// set cval and dval  to zero. For the minimum cval, search
// the “c” field in the nodes of the list. For the minimum dval, 
// search the “d” field in the nodes of the list.
void SimpleLinkedList :: minValue( char & cval, double & dval)
{
    if (first == NULL)
    {
        cval = 0;
        dval = 0;
    }
    else
    {
        Node *tmp = first;
        char min = first -> cvalue;
        double dmin = first -> dvalue;
        tmp = tmp -> next; 
        while (tmp != NULL)
        {
            if (min > tmp -> cvalue)
                min = tmp -> cvalue;
            else if (dmin > tmp -> dvalue)
            {
                dmin = tmp -> dvalue;
                tmp = tmp -> next;
            }
        }
        cval=min;
        dval=dmin; 
        cout << "Minimum character value is " << min;
        cout << "Minimum double value is " << dmin;
    }
}

void SimpleLinkedList :: maxValue(char & cval, double & dval)
{
    Node *tmp = first;
    char max = cval;
    double dmax = dval;
    while (tmp != NULL)
    {
        if (cval > tmp -> cvalue)
            tmp = tmp -> next;
        else
            max = cval;
    }
    cout << "Maximum character value is " << max;
    delete tmp;
    Node *tmp2 = first;
    while (tmp != NULL)
    {
        if (dval > tmp -> dvalue)
            tmp = tmp -> next;
        else
            dmax = dval;
    }
    cout << "Maximum double value is " << dmax;
    delete tmp2;
};

int main ()
{
    SimpleLinkedList list;
    cout << list.empty() << endl;
    list.insert('A', 1, 1);
    list.insert('B', 2, 2);
    list.insert('E', 5, 5);
    list.insert('C', 3, 3);
    list.insert('D', 4, 4);
    list.displayList(cout);
    list.remove(5);
    cout << list.getValue(1) << endl;
    list.minValue();
    list.maxValue();
    list.displayList(cout);
    cout << list.empty() << endl;
    cout << list.listCount() << endl;
    system ("PAUSE");
}

Compile error.

>> I can't get the getValue function to work properly either I get error that says to few arguments when I try to call it in main.


Runtime errors.

>> When I run it starts with a junk value in the first node. Also the minValue and maxValue doesn't output anything.


You can't run it if it doesn't compile, so these errors must be from code that is different from what you posted. The compile problem on line 229 is pretty straightforward. The function says it wants 3 parameters and you only give it one. That's the error. You need to need to pass two more parameters.

No I just comment out the code to run the rest and the junk value appears as the first node. The getValue I don't realize what I need to pass it do I assign a variable to that value and pass that or do I just pass &cval and &dval thru it. Sorry I am still learning.

Line 224

list.insert('E', 5, 5);

You have a key of 5(red) and for that key you have a corresponding value pair of 'E' and 5. Those are the values that getValue "gets". That's why the parameters have & in front of them in that function. The '&' is pass by reference and the function fills in the values. It can't fill them in if you don't pass them.

char cvalue;
double dvalue;
bool keyIsInList;
int key = 5;
keyIsInList = getValue(key, cvalue, dvalue);

Itried this and it says initial values of reference to non-const must be an lvalue. I thought at first I had them switched around but I don't.

Not sure where or why you're getting that error. I left off the "list." part of the line in my example, but that should have given you a different error.

Anyway, this compiles just fine, though it crashes when it runs. I had to comment out a few lines to make the compiler happy. The error you mention doesn't show up for me. Try compiling the following code. Again, it compiles fine using g++, but won't run.

//SimpleLinkedList.h
#include <iostream>
using namespace std;

struct Node
{
    char cvalue;
    double dvalue;
    int keyvalue;
    Node *next;
};

//SimpleLinkedList.cpp
//#include "SimplelinkedListLab_2.h"

class SimpleLinkedList
{
private:
    Node *first;	// This is the only member variable your are allowed!
public:
    SimpleLinkedList();	// Default constructor
    ~SimpleLinkedList();	// Destructor must delete all nodes in the list
    bool empty();// returns true if list is empty
    void insert (char, double, int);
    void remove(int);
    int listCount();
    bool getValue(int, char & c, double & d);
    void minValue( char & cval, double & dval);
    void maxValue(char & cval, double & dval);
    void displayList( ostream & out);
};
//default constructor
SimpleLinkedList :: SimpleLinkedList()
{
    first = new Node;
    first -> keyvalue = 0;
    first -> next = NULL;
}
//destructor deletes all nodes in list
SimpleLinkedList :: ~SimpleLinkedList()
{
    Node *temp = first;
    while (temp != NULL)
    {
        temp = first -> next;
        delete temp;
    }
}
//returns true if list is empty
bool SimpleLinkedList :: empty()
{
    if (first -> next == NULL)
        return true;
    else
        return false;
}
//inserts new node in sorted order
void SimpleLinkedList :: insert( char c, double d, int key)
{
    Node *tmp = new Node;
    tmp -> dvalue = d;
    tmp -> cvalue = c;
    tmp -> keyvalue = key;
    if (tmp == NULL)
        return;
    if (first == NULL || key < first -> keyvalue)
    {
        first = tmp;
    }
    else
    {
        Node *pred = first;
        while (pred -> next != NULL && pred -> next -> keyvalue < key)
        {
            pred = pred -> next;
        }
        tmp -> next = pred -> next;
        pred -> next = tmp;
    }
}
// Traverse the list, keeping track of where the precedessor for each node
// is located. When the value is found in some node in the list, make the
// predecessor node bypass the node which holds the value. Then delete
// the node which was removed from the list.
void SimpleLinkedList :: remove(int key)
{
    Node *pred = NULL;
    Node *tmp = first;
    if (tmp == NULL)
        return;
    if (tmp -> keyvalue == key)
    {
        first = tmp -> next;
        delete tmp;
        return;
    }
    tmp = tmp -> next;
    while (tmp != NULL && tmp -> keyvalue != key)
    {
        pred = tmp;
        tmp = tmp -> next;
    }
    if (tmp != NULL)
    {
        pred -> next = tmp -> next;
        delete tmp;
    }
}

// Returns the number of nodes in the list.
int SimpleLinkedList :: listCount()
{
    Node *tmp = first;
    int count = 0;
    while (tmp != NULL)
    {
        count++;
        tmp = tmp -> next;
    }
    delete tmp;
    return count;
}

//displays list
void SimpleLinkedList :: displayList( ostream & out)
{
    Node *tmp = first;
    while(tmp != NULL)
    {
        out << tmp -> cvalue << " ";
        out << tmp -> dvalue << " ";
        out << tmp -> keyvalue << " ";
        tmp = tmp -> next;
        out << endl;
    }
    out << endl;
}

// Traverse the list until you locate the requested key value. Return the
// character and double values from the node which has the requested key
// value. If successful return true, otherwise return false.
bool SimpleLinkedList :: getValue(int key, char & c, double & d)
{
    Node *tmp = first;
    while (tmp != NULL && tmp -> keyvalue != key)
    {
        tmp = tmp -> next;
    }
    c = tmp -> cvalue;
    d = tmp -> dvalue;
    if (tmp == NULL)
        return false;
    else
        return true;
}

// The methods are used to find the minimum and
// maximum values in the list. If the list is empty, simply
// set cval and dval  to zero. For the minimum cval, search
// the “c” field in the nodes of the list. For the minimum dval,
// search the “d” field in the nodes of the list.
void SimpleLinkedList :: minValue( char & cval, double & dval)
{
    if (first == NULL)
    {
        cval = 0;
        dval = 0;
    }
    else
    {
        Node *tmp = first;
        char min = first -> cvalue;
        double dmin = first -> dvalue;
        tmp = tmp -> next;
        while (tmp != NULL)
        {
            if (min > tmp -> cvalue)
                min = tmp -> cvalue;
            else if (dmin > tmp -> dvalue)
            {
                dmin = tmp -> dvalue;
                tmp = tmp -> next;
            }
        }
        cval=min;
        dval=dmin;
        cout << "Minimum character value is " << min;
        cout << "Minimum double value is " << dmin;
    }
}

void SimpleLinkedList :: maxValue(char & cval, double & dval)
{
    Node *tmp = first;
    char max = cval;
    double dmax = dval;
    while (tmp != NULL)
    {
        if (cval > tmp -> cvalue)
            tmp = tmp -> next;
        else
            max = cval;
    }
    cout << "Maximum character value is " << max;
    delete tmp;
    Node *tmp2 = first;
    while (tmp != NULL)
    {
        if (dval > tmp -> dvalue)
            tmp = tmp -> next;
        else
            dmax = dval;
    }
    cout << "Maximum double value is " << dmax;
    delete tmp2;
};

int main ()
{
    char cvalue;
    double dvalue;
    bool keyIsInList;
    int key = 5;

    SimpleLinkedList list;
    cout << list.empty() << endl;
    list.insert('A', 1, 1);
    list.insert('B', 2, 2);
    list.insert('E', 5, 5);
    list.insert('C', 3, 3);
    list.insert('D', 4, 4);
    list.displayList(cout);
    list.remove(5);
    keyIsInList = list.getValue(key, cvalue, dvalue);
    //list.minValue();
    //list.maxValue();
    list.displayList(cout);
    cout << list.empty() << endl;
    cout << list.listCount() << endl;
    //system ("PAUSE");

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.