Hello, I have a program that requires me to create an object, populate it with information, and then append that objects info to a linked list. I was able to do this successfully. However, none of my other LinkedList functions (displaylist, deletenode, and insert node) work. I really just need to be able to display this information from the LinkedList itself. Any help would be much appreciated.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
usingnamespace std;
template <class L>
class LinkedList
{
private:
// Declare a structure for the list
struct List
{
L value; // The value in the node
struct List *next; // To point to the next node
};
List *head; // The head pointer
public:
// Constructor
LinkedList()
{
head = NULL; // Establishes empty linked list
}
// Destructor
~LinkedList();
// Linked list operations
void appendNode(L);
void insertNode(L);
void deleteNode(L);
L searchList(int);
void displayList() const;
};
//***********************************************
// appendNode function adds the node to the end *
// list. It accepts an argument. *
//***********************************************
template <class L>
void LinkedList<L>::appendNode(L num)
{
List *newNode; // To point to a new node
List *nodePtr; // To move through the list
// Allocate a new node and store num there
newNode = new List;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list make
// newNode the first node
if (!head)
{
head = newNode;
}
else // Otherwise, insert newNode at end
{
// Intialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
{
nodePtr = nodePtr->next;
}
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
template <class L>
void LinkedList<L>::insertNode(L num)
{
List *newNode; // A new node
List *nodePtr; // To traverse the list
List *previousNode = NULL; // The previous node
// Allocate a new node and store num there
newNode = new List;
newNode->value = num;
// If there are no nodes in the list make
// newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list
nodePtr = head;
// Initialize previousNode to NULL
previousNode = NULL;
// Skip all nodes whose value is less than num
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list
// insert it before all other nodes
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class L>
void LinkedList<L>::deleteNode(L num)
{
List *nodePtr; // To traverse the list
List *previousNode; // To point to the previous node
// If the list is empty, do nothing
if (!head)
{
return;
}
// Determine if the first node is the one
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Intialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is not
// equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
template <class L>
void LinkedList<L>::displayList() const
{
List *nodePtr; // To move through the list
// Postion nodePtr at the head of the list
nodePtr = head;
// While nodePtr points to a node, traverse the list
while (nodePtr)
{
// Display the value in this node
cout << nodePtr->value << " ";
// Move to the next node
nodePtr = nodePtr->next;
}
}
template <class L>
L LinkedList<L>::searchList(int value)
{
List *nodePtr; // To move through the list
int location = -1;
if (!head)
{
cout << "Element not found in list." << endl;
return -1;
}
// Position nodePtr at the head of the list
nodePtr = head;
// While nodePtr points to a node, traverse the list
while (nodePtr)
{
location++;
// Determine if nodePrt is the one
if (nodePtr->value == value)
{
return location;
}
else // If not move to the next one
{
nodePtr = nodePtr->next;
}
}
return -1;
}
template <class L>
LinkedList<L>::~LinkedList()
{
List *nodePtr; // To traverse the list
List *nextNode; // To point to the next node
// Position nodePtr at the head of the list
nodePtr = head;
// While nodePtr is not at the end of the list
while (nodePtr != NULL)
{
// Save a pointer to the next node
nextNode = nodePtr->next;
// Delete the current node
delete nodePtr;
// Position nodePtr at the next node
nodePtr = nextNode;
}
}
#endif
#ifndef WEATHERSTATS_H
#define WEATHERSTATS_H
class WeatherStats
{
private:
double rainFall, // To hold rain fall amount
snowFall, // To hold snow fall amount
sunShine; // To hold amount of sunshine
public:
// Default constructor
WeatherStats();
// Constructor
WeatherStats(double, double, double);
// Mutators
void setRainFall(double);
void setSnowFall(double);
void setSunShine(double);
// Accessors
double getRainFall();
double getSnowFall();
double getSunShine();
};
//***********************************************
// Default Constructor initializes the data *
// members to 0. *
//***********************************************
WeatherStats::WeatherStats()
{
rainFall = 0;
snowFall = 0;
sunShine = 0;
}
//***********************************************
// Constructor that set the data. *
//***********************************************
WeatherStats::WeatherStats(double rain, double snow,
double sun)
{
rainFall = rain;
snowFall = snow;
sunShine = sun;
}
//***********************************************
// setRainFall function sets the amount of *
// rainfall for a given month *
//***********************************************
void WeatherStats::setRainFall(double rf)
{
rainFall = rf;
}
//***********************************************
// setSnowFall function sets the amount of *
// snowfall for a given month *
//***********************************************
void WeatherStats::setSnowFall(double sf)
{
snowFall = sf;
}
//***********************************************
// setSunShine function sets the amount of *
// sunshine for a given month *
//***********************************************
void WeatherStats::setSunShine(double ss)
{
sunShine = ss;
}
//***********************************************
// getRainFall function returns the amount of *
// rainfall for a given month *
//***********************************************
double WeatherStats::getRainFall()
{
return rainFall;
}
//***********************************************
// getSnowFall function returns the amount of *
// snowfall for a given month *
//***********************************************
double WeatherStats::getSnowFall()
{
return snowFall;
}
//***********************************************
// getSunShine function returns the amount of *
// sunshine for a given month *
//***********************************************
double WeatherStats::getSunShine()
{
return sunShine;
}
#endif
You are trying to display a list of WeatherStats, which tries to output the data using operator <<, but no such operator exists for your WeatherStats class.
Hello firedraco, thank you for your response. So what you're saying is I need to make an overloaded << function to output values in my WeatherStats class in order to display them from the linked list?