Linked List

Hello Everyone! I am attempting to write a function that searches a linked list for a value that the user enters. However, the problem I am running into with the program is that after searching the nodes and outputting where the value is in the list it always outputs the wrong place in the list. Any help with this problem would be much appreciated! Thanks! Here is what I have so far:

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
// This program allows the user to enter data into a linked list
// then calls a function that searches the linked list for a number
// that the user wants to find. If it is in linked the list the function 
// then returns a number indicating its position in the list. If it isn't found
// then the function just returns a 0.

#include <iostream>
using namespace std;

struct ListNode {
	float value;
	ListNode *next;
};
ListNode *head = NULL;

float insertNode(float num);
float searchNodes(float value);

int main()
{
	float num, value;
	char answer;
	do {
		cout << "Please enter a value to put in the list --> ";
		cin >> num;
		insertNode(num);
		cout << endl;
		cout << "Would you like to put another value into your list? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

	do {
		cout << "Please enter the value you would like to search for in the list--> ";
		cin >> value;
		searchNodes(value);
		cout << endl;
		cout << "Would you like to search for another value? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

}

float insertNode(float num)
{
	struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}
	newNode->value = num;
	newNode->next = NULL;
	if(head==NULL) {
		cout << "List was empty - " << newNode->value;
		cout << " is part of list's first node.\n";
		head = newNode;
	}
	else {
		while((nodePtr != NULL) && (nodePtr->value < num)) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(prevNodePtr==NULL) {
			newNode->next = head;
			head = newNode;
		}
		else {
			newNode->next = nodePtr; 
			prevNodePtr->next = newNode;
		}
	}
	return 0;
}

float searchNodes(float value)
{
	struct ListNode *nodePtr = head, *prevNodePtr = NULL;
	int count = 0;

	while((nodePtr->next != NULL) && (nodePtr->value != value)) {
		++count;
		prevNodePtr = nodePtr;
		nodePtr = nodePtr->next;
	}

	if (nodePtr->value = value) {
		cout << "Your value was in the list, at position " << count << ".\n";
	}
	else {
		count = 0;
		cout << "The value you have entered was not in the list!\n";
		cout << count;
	}
	
}
Last edited on
bump...please help =[
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
// This program allows the user to enter data into a linked list
// then calls a function that searches the linked list for a number
// that the user wants to find. If it is in linked the list the function 
// then returns a number indicating its position in the list. If it isn't found
// then the function just returns a 0.

#include <iostream>
using namespace std;

struct ListNode {
	float value;
	ListNode *next;
};
ListNode *head = NULL;

float insertNode(float num);
float searchNodes(float value);

//don't forget to free allocated memory when you're done with it! 
void clearList();

int main()
{
	float num, value;
	char answer;
	do {
		cout << "Please enter a value to put in the list --> ";
		cin >> num;
		insertNode(num);
		cout << endl;
		cout << "Would you like to put another value into your list? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

	do {
		cout << "Please enter the value you would like to search for in the list--> ";
		cin >> value;
		searchNodes(value);
		cout << endl;
		cout << "Would you like to search for another value? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

    clearList();
}

float insertNode(float num)
{
	struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}
	newNode->value = num;
	newNode->next = NULL;
	if(head==NULL) {
		cout << "List was empty - " << newNode->value;
		cout << " is part of list's first node.\n";
		head = newNode;
	}
	else {
        //*************************************** 
        //why are you doing this so complicated?*
        //***************************************
        
		/*while((nodePtr != NULL) && (nodePtr->value < num)) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(prevNodePtr==NULL) {
			newNode->next = head;
			head = newNode;
		}
		else {
			newNode->next = nodePtr; 
			prevNodePtr->next = newNode;
		}*/
		
		//*********************
                //this would be enough*
		//*********************
		
		while (nodePtr->next) nodePtr=nodePtr->next;
		nodePtr->next=newNode;
	}
	return 0;
}

float searchNodes(float value)
{
	struct ListNode *nodePtr = head, *prevNodePtr = NULL;
	int count = 0;

    //****************************
    //this is complicated too!...*
    //****************************
    
	/*while((nodePtr->next != NULL) && (nodePtr->value != value)) {
		++count;
		prevNodePtr = nodePtr;
		nodePtr = nodePtr->next;
	}

	if (nodePtr->value = value) {
		cout << "Your value was in the list, at position " << count << ".\n";
	}
	else {
		count = 0;
		cout << "The value you have entered was not in the list!\n";
		cout << count;
	}*/
	
	//*********************
	//try this one instead*
	//*********************
	
	while (nodePtr)
	{
          //be careful, test equality with '==' and not '='
          if (nodePtr->value==value) 
             cout << "value found at pos " << count << endl;
             
          nodePtr=nodePtr->next;
          count++;
    }
}

void clearList()
{
     struct ListNode * nodePtr=head;
     
     while (head) {head=head->next; delete nodePtr; nodePtr=head;}
}
Last edited on
When I use your search function after inputting a value to search for in the list it just continues to keep asking me to input a value to search for. It doesn't tell me if it is in the list or not...
Nevermind I made a mistake...thanks for all the help master roshi.
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
// This program allows the user to enter data into a linked list
// then calls a function that searches the linked list for a number
// that the user wants to find. If it is in linked the list the function 
// then returns a number indicating its position in the list. If it isn't found
// then the function just returns a 0.

#include <iostream>
using namespace std;

struct ListNode {
	float value;
	ListNode *next;
};
ListNode *head = NULL;

float insertNode(float num);
float searchNodes(float value);

//don't forget to free allocated memory when you're done with it! 
void clearList();

int main()
{
	float num, value;
	char answer;
	do {
		cout << "Please enter a value to put in the list --> ";
		cin >> num;
		insertNode(num);
		cout << endl;
		cout << "Would you like to put another value into your list? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

	do {
		cout << "Please enter the value you would like to search for in the list--> ";
		cin >> value;
		searchNodes(value);
		cout << endl;
		cout << "Would you like to search for another value? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

    clearList();
}

float insertNode(float num)
{
	struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}
	newNode->value = num;
	newNode->next = NULL;
	if(head==NULL) {
		cout << "List was empty - " << newNode->value;
		cout << " is part of list's first node.\n";
		head = newNode;
	}
	else {
        //*************************************** 
        //why are you doing this so complicated?*
        //***************************************
        
		/*while((nodePtr != NULL) && (nodePtr->value < num)) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(prevNodePtr==NULL) {
			newNode->next = head;
			head = newNode;
		}
		else {
			newNode->next = nodePtr; 
			prevNodePtr->next = newNode;
		}*/
		
		//*********************
                //this would be enough*
		//*********************
		
		while (nodePtr->next) nodePtr=nodePtr->next;
		nodePtr->next=newNode;
	}
	return 0;
}

float searchNodes(float value)
{
	struct ListNode *nodePtr = head, *prevNodePtr = NULL;
	int count = 0;

    //****************************
    //this is complicated too!...*
    //****************************
    
	/*while((nodePtr->next != NULL) && (nodePtr->value != value)) {
		++count;
		prevNodePtr = nodePtr;
		nodePtr = nodePtr->next;
	}

	if (nodePtr->value = value) {
		cout << "Your value was in the list, at position " << count << ".\n";
	}
	else {
		count = 0;
		cout << "The value you have entered was not in the list!\n";
		cout << count;
	}*/
	
	//*********************
	//try this one instead*
	//*********************
	
	int times_found=0;
	while (nodePtr)
	{
          //be careful, test equality with '==' and not '='
          if (nodePtr->value==value) 
          {
             cout << "value found at pos " << count << endl;
             times_found++;
          }
             
          nodePtr=nodePtr->next;
          count++;
    }
    
    if (times_found==0) cout << "value not found on the list" << endl;
    else cout << "value found a total of " << times_found << " time(s)" << endl;
}

void clearList()
{
     struct ListNode * nodePtr=head;
     
     while (head) {head=head->next; delete nodePtr; nodePtr=head;}
}
Thanks for all the help m4ster r0shi!
Topic archived. No new replies allowed.