-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllrec-test.cpp
More file actions
113 lines (90 loc) · 2.09 KB
/
Copy pathllrec-test.cpp
File metadata and controls
113 lines (90 loc) · 2.09 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
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
#include <iostream>
#include <fstream>
#include <functional>
#include "llrec.h"
using namespace std;
/**
* Reads integers (separated by whitespace) from a file
* into a linked list.
*
* @param[in] filename
* The name of the file containing the data to read
* @return
* Pointer to the linked list (or NULL if empty or the
* file is invalid)
*/
Node* readList(const char* filename);
/**
* Prints the integers in a linked list pointed to
* by head.
*/
void print(Node* head);
/**
* Deallocates the linked list nodes
*/
void dealloc(Node* head);
Node* readList(const char* filename)
{
Node* h = NULL;
ifstream ifile(filename);
int v;
if( ! (ifile >> v) ) return h;
h = new Node(v, NULL);
Node *t = h;
while ( ifile >> v ) {
t->next = new Node(v, NULL);
t = t->next;
}
return h;
}
void print(Node* head)
{
while(head) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
void dealloc(Node* head)
{
Node* temp;
while(head) {
temp = head->next;
delete head;
head = temp;
}
}
// -----------------------------------------------
// Add any helper functions or
// function object struct declarations
// -----------------------------------------------
int main(int argc, char* argv[])
{
if(argc < 2) {
cout << "Please provide an input file" << endl;
return 1;
}
// -----------------------------------------------
// Feel free to update any code below this point
// -----------------------------------------------
Node* head = readList(argv[1]);
cout << "Original list: ";
print(head);
Node* smaller = nullptr;
Node* larger = nullptr;
llpivot(head, smaller, larger, 10);
cout << "Smaller: ";
print(smaller);
cout << "Larger: ";
print(larger);
struct IsOdd {
bool operator()(int val) { return val % 2 == 1; }
};
IsOdd isOdd;
Node* filtered = llfilter(smaller, isOdd);
cout << "Filtered (no odd): ";
print(filtered);
dealloc(larger);
dealloc(filtered);
return 0;
}