-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl.cpp
40 lines (34 loc) · 1.26 KB
/
stl.cpp
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
// stl.cpp
//jay ashworth and andy zeng
#include "volsort.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool node_number_compare(const Node *a, const Node *b) {
return a->number<b->number;
} //implement in this file (volsort.h), used by quick, merge and stl
bool node_string_compare(const Node *a, const Node *b){
return a->string<b->string;
} //implement in this file (volsort.h), used by quick, merge and stl
//most basic sort function. it intakes the linked list into a vector by implementing a for loop similar to that in main and then passes that into stl::sort.
//I then returned the sorted vector to the linked list by applying the same ideas used in the first for loop but in a more traditional for loop.
void stl_sort(List &l, bool numeric) {
vector<Node*> list;
for (Node * curr = l.head; curr != NULL; curr = curr->next) {
list.push_back(curr);
}
if(numeric) {
sort(list.begin(),list.end(),node_number_compare);
}
else {
sort(list.begin(),list.end(),node_string_compare);
}
list.push_back(nullptr);
l.head = list.at(0);
Node * curr = l.head;
for (unsigned int i = 1; i<list.size();i++) {
curr->next = list.at(i);
curr = curr->next;
}
}