-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue-generic.c
139 lines (111 loc) · 2.75 KB
/
queue-generic.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct q_s {
void **container;
int capacity;
int size;
int front;
int rear;
} q_t;
q_t * initq (int capacity) {
q_t *q = malloc(sizeof(q_t));
printf("sizeof(void *)=%ld,sizeof(void**)=%ld\n",sizeof(void *),sizeof(void**));
q->container = malloc(sizeof(void**) * capacity);
q->capacity = capacity;
q->size = 0;
q->front = 0;
q->rear = q->capacity - 1; //This is important, as we increment when enqueue is done
return q;
}
int isEmpty(q_t *q){
return (q->size == 0);
}
int isFull (q_t *q) {
return (q->size == q->capacity);
}
//Double the capacity
void resizeContainer(q_t *q){
void** tmpContainer = malloc(sizeof(void**) * q->capacity * 2);
memcpy(tmpContainer, q->container, sizeof(void**)*q->capacity);
free(q->container);
q->container = tmpContainer;
q->capacity *= 2;
printf("Resized queue to %d\n",q->capacity);
}
int enqueue(q_t * q, void *item){
if(q->size == q->capacity) {
resizeContainer(q);
}
q->rear = (q->rear+1) % q->capacity;
q->container[q->rear] = item;
q->size++;
printf("enqueued now size is %d rear %d\n", q->size,q->rear);
return 0;
}
void * dequeue(q_t *q) {
int front;
if(q->size == 0) {
printf("Nothing to Dequeue\n");
return NULL;
}
front = q->front;
q->front = (q->front + 1) % q->capacity;
q->size--;
printf("dequeued now size is %d front %d\n", q->size, q->front);
return q->container[front];
}
typedef struct elem_t {
int value;
int count;
struct elem_t *left;
struct elem_t *right;
} elem_t;
/* Breadth First Search */
elem_t *BFS(elem_t *r, int value) {
elem_t *temp, *ret = NULL;
int val = 0;
q_t *q= initq(100);
/*
* BFS : 1. Visit r
2. Enqueue r
3. Dequeue Until queue is empty
4 For every dequeued node, visit it , and enqueue their ADJ nodes
*/
if (r == NULL)
return ret;
printf("%d ", r->value);
r->count = 1;
enqueue(q, (void *)r);
while (!isEmpty(q)) {
temp = (elem_t *)dequeue(q);
if(temp->left && temp->left->count == 0) {
printf("%d ", temp->left->value);
temp->left->count = 1;
enqueue(q, (void *)(temp->left));
}
if (temp->right && temp->right->count == 0) {
printf("%d ", temp->right->value);
temp->right->count = 1;
enqueue(q, (void *)(temp->right));
}
}
free(q);
return ret;
}
int main(int argc, char *argv[]) {
//Test queue implementation.
q_t *q = initq(10);
char *str;
for (int i=0; i<15; i++) {
str = malloc(4 * sizeof(char));
sprintf(str," %d ",i);
enqueue(q,(void *)str);
}
for (int i=0; i<15; i++) {
str = (char *)dequeue(q);
if(str)
printf("%s",str);
}
//TBD test BFS here after inserting elements in a temp tree
}