-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
85 lines (73 loc) · 1.66 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
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
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct Foo
{
int a;
int b;
int c;
} Foo;
void printQueue(queue *q)
{
printf("\n-----------------\n");
while(!isEmpty(q))
{
Foo temp;
// No error checking
dequeue(q, &temp);
printf("Dequeued %d\n", temp.c);
}
printf("-----------------\n");
}
// Driver code to test the generic queue library
int main()
{
queue *q = createQueue(sizeof(Foo));
if(q == NULL)
{
fprintf(stderr, "Cant create queue\n");
return -1;
}
Foo f;
f.a = 100;
f.b = 100;
f.c = 100;
// Insert one element into the queue
if(enqueue(q, &f) == NULL)
{
fprintf(stderr, "Insertion failure\n");
return -1;
}
printf("Enqeued %d\n", f.a);
// Peek the front element
Foo frontElem;
if(front(q, &frontElem) == NULL)
{
fprintf(stderr, "Cant get front element\n");
return -1;
}
printf("Front element of queue is %d\n", frontElem.a);
// Insert 3 elements into the queue
for(int i = 0; i < 3; i++)
{
Foo f1;
f1.c = i + 10;
f1.b = 0;
f1.a = 0;
// Even after f1 goes out of scope we make a copy so it does not matter
enqueue(q, &f1); // No error checking
printf(
"Enqueued %d\n",
f1.c); // Showing only one number of the struct for simplicity reasons
}
printf("Size of queue is %zd\n", getSize(q));
printf("Reserving queue\n");
reverse(q);
queue *deepCopy = copyQueue(q); // No error checking
printQueue(deepCopy);
printf("Cleared queue\n");
destroyQueue(&q);
destroyQueue(&deepCopy);
printf("Destroyed queue\n");
return 0;
}