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