LinkedList Search

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.

Any help with this would be greatly appreciated.

Linked.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#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 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;

}
http://www.cplusplus.com/forum/general/112111/
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
Hello Ne555, thank you for your response.

At this point I know where the error lies. I don't necessarily need to use the debugger to figure that out. For example, the following code runs fine:

Linked.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;

}


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?

Topic archived. No new replies allowed.