-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog3.c
More file actions
98 lines (85 loc) · 2.08 KB
/
prog3.c
File metadata and controls
98 lines (85 loc) · 2.08 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"List.h"
int main(int argc, char **argv)
{
struct List_t *myList = (struct List_t*)malloc(1 * sizeof(struct List_t));
myList = makeList(myList);
if(argc != 3)
{
fprintf(stderr, "Not enough arguments: %s <count> <seed>\n", argv[0]);
return 1;
}
int count = atoi(argv[1]);
int seed = atoi(argv[2]);
srand(seed);
char commands[8] = "csifr";
for(int i=0; i<count; i++)
{
char rCommand = commands[rand() % 5];
int rOption = rand() % 64;
struct Node_t* myNode = NULL;
//size_t howMany = 0;
char command = ' ';
char rBuffer[100];
sprintf(rBuffer, "-%c:%d", rCommand, rOption);
int option = 0;
fprintf(stderr, "Processing command '%s'.\n", rBuffer);
sscanf(rBuffer, "-%c:%d", &command, &option);
switch(command)
{
case 'c':
myList = freeList(myList);
myList = (struct List_t*)malloc(1 * sizeof(struct List_t));
myList = makeList(myList);
break;
case 's':
//howMany = size(myList);
size(myList);
break;
case 'i':
myNode = insert(myList, option);
if(myNode == NULL)
{
fprintf(stderr, "Insert failed\n");
return 3;
}
break;
case 'f':
//howMany = find(myList, option);
find(myList, option);
break;
case 'r':
//howMany = removeItem(myList, option);
removeItem(myList, option);
break;
}
//Dangerous estimate
char *buffer = (char *)calloc(1, 250000 * sizeof(char));
if(traverse(myList, buffer) == 0)
{
fprintf(stdout, "List is empty.\n");
}
else
{
fprintf(stdout, "List: %s.\n", buffer);
}
free(buffer);
char *printable = traverse2(myList);
fprintf(stdout, "List: %s.\n", printable);
free(printable);
}
//Dangerous estimate but we don't want to answer a part of the assignment.
char *buffer = (char *)calloc(1, 250000 * sizeof(char));
traverse(myList, buffer);
fprintf(stdout, "%s\n", buffer);
free(buffer);
char *printable = traverse2(myList);
fprintf(stdout, "%s\n", printable);
free(printable);
myList = freeList(myList);
//valgrind should report no memory leaks at this point
return 0;
}