-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
64 lines (55 loc) · 1.37 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "list.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <iostream>
#include <fstream>
using namespace std;
// The data in our lists will be char* strings, so
// create a comparator function that can be used by the List api:
int comparator(void *s1, void *s2) {
int i1 = *((int*)s1);
int i2 = *((int*)s2);
if (i1 > i2) {
return 1;
}
if (i1 == i2) {
return 0;
}
return -1;
}
// Cleanup function used to free data by List api
bool cleanup(void *data) {
free(data);
return true;
}
List *makeList(char *filename) {
List *list = createList();
ifstream file(filename);
int data;
while (file >> data) {
int* datap = new int;
*datap = data;
appendTo(list, datap);
}
return list;
}
void printList(List *list) {
for (ListNode *node = list->head; node != NULL; node = node->next) {
cout << *((int*)node->data) << ' ';
}
cout << endl;
}
// Invoke the list populator functions (appendAndSort, addSorted) with different sizes of word sets
int main(int argc, char **argv) {
if (argc != 2) {
cout << "Usage: " << argv[0] << "/path/to/file/containing/numbers/to/sort" << endl;
return 0;
}
List *l = makeList(argv[1]);
mergeSort(l, comparator);
cout << "Number of inversions=" << numInversions() << endl;
deleteList(&l, cleanup);
return 1;
}