8 Replies - 729 Views - Last Post: 10 February 2014 - 03:54 PM Rate Topic: -----

#1 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

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:

Linked.h
#ifndef LINKED_H
#define LINKED_H
#include<iostream>

template <class T>
class Linked
{
private:
  // Declare a structure for the list
 struct ListNode
 {
 T value; // The value in this node
 struct ListNode *next; // To point to the next node
 }; 
 
 ListNode *head; // List head pointer
 public:
 // Constructor
Linked()
 { head = NULL; }
 
 // Destructor
~Linked();
 

 // Linked list operations
 void appendNode(T);
 void insertNode(T);
 void deleteNode(T);
 void destroyList();
 void displayList() const;
 ///ListNode<T>* sequentialSearch(const T) const;
 };


template<class T>
Linked<T>::~Linked()
{
ListNode *nodePtr; // To traverse the list
ListNode *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;
}
}


//insertnode
template <class T>
void Linked<T>::insertNode(T num)
  {
  ListNode *newNode; // A new node
  ListNode *nodePtr; // To traverse the list
  ListNode *previousNode = NULL; // The previous node
  
  // Allocate a new node and store num there.
  newNode = new ListNode;
  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
template <class T>
void Linked<T>::deleteNode(T num)
 {
 ListNode *nodePtr; // To traverse the list
 ListNode *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
 {
 // Initialize 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, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}

template <class T>
void Linked<T>::appendNode(T num)
  {
  ListNode *newNode; // To point to a new node
  ListNode *nodePtr; // To move through the list
  
  // Allocate a new node and store num there.
  newNode = new ListNode;
  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.
  {
  // Initialize 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 T>
void Linked<T>::displayList() const
  {
  ListNode *nodePtr; // To move through the list
  
  // Position 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 << endl;
  
  // Move to the next node.
  nodePtr = nodePtr->next;
  }
}
/*
template <class T>
ListNode<T>* Linked<T>::sequentialSearch(const T& searchItem) const
   {
   LNode<T>* current;

   for (current = first; current != NULL && searchItem > current->data; current = current->next)
      ;

   if (current != NULL && searchItem == current->data)
      return current;
   else
      return NULL;
   }
    */
 #endif 



main.cpp
#include<iostream>
#include "Linked.h"

using namespace std;

int main(){

	Linked<double> list;
	cout<<"List is populated with 3 values: "<<endl<<endl;
	list.appendNode(2.5);
	list.appendNode(7.9);
	list.appendNode(12.6);
	list.displayList();
	cout<<endl;

	cout<<"10.5 is inserted into the list: "<<endl<<endl;
	list.insertNode(10.5);
	list.displayList();
	cout<<endl;

	cout<<"7.9 is deleted from the list: "<<endl<<endl;
	list.deleteNode(7.9);

	list.displayList();

	//list.sequentialSearch(12.6);

	
	system("pause");
	return 0;

}



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.

Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Search Function

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Linked List Search Function

Posted 09 February 2014 - 07:19 PM

Hi, when you declare the function you have no reference parameter, when you define your function you do.
Was This Post Helpful? 0
  • +
  • -

#3 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 246
  • View blog
  • Posts: 893
  • Joined: 18-April 09

Re: Linked List Search Function

Posted 09 February 2014 - 07:45 PM

Hi,

Also, from seeing your sequentialSearch() function, I have noticed you didn't declare any of these in your ListNode structure:

1. data
2. first
3. LNode.

according to your ListNode structure, maybe it should be like this in your sequentialSearch() function

1. value
2. head
3. ListNode

instead.
Was This Post Helpful? 0
  • +
  • -

#4 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

Re: Linked List Search Function

Posted 09 February 2014 - 08:43 PM

Thanks for the replies guys I can't believe I did that. Regardless, I've changed everything in the function definition and declaration but keep getting the same errors for my function as follows:

Error 1 error C2143: syntax error : missing ';' before '<'
Error 2 error C4430: missing type specifier - int assumed.
Error 3 error C2988: unrecognizable template declaration/definition
Error 4 error C2059: syntax error : '<'
Error 5 error C2065: 'T' : undeclared identifier
Error 6 error C2923: 'Linked' : 'T' is not a valid template type argument for parameter 'T'

Obviously there is something wrong with my declaration but I can't tell how I can fix it and still make sure it is a template function.
Was This Post Helpful? 0
  • +
  • -

#5 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 246
  • View blog
  • Posts: 893
  • Joined: 18-April 09

Re: Linked List Search Function

Posted 09 February 2014 - 09:47 PM

Could you post your corrected code for us to help ?
Was This Post Helpful? 0
  • +
  • -

#6 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

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 



WeatherStats.h
 #ifndef WEATHERSTATS_H
 #define WEATHERSTATS_H 

 // This class holds information about the weather
 // for a given month. It has data members to hold
 // the amount rain, snow, and sun for the month.
 // It has member functions for the data members. 

 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 



main.cpp

#include "WeatherStats.h"
#include "LinkedList.h"
#include <iostream>
#include <list>

using namespace std;

// This program creates an instance of LinkedList
// class with a data type of the WeatherStats.
// It asks the user for information about weather
// statistics and calls a function to display the 
// information

int main()
{
 int months;    // To hold number of months
 double rainFall;
 double snowFall;
 double sunShine;

// Create a LinkedList object
LinkedList<WeatherStats> weather;

// Define some WeatherStats objects
WeatherStats forecast;

// Get input from user 
cout << "How many months do you wish to enter\n"
     << "weather statistics for? ";
cin >> months;

for (int i = 0; i < months; i++)
{
cout << "Enter the amounts for month " << i+1 << endl;
cout << "Rainfall: ";
cin >> rainFall;
cout << "Snowfall: ";
cin >> snowFall;
cout << "Sunshine: ";
cin >> sunShine;
cout << endl;

// Store the amounts
forecast.setRainFall(rainFall);
forecast.setSnowFall(snowFall);
forecast.setSunShine(sunShine);

// Append the amounts
cout<<endl;

}
weather.appendNode(forecast);
weather.displayList(); // <---This is where the error occurs

system("Pause");
}



The error I get is as follows:

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'WeatherStats' (or there is no acceptable conversion)

Anyone know what's going on?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

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.
Was This Post Helpful? 1
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

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.
Was This Post Helpful? 1
  • +
  • -

#9 sensible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-December 12

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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1