-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListAssignment.cpp
More file actions
455 lines (364 loc) · 10.8 KB
/
LinkedListAssignment.cpp
File metadata and controls
455 lines (364 loc) · 10.8 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//============================================================================
// Name : LinkedList.cpp
// Author : Garret Gherardini
// Version : 1.0
// Copyright : Copyright © 2017 SNHU COCE
// Description : Lab 3-3 Lists and Searching
//============================================================================
#include <algorithm>
#include <iostream>
#include <time.h>
#include "CSVparser.hpp"
using namespace std;
//============================================================================
// Global definitions visible to all methods and classes
//============================================================================
// forward declarations
double strToDouble(string str, char ch);
// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
};
//============================================================================
// Linked-List class definition
//============================================================================
/**
* Define a class containing data members and methods to
* implement a linked-list.
*/
class LinkedList {
private:
//Internal structure for list entries, housekeeping variables
struct Node {
Bid bid;
Node *next;
// default constructor
Node() {
next = nullptr;
}
// initialize with a bid
Node(Bid aBid) {
bid = aBid;
next = nullptr;
}
};
Node* head;
Node* tail;
int size = 0;
public:
LinkedList();
virtual ~LinkedList();
void Append(Bid bid);
void Prepend(Bid bid);
void PrintList();
void Remove(string bidId);
Bid Search(string bidId);
int Size();
};
/**
* Default constructor
*/
LinkedList::LinkedList() {
// FIXME (1): Initialize housekeeping variables
//set head and tail equal to null
head = nullptr;
tail = nullptr;
size = 0;
}
/**
* Destructor
*/
LinkedList::~LinkedList() {
// start at the head
Node* current = head;
Node* temp;
// loop over each node, detach from list then delete
while (current != nullptr) {
temp = current; // hang on to current node
current = current->next; // make current the next node
delete temp; // delete the orphan node
}
}
/**
* Append a new bid to the end of the list
*/
void LinkedList::Append(Bid bid) {
// FIXME (2): Implement append logic
//Create new node
Node* newNode = new Node(bid); // new node is added to end of list
//if there is nothing at the head...
// new node becomes the head and the tail
if (head == nullptr) { // IF list is empty; head & tail pointers set to new node
head = newNode;
tail = newNode;
}
//else
// make current tail node point to the new node
// and tail becomes the new node
else { // ELSE list is not empty; tail pointer's next pointer set to new node
tail->next = newNode;
tail = newNode;
}
//increase size count
size++;
}
/**
* Prepend a new bid to the start of the list
*/
void LinkedList::Prepend(Bid bid) {
// FIXME (3): Implement prepend logic
// Create new node
Node* newNode = new Node(bid); // new node is added to beginning of list
// if there is already something at the head...
// new node points to current head as its next node
if (head == nullptr) { // IF list is empty; head & tail pointers set to new node
head = newNode;
tail = newNode;
}
// head now becomes the new node
else { // ELSE list is not empty; head pointer's next pointer set to new node
newNode->next = head;
head = newNode;
}
//increase size count
size++;
}
/**
* Simple output of all bids in the list
*/
void LinkedList::PrintList() {
// FIXME (4): Implement print logic
// start at the head
Node* current = head;
// while loop over each node looking for a match
while (current != nullptr) {
//output current bidID, title, amount and fund
displayBid(current->bid);
//set current equal to next
current = current->next;
}
}
/**
* Remove a specified bid
*
* @param bidId The bid id to remove from the list
*/
void LinkedList::Remove(string bidId) {
// FIXME (5): Implement remove logic
Node* current = head;
Node* temp = nullptr;
// special case if matching node is the head
if (current->bid.bidId == bidId) {
// make head point to the next node in the list
head = current->next;
delete current;
//decrease size count
size--;
//return
return;
}
// start at the head
// while loop over each node looking for a match
while (current->next != nullptr) {
// if the next node bidID is equal to the current bidID
if (current->next->bid.bidId == bidId) {
// hold onto the next node temporarily
temp = current->next;
// make current node point beyond the next node
cur->next = temp->next;
// now free up memory held by temp
delete temp;
// decrease size count
size--;
//return
return;
}
// current node is equal to next node
current = current->next;
}
}
/**
* Search for the specified bidId
*
* @param bidId The bid id to search for
*/
Bid LinkedList::Search(string bidId) {
// FIXME (6): Implement search logic
// special case if matching node is the head
Node* current = head;
// make head point to the next node in the list
head = current->next;
delete current;
//decrease size count
size--;
//return
return;
// start at the head of the list
// keep searching until end reached with while loop (next != nullptr
while (current != nullptr) {
// if the current node matches, return it
if (current->bid.bidId == bidId) {
// else current node is equal to next node
else {
current = current->next;
}
//return bid
return Bid();
}
/**
* Returns the current size (number of elements) in the list
*/
int LinkedList::Size() {
return size;
}
//============================================================================
// Static methods used for testing
//============================================================================
/**
* Display the bid information
*
* @param bid struct containing the bid info
*/
void displayBid(Bid bid) {
cout << bid.bidId << ": " << bid.title << " | " << bid.amount
<< " | " << bid.fund << endl;
return;
}
/**
* Prompt user for bid information
*
* @return Bid struct containing the bid info
*/
Bid getBid() {
Bid bid;
cout << "Enter Id: ";
cin.ignore();
getline(cin, bid.bidId);
cout << "Enter title: ";
getline(cin, bid.title);
cout << "Enter fund: ";
cin >> bid.fund;
cout << "Enter amount: ";
cin.ignore();
string strAmount;
getline(cin, strAmount);
bid.amount = strToDouble(strAmount, '$');
return bid;
}
/**
* Load a CSV file containing bids into a LinkedList
*
* @return a LinkedList containing all the bids read
*/
void loadBids(string csvPath, LinkedList *list) {
cout << "Loading CSV file " << csvPath << endl;
// initialize the CSV Parser
csv::Parser file = csv::Parser(csvPath);
try {
// loop to read rows of a CSV file
for (int i = 0; i < file.rowCount(); i++) {
// initialize a bid using data from current row (i)
Bid bid;
bid.bidId = file[i][1];
bid.title = file[i][0];
bid.fund = file[i][8];
bid.amount = strToDouble(file[i][4], '$');
//cout << bid.bidId << ": " << bid.title << " | " << bid.fund << " | " << bid.amount << endl;
// add this bid to the end
list->Append(bid);
}
} catch (csv::Error &e) {
std::cerr << e.what() << std::endl;
}
}
/**
* Simple C function to convert a string to a double
* after stripping out unwanted char
*
* credit: http://stackoverflow.com/a/24875936
*
* @param ch The character to strip out
*/
double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return atof(str.c_str());
}
/**
* The one and only main() method
*
* @param arg[1] path to CSV file to load from (optional)
* @param arg[2] the bid Id to use when searching the list (optional)
*/
int main(int argc, char* argv[]) {
// process command line arguments
string csvPath, bidKey;
switch (argc) {
case 2:
csvPath = argv[1];
bidKey = "98109";
break;
case 3:
csvPath = argv[1];
bidKey = argv[2];
break;
default:
csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
bidKey = "98109";
}
clock_t ticks;
LinkedList bidList;
Bid bid;
int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << " 1. Enter a Bid" << endl;
cout << " 2. Load Bids" << endl;
cout << " 3. Display All Bids" << endl;
cout << " 4. Find Bid" << endl;
cout << " 5. Remove Bid" << endl;
cout << " 9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
bid = getBid();
bidList.Append(bid);
displayBid(bid);
break;
case 2:
ticks = clock();
loadBids(csvPath, &bidList);
cout << bidList.Size() << " bids read" << endl;
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " milliseconds" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
case 3:
bidList.PrintList();
break;
case 4:
ticks = clock();
bid = bidList.Search(bidKey);
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
if (!bid.bidId.empty()) {
displayBid(bid);
} else {
cout << "Bid Id " << bidKey << " not found." << endl;
}
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
case 5:
bidList.Remove(bidKey);
break;
}
}
cout << "Good bye." << endl;
return 0;
}