I'm doing an assignment that requires various actions to be done on a linked list. AppendNode, InsertNode, DeleteNode, displayList, and a search list function. I was able to get all of these to work fine except for the search list function. My whole program exploded when I tried and when I comment out the search function it runs fine again.
#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;
elsereturn NULL;
}
#endif
#include<iostream>
#include "Linked.h"
usingnamespace 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;
}
foo.cpp:32:2: error: ‘Linked<T>::ListNode’ is not a template
foo.cpp: In member function ‘void Linked<T>::displayList() const’:
foo.cpp:191:3: error: ‘cout’ was not declared in this scope
foo.cpp:191:29: error: ‘endl’ was not declared in this scope
foo.cpp: At global scope:
foo.cpp:199:1: error: ‘ListNode’ does not name a type
#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
#include<iostream>
#include "Linked.h"
usingnamespace 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;
}
As you can see I commented out the sequentialSearch function header, definition, and call. The problem is that I just honestly cannot figure out for the life of me how to make a templated function that will search through a linked list for a specified value. I was able to get all the other functions working correctly and have attempted the search function.
Could anyone assist in helping me create this search function?