LINKED LIST FUNCTIONS IN HEADER

So I want to implement three different functions into my header file. I am working on the first one but I am unsure where to begin. Here is my current header file:

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#ifndef LINKLIST_H
#define LINKLIST_H

class NumberList
{
	private:
		//Declare a structure for the list 
		struct ListNode
		{
			int value;			 //The value in this node
			struct ListNode *next;	//The values in the next node
		};
		
		ListNode *head; 
	
	public:
		//Constructor
		NumberList()
		{
			head = NULL;
		}
		
		//Destructor
		~NumberList();
		
		//Link list functions
		void appendNode(int);
		void insertNode(int);
		void deleteNode(int);
		void displayList() const;
		void deleteNodeAll(int);
		bool listSorted() const;
		void deleteList();
		void listAddEvenMultiplyOdd();		//Implement this function
		void listOddPositions();			//Implement this function
		void listDivisibleBy5or10();		//Implement this function		
		
};

NumberList::~NumberList()
{
	ListNode *nodePtr;	//to traverse the list
	ListNode *nextNode; // to point to the next node
	
	//Position nodePtr at the head of the list
	nodePtr = head;
	
	//while the node pointer is not at the end of the list
	while(nodePtr != NULL)
	{
		//save a pointer to the next node
		nextNode = nodePtr -> next;
		
		// Delete the current Node
		delete nodePtr;
		
		//position nodePtr at the next node
		nodePtr = nextNode; 
	}
	
	
}


void NumberList::appendNode(int num)
{
	ListNode *newNode;	//To point to a new node
	ListNode *nodePtr;	//move through the list
	
	//Allocate a new node  and store num there
	newNode = new ListNode;
	newNode -> value = num;
	newNode -> next = NULL;
	
	
	//If there are no nodes in the list 
	//make newNode the first node.
	if(!head)
		head = newNode;
	else	//insert newNode at the end
	{
		// Intialize NodePtr to head of the list
		nodePtr = head;
		
		//Find the last node in the list
		while ( nodePtr->next)
			nodePtr= nodePtr -> next;
		//insert newNode as the last node
		nodePtr -> next= newNode;
		
	}
}


void NumberList::displayList() const
{
	ListNode *nodePtr;	//To move through the list
	
	//Position nodePtr at the head of the list
	nodePtr = head;
	
	//while nodePtr points to a node, traverse the list
	while (nodePtr)
	{
		//Display the value in this node
		std::cout<< nodePtr->value <<"   ";
		
		//Move to the next node
		nodePtr = nodePtr -> next;
	}
	std::cout<< std::endl;
}



void NumberList::deleteNode(int num)
{
	ListNode *nodePtr;	//To traverse the list
	ListNode *previousNode;	//To point to the previous node 
	
	//If the list is empty, do nothing
	if(!head)
		return;
	
	//Determine if the first node is the one to be deleted
	if(head->value == num)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}
	else
	{
		//Intialize nodePtr to head of list
		nodePtr = head;
		
		//Skip all nodes whose value member is not equal to num
		while (nodePtr != NULL && nodePtr->value != num)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}
		
		//If nodePtr is not at the end of the list, link the previous 
		// node to the node after nodePtr, then delete nodePtr
		if(nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
		
	}

}

void NumberList::insertNode(int num)
{
	ListNode *newNode;	//A new Node
	ListNode *nodePtr;	//To traverse the list
	ListNode *previousNode = NULL;	//The previous node
	
	//Allocate a new node and store num there
	newNode = new ListNode;
	newNode->value = num;
	newNode->next = NULL;
	
	//If there are no nodes in the list make newNode the first node
	if(!head)
	{
		head = newNode;

	}
	
	else //otherwise, insert newNode
	{
		//Position modePtr at the head of the list
		nodePtr = head;
		
		//Intialize previousNode to NULL
		previousNode = NULL;
		
		//Skip all nodes whose value is less than num
		while(nodePtr != NULL && nodePtr->value <num)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}
		
		//If the new node is to be 1st in the list, Insert it before all other nodes.
		if(previousNode == NULL)
		{
			head = newNode;
			newNode->next = nodePtr;
			
		}
		
		else
		{
			previousNode->next = newNode;
			newNode->next = nodePtr;
		}
	}
}



void NumberList::deleteNodeAll(int num)
{
	ListNode *nodePtr;	//To traverse the list
	ListNode *previousNode;	//To point to the previous node 
	
	//If the list is empty, do nothing
	if(!head)
		return;
	
	//Determine if the first node is the one to be deleted
	if(head->value == num)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}

	//Intialize nodePtr to head of list
	nodePtr = head;
	
	while(nodePtr !=NULL)
	{
	
		//Skip all nodes whose value member is not equal to num
		while (nodePtr !=NULL && nodePtr->value != num)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}
		
		//If nodePtr is not at the end of the list, link the previous 
		// node to the node after nodePtr, then delete nodePtr
		if(nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
			nodePtr= previousNode->next;
		}
		
	
	}	
}

bool NumberList::listSorted() const
{
	ListNode *nodePtr;	//To traverse the list
	ListNode *previousNode;	//To point to the previous node 
	
	//If the list is empty, do nothing
	if(!head)
		return true;
	

	if(head->next ==NULL)
		return true;
	//Intialize nodePtr to head of list
	nodePtr = head->next;
	previousNode = head;

	
	//Skip all nodes whose value member is not equal to num
	while (nodePtr !=NULL && previousNode->value < nodePtr->value )
	{
		previousNode = nodePtr;
		nodePtr = nodePtr->next;
	}
	
	//If nodePtr is not at the end of the list, link the previous 
	// node to the node after nodePtr, then delete nodePtr
	if(nodePtr)
	{
		return false;
	}
	
	else
		return true;
		
	
}



void NumberList::deleteList()
{

	ListNode *nodePtr;	//To traverse the list
	ListNode *nextNode;	//To point to the previous node 
	
	//If the list is empty, do nothing
	if(!head)
		return;
	

	if(head->next ==NULL)
	{
	
		delete head;
		head = NULL;
		return;
	}
	//Intialize nodePtr to head of list
	nodePtr = head;

	
	//Skip all nodes whose value member is not equal to num
	while (nodePtr !=NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;
	}
	
	head=NULL;
	return;
	
	
}


How do I actually begin getting somewhere with the first of the three functions?
1
2
3
4
5
6
7
8
void listAddEvenMultiplyOdd();
{


}

return;
}
Last edited on

CodingIsHard17 wrote:
How do I actually begin getting somewhere with the first of the three functions?
listAddEvenMultiplyOdd


Perhaps you could start by telling us exactly what a function with the name
listAddEvenMultiplyOdd()
is supposed to do ... and with what.

Remote guess ... add up the even numbers and multiply together the odd? Well you have written a function void NumberList::displayList() const to traverse the list (haven't you?). If my wild guess is correct you could modify that to add up the even numbers and multiply the odd numbers as you went along ... if, of course, that was what you intended to do.

BTW, your routines aren't templated, so there's no reason for them to be in a header file.
Last edited on
It’s a weird name but that’s what he came up with.

The function will add 4 to all the even values in the link list and multiply all the odd values by 5.
Last edited on
If you're not doing homework, you shouldn't be writing your own linked list; no one does that in C++. Use the one from the standard library. In this case it can be:
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
#include <list>
#include <numeric>
#include <iostream>

template <typename T>
bool even(T n) {
    return n%2 == 0;
}

class WeirdGame {
    std::list<int> nums_;

public:
    void add(int n) { nums_.push_back(n); }
    int addEven() const {
        return std::accumulate(
                    nums_.cbegin(), nums_.cend(), 0,
                    [](int sum, int n){ return even(n) ? (sum + n) : sum; });
    }
};

int main() {
    WeirdGame game;

    game.add(1);
    game.add(2);
    game.add(3);
    game.add(4);
    std::cout << game.addEven() << std::endl;
}

std::accumulate is a libary function that "sums" stuff. We use it to add numbers only if we encounteer an even one.

The code is terse, but there's a lot less of it, and it's produced much more quickly and reliably.
Last edited on
kbw:
If you're not doing homework, you shouldn't be writing your own linked list; no one does that in C++. Use the one from the standard library. In this case it can be:


I made a mistake. My professor uploaded this like that. I just have to add three different functions to it. I am working on the first function right now. This is what I have so far but it doesn't work with the source file:
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
void listAddEvenMultiplyOdd()
{
	int ListNode *currentNode;	//Pointer for current value in the link list
	int ListNode *nextNode;	// Pointer for the next value in the link list
	int listOdd =0;		
	int listEven = 0;
	
	if(isEmpty()) // checks for an empty link list
	{
		std::cout<<"The Link list is empty \n"; // tells user that the link list is empty
	}
	
	else
	{
	
		currentNode = top;
		while(currentNode!= NULL)
		{
			if(currentNode->value%2 == 0)
			{
				listEven += 4;
			}
			if(currentNode->value%2 == 1)
			{
				listOdd *= 5;
			}
			currentNode = currentNode->next; // shows that the node 
                                                          // after the current node is next
		
		}		
	}
	std::cout<<listOdd <<"\n";	
	std::cout<< listEven <<"\n";
}


Any help with trying to fix it?
Last edited on
Your instructor wrote that shitty code? Yikes!

Anyway, what do you think multiplying 0 by 5 a bunch of times is going to accomplish?
You said you wanted to add 4 to even values and multiply odd values by 5.
Obviously you aren't doing that.
1. Line 1 is defining a non-member function. You need to add the class scope to the function name to make it a member function.

2. listOdd will never contain anything because you are constantly multiplying by 0. Change the initializer in line 5 to '1'.

3. isEmpty is not defined anywhere.

4. "top" is not defined anywhere.

5. I don't think you need nextNode (line 4).
That's what he wrote and that's the tasks he assigned. He wants us to add four to the even values and multiply the odd values by 5. I don't know what my code is doing currently. That's why I am asking for guidance.
You don't need listEven or listOdd at all. You are adding 4 to, or multiplying by 5, the variable currentNode->value. Simply traverse the list (roughly in the way you are doing) and change each currentNode->value as you get to it. No idea why you wrote "top": it's called "head".

BTW, where were 4 and 5 in your original question? You sent several of us completely the wrong way by failing to state that and leaving us to guess from the name of the function.

Make sure that you address all of doug4's points (except possibly his number 2, now you have actually explained the task).
Last edited on
I want to reiterate something that @lastchance stated much earlier and may not have been addressed:

BTW, your routines aren't templated, so there's no reason for them to be in a header file.


I would go so far as to say that is is actually WRONG to put them in a header file. They absolutely should be in a .cpp file.
@lastchance:
I just got that 4 and 5 clarified because I was a little confused myself.

@doug4:
My professor wants it in a header file that's why they're in a header file and not a .cpp file. It is automatically a fail if I put it in a .cpp file. Thank you. I will try to figure out the function.

How would I begin the function to find numbers in the odd placements on the list and multiply those values by 10?
What I have for my function currently:
1
2
3
4
5
6
7
8
9
10
11
12
void listOddPositions()
{
	int arr[i];
	int size;
    for(int i = 1;i<=size;i++)
	{
        if(i%2==1)
		{
            arr[i-1] *= 10;
        }        
    }
}


Why won't it run?

My errors are:
[Error] 'i' was not declared in this scope
[Error] 'arr' was not declared in this scope
Where are you declaring i outside of your for loop?

The two errors are related. Since i is undeclared, trying to declare the array will fail.

What is the value of size? You are using an uninitialized variable in the for loop. That will likely blow well past the array's boundary.

You are creating an array that is local to your function. If you are expecting to use that function outside that function, you can't.
Last edited on
I'm stuck -- this is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void NumberList::listOddPositions(struct ListNode *head)
{
	if(!head) // checks for an empty link list
	{
		std::cout<<"The list is empty \n"; // tells user that the list is empty
	}
   struct ListNode *temp=head;
   while(temp!=NULL)
   {
       temp->data = temp->data*10; //multiply values at odd position by 10
       temp = temp->next;
       //skip odd position values
       if(temp!= NULL)
       temp = temp->next;
   }
}
Last edited on
Where are you assigning a value to size? Line 14 is using an uninitialized variable.

You keep wanting to shotgun change what you posted, I'll stop trying to help.
Last edited on
My new function is different. I scrapped it and rewrote the entire thing. I'm just getting this error when I run the demo file.

[Error] prototype for 'void NumberList::listOddPositions(NumberList::ListNode*)' does not match any in class 'NumberList'
If you see the time. I edited it at 8:13 and your response came at 8:14 so I don't know where the confusion came in at. If you don't want to help then fine. Hopefully someone else will be willing to help on a forum that people are supposed to be able to come to for help.
If you see the time. I edited it at 8:13 and your response came at 8:14 so I don't know where the confusion came in at.


Because @Furry Guy probably started writing his response at 8:12. You ninja'd him by changing your code while he was responding. Now his response doesn't make any sense.
Topic archived. No new replies allowed.