searching in link list

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
#include <iostream>

using namespace std;

const short unsigned int NUMVALS = 10;

class linklist
{
private:
	struct ListNode
	{
		int value;
		ListNode* next;
		static void 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];
	}
	short unsigned int 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.
}
sh1t i am really bad a doing the set/get in class

i suppose in public
gethead(void) {return head;}?
This should return ListNode* but I think it should work. Try testing it...
what does itr means?
It's short for iterator.
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 about indexof?
Ok i finish testing here's my results

when i am setting my get function inpublic

(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
ListNode* getHead() { return *head; } And regarding the second line, this should be just getHead(), I forgot it's inside the class.

EDIT: Please disregard, I see you continued your program in the other thread http://cplusplus.com/forum/beginner/41440/ also solving this problem.
Last edited on
Topic archived. No new replies allowed.