Linked List Search Function

Posted 09 February 2014 - 06:53 PM

I'm have troubles with this program that requires me to make a search through a Linked List and find a specified value. It also needs to be a template function. I've completed the rest of the program fine and everything runs ok except for the search function. Please check the code below:

I have commented out my attempt at the search function. When uncommenting the function I get several errors about <template> being incorrect syntax. Any help into fixing this would be much appreciated.

Re: Linked List Search Function

Posted 09 February 2014 - 10:07 PM

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.

(p.s, for those of you who saw my previous post I got the search function to work I just need help with this now)

Linked.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
// LinkedList is a template class that provides member
// functions for inserting, appending, deleting, searching,
// and displaying nodes. It also has a destructor to
// destroy the list.
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;
}
}
//***********************************************
// insertNode function inserts the node in *
// numerical order. It accepts an argument. *
//***********************************************
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;
}
}
}
//***********************************************
// deleteNode function removes the node from the*
// list without breaking the links created by *
// the next pointers and removes the node from *
// memory. *
//***********************************************
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 is not at the end of the list, link
// the previous node to the node after nodePtr,
// the delete nodePtr
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
//***********************************************
// displayList shows the value stored in each *
// node of the linked list pointed to by head *
//***********************************************
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;
}
}
//***********************************************
// searchList function searches the list for an *
// element in the list. It returns the location *
// of the item in the list. *
//***********************************************
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;
}
//***********************************************
// Destructor function deletes every node in the*
// list. *
//***********************************************
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

Re: Linked List Search Function

Posted 09 February 2014 - 11:19 PM

Post the complete error message including the line number on which the error occurred.

Please stop and consider that this forum is inhabited by volunteers. You are are expecting a volunteer to try to cut and paste your code into their personal computer to try to replicate the same error you are having. What if that volunteer doesn't have Visual Studio like you? Then they will have a completely different error code, and potentially at a completely different place.

Re: Linked List Search Function

Posted 09 February 2014 - 11:25 PM

Anyway, the particular problem is actually with this:

// Display the value in this node
cout << nodePtr->value << " ";

Since you didn't define a binary operator << that takes a right hand side WeatherStats, the compiler has no idea what to do with your attempt to print out the value within the node. The typical solution for this is to implement the << operator that lets you output to a stream.

Re: Linked List Search Function

Posted 10 February 2014 - 03:54 PM

I understand which is why I posted the error message that I was receiving. Anyway, I was able to overload the << operator in the class and everything displays fine. Thank you everyone for your much needed help!