Linked List search needs a fix.

My program works almost 100% well, except for the search. Every time you search a node to know its position, it keeps printing -1 instead of 0, 1, 2 and so on. How can this be fixed? Here's my code:

Header file.
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
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>
using namespace std;

class LinkedList
{
	private:
		struct ListNode			
		{
			int value;		
			struct ListNode *next;	
		};

		ListNode *head;			

	public:
		LinkedList()			
		{ 
			head = NULL;		
		}

		~LinkedList();			

		void appendNode(int);		
		void insertNode(int);		
		void deleteNode(int);		
		void Reverse();			
		void Display() const;		
		int Search(int);		
};
#endif 


Implementation file.
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "LinkedList.h"	

//*****************************************************************
// appendNode appends a node containing the value passed into	  *
// newValue, to the end of the list.				  *
//*****************************************************************
void LinkedList::appendNode(int num)
{
	ListNode *newNode;		
	ListNode *nodePtr;		

	// 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
	{
		// 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;
	}

	Display();
}

//*****************************************************************
// Display shows the value stored in each node of the linked list *
// pointed to by the head.					  *
//*****************************************************************
void LinkedList::Display() const
{
	ListNode *nodePtr;		

	if(!head)
	{
		cout << "\n\tThe list is empty.";
		return;
	}

	// Position nodePtr at the head of list.
	nodePtr = head;

	cout << "\n\n\tThe elements in this list are: \n\t";

	// While nodePtr points to a node, traverse
	// the list.
	while(nodePtr)
	{
		cout << nodePtr -> value << " -> ";
		nodePtr = nodePtr -> next;
	}

	// End of the chain.
	cout << "NULL";
}

//*****************************************************************
// insertNode function inserts a node with newValue copied to its *
// value member.						  *
//*****************************************************************
void LinkedList::insertNode(int num)
{
	ListNode *newNode;			
	ListNode *nodePtr;			
	ListNode *previousNode = NULL;		

	// 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
	{
		nodePtr = head;		
		previousNode = NULL;	

		// Skip all nodes whose value is less than num.
		while(nodePtr != NULL && nodePtr -> value < num)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr -> next;
		}

		// If newNode is to be the first in the list,
		// insert it before all other nodes.
		if(previousNode == NULL)
		{
			head = newNode;
			newNode -> next = nodePtr;
		}
		else
		{
			previousNode -> next = newNode;
			newNode -> next = nodePtr;
		}
	}

	// Display results.
	Display();
}

//*****************************************************************
// Reverse function will re-arrange the nodes in the list.	  *
//*****************************************************************
void LinkedList::Reverse()
{
	ListNode *nodePtr;		
	ListNode *next;			
	ListNode *result = NULL;	

	// If the list is empty, display 
	// message and do nothing.
	if(!head)
	{
		cout << "\n\tThe list is empty.";
		return;
	}

	// Initialize nodePtr to the head of the list.
	nodePtr = head;
	
	// While nodePtr is not at the end of the list...
	while(nodePtr != NULL)
	{
		next = nodePtr -> next;
		nodePtr -> next = result;
		result = nodePtr;
		nodePtr = next;
	}

	// Position result at the head of the list.
	head = result;

	// Display results.
	Display();
}

//*****************************************************************
// The deleteNode function searches for a node with num as its	  *
// value. The node, if found, is deleted from the list and from	  *
// memory.						          *
//*****************************************************************
void LinkedList::deleteNode(int num)
{
	ListNode *nodePtr;		
	ListNode *previousNode;		

	// If the list is empty, display 
	// message and do nothing.
	if(!head)
	{
		cout << "\n\tFailed to delete. List is empty.";
		return;
	}

	// Determine if the first node is the one.
	if(head -> value == num)
	{
		nodePtr = head -> next;
		delete head;
		head = nodePtr;
	}
	else
	{
		// Initialize nodePtr to the head of the 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;
		}
		else
			cout << "\n\tFailed to delete as " << num 
			     << " because it is not found in the list.";
	}

	// Display results.
	Display();
}

//*****************************************************************
// Function Search searches for a value and returns the position  *
// if found. If its not found, -1 is then returned.	          *
//*****************************************************************
int LinkedList::Search(int value)
{
	ListNode *nodePtr;		
	int pos = -1;			
	int location = -1;
	bool found = false;

	// If the list is empty, display 
	// message and do nothing.
	if(!head)
	{
		cout << "\n\tThe list is empty.\n";
		//return -1;
		location = -1;
		found = true;
	}

	// Initialize nodePtr to the head of the list.
	nodePtr = head;

	// While nodePtr points to a node, traverse
	// the list.
	while(nodePtr && !found)
	{
		pos++;	// Increment position.

		// Determine if the first node is the one.
		if(nodePtr -> value == value)
		{
			location = pos;
			found = true;
		}
		else
			nodePtr = nodePtr -> next;
	}

	// Return -1.
	return -1;
}

//*****************************************************************
// Destructor.							  *
// This function deletes every node in the list.	          *
//*****************************************************************
LinkedList::~LinkedList()
{
	ListNode *nodePtr;			
	ListNode *nextNode;			

	// Position nodePtr at the head of the list.
	nodePtr = head;

	// While nodePtr is not at the end of the list...
	while(nodePtr != NULL)
	{
		nextNode = nodePtr -> next;     	
		delete nodePtr;			
		nodePtr = nextNode;		 
	}
}


Main file.
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
#include "LinkedList.h"		

//******************************
// Function main.	       *
//******************************
int main(void)
{
	LinkedList myList;

	char choice;
	int n;

	cout << "\n\n\tProgram to demostrate the LinkedList Class.";

	// do-while loop.
	do
	{
		cout << "\n\n\tAppend a Node: Press A";
		cout << "\n\tInsert a Node: Press I";
		cout << "\n\tDelete a Node: Press D";
		cout << "\n\tReverse a Node: Press R";
		cout << "\n\tSearch a Node: Press S";
		cout << "\n\tQuit: Press Q";

		cout << "\n\n\tEnter Your Choice: ";
		cin >> choice;

		switch(choice)
		{
			case 'A':
			case 'a':
				cout << "\tEnter a Value: ";
				cin >> n;

				myList.appendNode(n);
				break;

			case 'I':
			case 'i':
				cout << "\tEnter a Value: ";
				cin >> n;

				myList.insertNode(n);
				break;

			case 'D':
			case 'd':
				cout << "\tEnter a value: ";
				cin >> n;

				myList.deleteNode(n);
				break;

			case 'R':
			case 'r':
				myList.Reverse();
				break;

			case 'S':
			case 's':
				int position;
				
				cout << "\tEnter Element to be Searched: ";
				cin >> n;

				position = myList.Search(n);

				if(position == 1)
				
				cout << "\tElement Not Found in the List.";
			else
			
			cout << "\tElement Found At: " << position;
			break;

			case 'Q':
			case 'q':
				choice = 'q';
		}
	}while(choice != 'q');
}
Never mind, I fixed it. :)
Topic archived. No new replies allowed.