C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
queue.c
Go to the documentation of this file.
1 
6 #include "queue.h"
7 
8 
9 void queue_enqueue_ll(t_gen s, t_gen data);
11 
12 void queue_enqueue_arr(t_gen s, t_gen data);
14 
15 t_gen queue_peek(t_gen d, int idx);
16 bool queue_full(t_gen d);
17 bool queue_empty(t_gen d);
18 int queue_size(t_gen d);
19 void queue_print(t_gen d);
20 void destroy_queue (t_gen d);
21 
24 
27 
28 
37 t_gen create_queue (char *name, int max_size, e_queuetype qtype, t_dparams *prm)
38 {
39  t_queue *q = get_mem(1, sizeof(t_queue));
40 
41  // Init queue variables
42  q->name = name;
43  q->max_size = max_size;
44  q->type = qtype;
45  q->count = 0;
46  q->front = q->rear = -1;
47 
48  // Bind queue routines
49  q->full = queue_full;
50  q->empty = queue_empty;
51  q->len = queue_size;
52  q->print = queue_print;
53  q->enq = q_enq[qtype];
54  q->deq = q_deq[qtype];
55  q->peek = queue_peek;
57 
58  // create queue space depending on link list or array based
59  switch (qtype)
60  {
61  case eARRAY_QUEUE_CIRC:
62  q->data = get_mem(max_size, sizeof(t_gen));
63  break;
64  case eLL_QUEUE_CIRC:
65  q->data = create_link_list("queue_data",
66  eDOUBLE_LINKLIST, prm);
67  break;
68  }
69  q->print_data = prm->print_data;
70  q->free = prm->free;
71 
72  return (t_gen)q;
73 }
74 
75 
83 {
84  t_queue *q = (t_queue*)d;
85 
86  // return if queue full
87  if (q->full(q) == true) {
88  LOG_WARN("QUEUES", "%s: Queue Full\n",q->name);
89  }
90 
91  // queue empty (added first element)
92  q->front = (q->front != -1) ? q->front : 0;
93 
94  // Incr Rear and add data to queue
95  q->rear = (q->rear + 1) % q->max_size;
96  q->data[q->rear] = data;
97  q->count++;
98 
99 }
100 
107 {
108  t_queue *q = (t_queue*)d;
109  t_gen data;
110 
111  // return if queue empty
112  if (q->empty(q) == true) {
113  LOG_WARN("QUEUES", "%s: Queue Empty\n",q->name);
114  return NULL;
115  }
116 
117  // get queue element
118  data = q->data[q->front];
119 
120  // if last element read reset front and rear of queue to -1
121  q->count--;
122  if (q->front == q->rear) {
123  q->rear = q->front = -1;
124  } else {
125  q->front = (q->front + 1) % q->max_size;
126  }
127 
128  return data;
129 
130 }
131 
138 t_gen queue_peek(t_gen d, int idx)
139 {
140  t_queue *q = (t_queue*)d;
141  t_gen node;
142  t_linklist *l;
143 
144  // return if queue empty
145  if (q->empty(q) == true) {
146  LOG_WARN("QUEUES", "%s: Queue Empty\n",q->name);
147  return NULL;
148  }
149 
150  // index out of bound
151  if ((idx < 0) && (idx >= q->count)) {
152  LOG_WARN("QUEUE", "index is out of bounds\n", q->count);
153  return NULL;
154  }
155 
156  // get queue element for array based queue
157  if (q->type ==eARRAY_QUEUE_CIRC) {
158  return q->data[idx];
159  }
160 
161  // get queue element for linklist based queue
162  l = (t_linklist*)q->data;
163  node = l->get_idx(l, idx);
164 
165  return l->get_node_data(node);
166 
167 }
168 
176 {
177  t_queue *q = (t_queue*)d;
178  t_linklist *l = (t_linklist*)q->data;
179 
180  // return if queue full
181  if (q->full(q) == true) {
182  LOG_WARN("QUEUES", "%s: Queue Full\n",q->name);
183  }
184 
185  // Incr Rear and add data to queue
186  q->count++;
187  l->append(l, data);
188 }
189 
196 {
197  t_queue *q = (t_queue*)d;
198  t_linklist *l = (t_linklist*)q->data;
199 
200  // return if queue full
201  if (q->full(q) == true) {
202  LOG_WARN("QUEUES", "%s: Queue Full\n",q->name);
203  }
204 
205  // Decr count and pop first node in linklist
206  q->count--;
207 
208  return l->del_idx(l, 0);
209 }
210 
217 {
218  return ((t_queue*)d)->count >= ((t_queue*)d)->max_size;
219 }
220 
227 {
228  return ((t_queue*)d)->count == 0;
229 }
230 
237 {
238  return ((t_queue*)d)->count;
239 }
240 
241 /* @brief
242  * Util function to get type of queue in string
243  * @param type - Stack Type
244  * @return String of queue type
245 */
246 static char * get_qname(e_queuetype type)
247 {
248  switch(type) {
249  case eLL_QUEUE_CIRC:
250  return "LL_QUEUE";
251  case eARRAY_QUEUE_CIRC:
252  return "ARRAY_QUEUE";
253  }
254  return "UNDEFINED";
255 }
256 
263 {
264  t_queue *q = (t_queue*)d;
265  int i;
266  t_linklist *l;
267 
268  printf("%s {max: %d} {type:%s} {size: %d} {front/rear: [%d:%d]} \n",q->name,
269  q->max_size, get_qname(q->type), q->count, q->front,q->rear);
270  switch (q->type) {
271  case eARRAY_QUEUE_CIRC:
272  printf("[");
273  for (i = q->front; (i != -1) && (i <= q->rear); i ++) {
274  q->print_data(q->data[i]);
275  printf(", ");
276  }
277  printf("]\n");
278  break;
279  case eLL_QUEUE_CIRC:
280  l = (t_linklist*)q->data;
281  l->print(l);
282  break;
283  }
284 }
285 
292 {
293  t_queue *q = (t_queue*)d;
294  t_linklist *l;
295  int i;
296 
297  // Free created queue space
298  switch (q->type)
299  {
300  case eARRAY_QUEUE_CIRC:
301  while (q->empty(q) != true) {
302  q->free(q->deq(q), __FILE__, __LINE__);
303  }
304  free_mem(q->data);
305  break;
306  case eLL_QUEUE_CIRC:
307  l = (t_linklist*)q->data;
308  l->destroy(l);
309  break;
310  }
311 
312  // Free queue
313  free_mem(q);
314 }
#define LOG_WARN(mod, fmt, args...)
Definition: logger.h:18
#define free_mem(mem_addr)
Definition: memory_manager.h:9
#define get_mem(nmemb, size)
Definition: memory_manager.h:8
int queue_size(t_gen d)
Return queue size
Definition: queue.c:236
bool queue_full(t_gen d)
Check queue full
Definition: queue.c:216
t_gen queue_dequeue_arr(t_gen s)
pop front element in queue
Definition: queue.c:106
void destroy_queue(t_gen d)
Destroy queue instance
Definition: queue.c:291
void queue_enqueue_arr(t_gen s, t_gen data)
add element in queue
Definition: queue.c:82
void queue_enqueue_ll(t_gen s, t_gen data)
add element in queue
Definition: queue.c:175
bool queue_empty(t_gen d)
Check queue empty
Definition: queue.c:226
void queue_print(t_gen d)
queue_print_info
Definition: queue.c:262
t_gen queue_peek(t_gen d, int idx)
peek front element in queue
Definition: queue.c:138
t_gen create_queue(char *name, int max_size, e_queuetype qtype, t_dparams *prm)
Destroy queue instance
Definition: queue.c:37
t_gen queue_dequeue_ll(t_gen s)
pop front element in queue
Definition: queue.c:195
f_ins q_enq[]
Look Up function ptrs to enq elems to queue.
Definition: queue.c:23
f_gen q_deq[]
Look Up function ptrs to deq elems to queue.
Definition: queue.c:26
Contains declations of queue types, operations and structure.
e_queuetype
types of queue
Definition: queue.h:11
@ eLL_QUEUE_CIRC
Link List Based Queue.
Definition: queue.h:12
@ eARRAY_QUEUE_CIRC
Array Based Queue.
Definition: queue.h:13
data params struct defn
Definition: common.h:18
f_free free
Routine used for freeing elements of said data.
Definition: common.h:26
f_print print_data
Routine used for printing elem data.
Definition: common.h:33
queue struct defn
Definition: queue.h:17
f_len len
routine to get length queue
Definition: queue.h:31
f_print print
routine to print queue elements
Definition: queue.h:35
e_queuetype type
Stack Type.
Definition: queue.h:24
char * name
Stack instance name.
Definition: queue.h:19
f_gen deq
routine to pop elements out of queue
Definition: queue.h:30
f_destroy destroy
routine to detroy queue instance
Definition: queue.h:36
f_empty empty
routine to check if queue empty
Definition: queue.h:34
f_free free
Definition: queue.h:40
f_genidx peek
routine to peek node in queue
Definition: queue.h:32
int max_size
Max Size of queue.
Definition: queue.h:20
f_print print_data
Definition: queue.h:39
f_full full
routine to check if queue full
Definition: queue.h:33
int front
Queue Front Pointer.
Definition: queue.h:22
int count
Total elems present in queue.
Definition: queue.h:21
f_ins enq
routine to push elements to queue
Definition: queue.h:29
t_gen * data
Ptr to link List or array based on type of queue.
Definition: queue.h:27
int rear
Queue Rear Pointer.
Definition: queue.h:23
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41
f_vgen2 f_ins
fn type of insert elem function
Definition: typedefs.h:58
t_gen(* f_gen)(t_gen)
Generic data pointer definitions that are common to most data structure operations.
Definition: typedefs.h:46