-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.cpp
More file actions
54 lines (45 loc) · 1.3 KB
/
Copy pathsplit.cpp
File metadata and controls
54 lines (45 loc) · 1.3 KB
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
/*
CSCI 104: Homework 1 Problem 1
Write a recursive function to split a sorted singly-linked
list into two sorted linked lists, where one has the even
numbers and the other contains the odd numbers. Students
will receive no credit for non-recursive solutions.
To test your program write a separate .cpp file and #include
split.h. **Do NOT add main() to this file**. When you submit
the function below should be the only one in this file.
*/
#include "split.h"
#include <cstddef>
/* Add a prototype for a helper function here if you need */
void addNode(Node*& list, Node*& num);
void split(Node*& in, Node*& odds, Node*& evens)
{
/* Add code here */
// WRITE YOUR CODE HERE
// mod to see if even/odd
// add to correct list
//in: 1, 2, 3
if (in == NULL) {
return;
}
Node* rest = in->next; //save the next for recursive call
in->next = NULL; //remove
if (in->value % 2 == 0) {
addNode(evens, in);
}
else {
addNode(odds, in);
}
split (rest, odds, evens);
in = NULL; //don't save original
}
/* If you needed a helper function, write it here */
void addNode(Node*& list, Node*& num) {
if (list == NULL) {
//replace list with the num
list = num;
return;
}
//if not null, we haven't reached end of the list so keep moving until the end
addNode(list->next, num);
}