#include <iostream>
usingnamespace std;
constshortunsignedint NUMVALS = 10;
class linklist
{
private:
struct ListNode
{
int value;
ListNode* next;
staticvoid recursiveDelete(ListNode *node);
};
ListNode *head;
public:
linklist(void) {head = NULL;}
~linklist() {
if( head != NULL )
ListNode::recursiveDelete(head);
}
bool insertNode(int num);
void displayList(void);
bool apendinglist (int num);
bool deleteNode(int num);
};
int main(void)
{
linklist mylist;
int dumVals[NUMVALS];
for (int i = 0; i<NUMVALS; i++)
{
cout<<"please enter your "<<i+1<<" number in the list data ";
cin>>dumVals[i];
}
shortunsignedint i;
for(i=0; i<NUMVALS; i++) { // insert the array values into the linked list
if(mylist.insertNode(dumVals[i])) {
cout << "Error in function insertNode!\n";
cout << "Program will halt!\n";
exit(1);
}
}
mylist.displayList();
int dummy2;
cout<<"please enter the value that you wish to search";
cin>>dummy2;
}
ass you see this code make the user enter ten ints store it in a link list and display it in ascending order. (i did not post the complete program but it works). Anyways my problem is
1 2 3
int dummy2;
cout<<"please enter the value that you wish to search";
cin>>dummy2;
I want the user to input a value and a functino that checks whether the value is in the list or not, if it is in the list it will display it's position.
E.g user input 1 2 3 4 5 6 7 8 9 10 as the initial ten value, then it will ask the user to "please enter the value that you wish to search" the user type in 2, the it will display 2 is in position 2(position 1 for comptuer code but i will add 1 to it).
If you guys require me to post the complete code to asnwer my question i will post it.
Since head is private you should have a member function to access the head. After you do that, I would suggest something like (hopefully that would work):
1 2 3 4 5 6 7 8
int linklist::indexOf (int value) {
ListNode* itr = mylist.getHead(); // you will need to create this function.
for (int i = 1; itr != NULL; i++, itr = itr->next) {
if (itr->value == value)
return i; // index of the value you're searching for.
}
return -1; // not found.
}
It's a supporting pointer (iterator) which is like an index of an element in an array, advancing to the next node every time. This way itr passes all the elements in the list.
(what is the identifier, i tried void, int, float, double etc... ) gethead() {return *head;}
it also doesn't let me do *head or Listnode *head or head.
lastly
line 2 in your code you said mylist.gethead(), i assume you mean linkList