Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
Latest commit
98 lines (72 loc) · 2.1 KB
/
Copy pathmain.c
File metadata and controls
98 lines (72 loc) · 2.1 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
/* Vishwa Venkateshwaran */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"lab4.h"
voidpush(Stack*this, intx){
this->push(this->Stack, x);
}
intpop(Stack*this){
returnthis->pop(this->Stack);
}
intstackLength(Stack*this){
returnthis->length(this->Stack);
}
voidStackTest1(inttype) {
Stack*newStack=type==1 ? newStack1() : newStack2();
inti, removed, errors;
clock_tstart, end;
doublecpu_time_used;
printf("\nStack test 1 type %d\n", type);
start=clock();
errors=0;
for(i=0; i<NUM_ENTRIES; i++){
push(newStack, i);
}
printf("Expected length after adding all the elements: %i, length recieved: %i\n", NUM_ENTRIES, stackLength(newStack));
for(i=0; i<NUM_ENTRIES; i++){
removed=pop(newStack);
if(removed!=NUM_ENTRIES-i-1){
printf("Expected %i, removed %i\n", NUM_ENTRIES-i-1, removed);
errors++;
}
}
printf("Expected length after removing all the elements: 0, length recieved: %i\n", stackLength(newStack));
printf("Ended with %i errors\n", errors);
end=clock();
cpu_time_used= ((double)(end-start))/CLOCKS_PER_SEC;
printf("Took %f seconds\n", cpu_time_used);
}
voidStackTest2(inttype) {
Stack*newStack=type==1 ? newStack1() : newStack2();
inti, removed, errors, j=1;
clock_tstart, end;
doublecpu_time_used;
printf("\nStack test 2 type %d\n", type);
start=clock();
errors=0;
for(i=NUM_ENTRIES; i>0; i--){
push(newStack, i);
}
printf("Expected length after adding all the elements: %i, length recieved: %i\n", NUM_ENTRIES, stackLength(newStack));
for(i=NUM_ENTRIES; i>0; i--){
removed=pop(newStack);
if(removed!=j){
printf("Expected %i, removed %i\n", j, removed);
errors++;
}
j++;
}
printf("Expected length after removing all the elements: 0, length recieved: %i\n", stackLength(newStack));
printf("Ended with %i errors\n", errors);
end=clock();
cpu_time_used= ((double)(end-start))/CLOCKS_PER_SEC;
printf("Took %f seconds\n", cpu_time_used);
}
intmain(intargc, char*argv[]) {
StackTest1(1);
StackTest1(2);
StackTest2(1);
StackTest2(2);
return0;
}