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:
// 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>
usingnamespace 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;
}
}
// 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>
usingnamespace 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;}
}
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...
// 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>
usingnamespace 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;}
}