-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
49 lines (38 loc) · 1.14 KB
/
main.c
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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sorted-list.h"
int main(int argc, char** argv) {
// Set up and initialize a sorted list
sorted_list_t lst;
sorted_list_init(&lst);
// Read lines until the end of stdin
char* line = NULL;
size_t line_size = 0;
while (getline(&line, &line_size, stdin) != EOF) {
int num;
// Which command is this?
if (strcmp(line, "done\n") == 0) {
// The user is done so we can exit
break;
} else if (strcmp(line, "print\n") == 0) {
// Print the list
sorted_list_print(&lst);
} else if (sscanf(line, "insert %d\n", &num) == 1) {
// Add a value to the list
sorted_list_insert(&lst, num);
} else if (sscanf(line, "count %d\n", &num) == 1) {
// How many times does the provided value appear in the list?
size_t count = sorted_list_count(&lst, num);
printf("%d\n", count); // %d was origninally %lu but gave me an error even tho I didnt code this??
} else {
printf("unrecognized command.\n");
}
}
// Free the space allocated by getline
free(line);
// Clean up the queue
sorted_list_destroy(&lst);
return 0;
}