displaying linklist

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

#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);
	}	
	void getHead() {return ListNode* head;}
	bool insertNode(int num);
	void displayList(void);
	bool apendinglist (int num);
	bool deleteNode(int num);
	int indexOf (int value);
};
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;
	  mylist.indexOf (dummy2);

	
}

bool linklist::insertNode(int num)
// This function inserts a node into our linked list.
{
	ListNode* newNode;
	ListNode* nodePtr = head;
	ListNode* 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) { // bad programming practice but i don't care =D
		cout<<"your list is ";
		
		head = newNode;
	}
	else {
		while(nodePtr != NULL && nodePtr->value<num) {
			prevNodePtr = nodePtr;  // advance prevNodePtr up to nodePtr
			nodePtr = nodePtr->next;  // advance nodePtr by 1 node
		}
		if(prevNodePtr == NULL) {// at beginning of list
			newNode->next = head;
			head = newNode;
		}
		else {
			prevNodePtr->next = newNode;
			newNode->next = nodePtr;
		}
	}

	return 0;
}

void linklist::displayList(void)
// This function displays our linked list!
{
	ListNode* nodePtr = head;
	

	if(head == NULL) {
		cout << "List is currently empty!\n";
	}
	else {
		while (nodePtr != NULL) {
			cout << nodePtr->value <<"\t";
			nodePtr = nodePtr->next;
		}
	}
}

bool linklist::deleteNode(int num)
// This function deletes a node, if it is in face there.
{
	ListNode* nodePtr = head;
	ListNode* prevNodePtr = NULL;
	
	if(head==NULL) { // list is currently empty
		cout << "The list is currently empty - ";
		cout << "There are no nodes to delete!\n";
	}
	else if(nodePtr->value == num) { // node is first one
		head = nodePtr->next;
		delete nodePtr;
	}
	else {
		while(nodePtr != NULL && nodePtr->value != num) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(nodePtr == NULL) {
			cout << "Your value (i.e., " << num << ")";
			cout << " was not in the list!\n";
		}
		else {
			prevNodePtr->next = nodePtr->next;
			delete nodePtr;
		}
	}
	
	return 0;
}

bool linklist::apendinglist(int num)
// This function inserts a node into our linked list.
{
	ListNode* newNode;
	ListNode* nodePtr = head;
	ListNode* 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) { // check if list is currently empty
		cout << "List is currently empty - " << newNode->value;
		cout << " is part of the list's first node!\n";
		head = newNode;
	}
	else 
	{
		while(nodePtr->next != NULL)
			nodePtr=nodePtr->next;
		nodePtr->next = newNode;
	}
	return 0;

	
}

void linklist::ListNode::recursiveDelete(ListNode *node)
{
	if( node->next != NULL )
		recursiveDelete(node->next);
	delete node;
}

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.
		cout<<i;
	}
	return -1; // not found.
}


As you see i am letting the user type 10 ints and store it in a link list, i then display the linklist in ascending order (and so far my program works). NOw i want to modify my program to let the user promt an int, and if that int belongs to that link list then it will display the position of that number, starting head = 0.

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).

Now i got help from other posts and here's my problems
on line 25, my getHead function is invalid
line 188 mylist is invalid (although i declare it in main).
Your getHead function returns void; that is, no return. Yet you then try to return something.

Note that when you return an object, it is unusual to declare its type as well.

That is,

1
2
3
4
5
return i; // good
return int i; // a bit odd

return head; //good
return ListNode* head; //a bit odd 


line 188; you are calling a member function of your mylist object. It can see the getHead function because that is another member function within that object.

Try

ListNode* itr = getHead(); // you will need to create this function.
Last edited on
Hi, your suggestions
return i; // good
return int i; // a bit odd

return head; //good
return ListNode* head; //a bit odd
i tried it before, and it doesn't work, there's a red line under it, is it because i said it's void?

note that i am talking about line 25,
and what is int i? i don't think i declare it.
In short what is the correct getHead declaration? for line 25
return int i was a simple example to show you something that you shouldn't do.
return ListNode* head; is a slightly more complicated example to show you something you shouldn't do.

You should replace the void on line 25 with the kind of object you are returning. Look at line 25, work out what kind of object you are returning (i.e. what kind of object head is), and put that where void is.
Last edited on
haha, that is what i'm asking i know it is not "void" i tried int, float, double, bla bla bla but the problem is the return statment, no matter what i return it'still underlind with red.

to clear things up these are the thigns i tried

void getHead() {return ListNode* head;}
void getHead() {return head;}
void getHead() {return *head;}
ListNode* getHead() {return ListNode* head;}
ListNode* getHead() {return head;}
ListNode* getHead() {return *head;}
linklist *getHead() {return ListNode* head;}
etc...
Last edited on
closed account (D80DSL3A)
ListNode* getHead() {return head;} = Bingo!
For line 188 try ListNode* itr = getHead();
Or try ListNode* itr = head; because private class members are accessible in the class member functions (ie. you didn't need getHead() after all).
HOLD ON A SEC something is really worng here, fun2code you did solve my problem but my code is not doing what i want, for the data i type 1 2 3 4 5 6 7 8 9 0

well so the code will display 0 1 2 3 4 5 6 7 8 9
then the code will ask you "please enter the value you wish to search" then i type 4
then it display 1234 )'=
closed account (D80DSL3A)
Yes, the list will display "backwards". That's how it's supposed to work.
Remember, the 1st value entered becomes the last value in the list while the last value entered becomes the head (or 1st) value in the list.

then it display 1234

That's what line 192 will do.
If you want to see only the index where the value was found then simply display the functions return value. Replace line 54 with: cout << mylist.indexOf(dummy2); and remove line 192.
Topic archived. No new replies allowed.