I am currently working on a problem that requires me to search a user entered linked list for a value that the user asks for. So far I have written most of the code but I am having trouble figuring out how to write the code for the function that searches the linked list for the value and prints out its position in the list. If anyone could help me with this that would be great! 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;
int main()
{
int 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(num);
cout << endl;
cout << "Would you like to search for another value? ";
cin >> answer;
} while(toupper(answer)=='Y');
}
int 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;
}
int searchNodes(int num)
{
// No idea...
}
Search_Node(int search)
{
int ctr=0,f=0;
curr=head;
while(curr->next!=NULL)
{
ctr++;
if(curr->data==search)
{
cout<<"Value Found !,and is located at position "<<ctr<<" ...";
f=1;
}
curr=curr->next;
}
if(f==0)
{
cout<<"\nNumber not Found!";
}
return 0;
}
1) The STL has an already implemented doubly-linked list with full C++ capabilities. This, by chance, is called the std::list.
2) All C++ headers do not use an extension. The header names you used qtpan, are wrong.
3) If you are set to implementing your own list, I suggest you look up templates since they are very effective here.
4) I really don't suggest you use conio.h. It's platform specific. At that point, I would switch to pdcurses or something similar that has multiplatform capabilities.
5) I would suggest you read this article based on how to properly take in different variables using streams: http://cplusplus.com/forum/articles/6046/
EDIT 6) It's not healthy to use usingnamespace std;. Instead, either use the std:: label or specify specific parts of the std namespace that you use, not all of it.