C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
stack.c
Go to the documentation of this file.
1 
6 #include "stack.h"
7 
8 bool is_stack_full(t_gen d);
9 bool is_stack_empty(t_gen d);
10 int stack_size(t_gen d);
11 
18 t_gen stack_peek(t_gen d,int idx);
19 void stack_print(t_gen d);
20 void destroy_stack (t_gen d);
21 
22 
25 
28 
37 t_gen create_stack (char *name, int max_size, e_stacktype stype, t_dparams *prm)
38 {
39  t_stack *s = get_mem(1, sizeof(t_stack));
40 
41  // Init stack variables
42  s->name = name;
43  s->max_size = max_size;
44  s->type = stype;
45  s->count = 0;
46 
47  // Initialize top of stack to max size if down growing
48  s->top = (stype == eARRAY_STACK_DOWN) ? max_size : -1;
49 
50  // Bind stack routines and create stack space based on stack type
51  s->push = stack_push[stype];
52  s->pop = stack_pop[stype];
53  s->peek = stack_peek;
54  s->full = is_stack_full;
55  s->empty = is_stack_empty;
56  s->len = stack_size;
57  s->print = stack_print;
59 
60  // Create link list or array depending on type of stack
61  switch (stype)
62  {
63  case eLL_STACK:
64  s->data = create_link_list("stack_data", eSINGLE_LINKLIST, prm);
65  break;
66  case eARRAY_STACK:
67  case eARRAY_STACK_DOWN:
68  s->data = get_mem(max_size, sizeof(t_gen));
69  break;
70  }
71 
72  s->print_data = prm->print_data;
73  s->free = prm->free;
74 
75  return (t_gen) s;
76 }
77 
84 {
85  return ((t_stack*)d)->count >= ((t_stack*)d)->max_size;
86 }
87 
94 {
95  return ((t_stack*)d)->count == 0;
96 }
97 
104 {
105  return ((t_stack*)d)->count;
106 }
107 
115 {
116  t_stack *s = (t_stack*)d;
117 
118  // Return id stack full
119  if (s->count >= s->max_size) {
120  LOG_WARN("STACKS", "%s: Stack Full\n",s->name);
121  return NULL;
122  }
123  // Incr top and push to stack
124  s->data[++(s->top)] = data;
125  s->count++;
126 
127  return data;
128 }
129 
136 {
137  t_stack *s = (t_stack*)d;
138  t_gen data = NULL;
139 
140  if (s->count == 0) {
141  LOG_WARN("STACKS", "%s: Stack empty\n",s->name);
142  return data;
143  }
144 
145  // Incr top and push to stack
146  data = s->data[(s->top)--];
147  s->count--;
148 
149  return data;
150 }
151 
159 {
160  t_stack *s = (t_stack*)d;
161 
162  // Return id stack full
163  if (s->count >= s->max_size) {
164  LOG_WARN("STACKS", "%s: Stack Full\n",s->name);
165  return NULL;
166  }
167  // Incr top and push to stack
168  s->data[--(s->top)] = data;
169  s->count++;
170 
171  return data;
172 }
173 
180 {
181  t_stack *s = (t_stack*)d;
182  t_gen data = NULL;
183 
184  if (s->count == 0) {
185  LOG_WARN("STACKS", "%s: Stack empty\n",s->name);
186  return data;
187  }
188 
189  // Incr top and push to stack
190  data = s->data[s->top++];
191  s->count--;
192 
193  return data;
194 }
195 
203 {
204  t_stack *s = (t_stack*)d;
205  t_linklist * l = (t_linklist *)s->data;
206 
207  // Return id stack full
208  if (s->count >= s->max_size) {
209  LOG_WARN("STACKS", "%s: Stack Full\n",s->name);
210  return NULL;
211  }
212 
213  // add in begining of the link list
214  l->add(l, data);
215  s->count++;
216 
217  return data;
218 }
219 
226 {
227  t_stack *s = (t_stack*)d;
228  t_gen data = NULL;
229  t_linklist * l = (t_linklist *)s->data;
230 
231  if (s->count == 0) {
232  LOG_WARN("STACKS", "%s: Stack empty\n",s->name);
233  return data;
234  }
235 
236  // deleting the head node of link list i.e top of stack
237  data = l->del_idx(l, 0);
238  s->count--;
239 
240  return data;
241 }
242 
243 /* @brief
244  * Util function to get type of stack in string
245  * @param type - Stack Type
246  * @return String of stack type
247 */
248 static char * get_stack_name(e_stacktype type)
249 {
250  switch(type) {
251  case eARRAY_STACK:
252  return "ARRAY_STACK_UP";
253  case eARRAY_STACK_DOWN:
254  return "ARRAY_STACK_DOWN";
255  case eLL_STACK:
256  return "LL_STACK";
257  }
258 
259  return "UNDEFINED";
260 }
261 
268 {
269  t_stack *s = (t_stack*)d;
270  t_linklist *l = NULL;
271  int i;
272 
273  printf("%s {max: %d} {size: %d} {top: %d} {type: %s} \n[",s->name,
274  s->max_size, s->count, s->top, get_stack_name(s->type));
275 
276  if (s->type != eLL_STACK) {
277  i = (s->type != eARRAY_STACK_DOWN)? 0:(s->max_size-1);
278  do {
279  s->print_data(s->data[i]);
280  printf(", ");
281  (s->type != eARRAY_STACK_DOWN)? i++:i--;
282  } while(i != s->top);
283  s->print_data(s->data[i]);
284  printf("]\n");
285  }
286  else {
287  l = (t_linklist *)s->data;
288  l->print(l);
289  }
290 }
291 
292 /* @brief
293  * sneak peek into an element of the stack
294  * @param d - Pointer to instance of stack
295  * @param idx - Index of the element to peek
296  * @return - Data pointer if index in bounds else NULL
297 */
299 {
300  t_stack *s = (t_stack *)d;
301  t_linklist *l = NULL;
302  t_gen n;
303 
304  // index out of bound
305  if ((idx < 0) || (idx >= s->count)) {
306  LOG_WARN("STACKS", "index is out of bounds\n", s->count);
307  return NULL;
308  }
309 
310  // Return data for array based stack
311  if (s->type != eLL_STACK) {
312  return s->data[idx];
313  }
314 
315  // Return data for link list based stack
316  l = (t_linklist*)s->data;
317  n = l->get_idx(l, idx);
318 
319  return l->get_node_data(n);
320 }
321 
328 {
329  t_stack *s = (t_stack*)d;
330  t_linklist *l = NULL;
331  int i;
332 
333  // Free created stack space
334  switch (s->type)
335  {
336  case eLL_STACK:
337  l = (t_linklist*)s->data;
338  l->destroy(l);
339  break;
340  case eARRAY_STACK:
341  case eARRAY_STACK_DOWN:
342  while (s->empty(s) != true) {
343  s->free(s->pop(s), __FILE__, __LINE__);
344  }
345  free_mem(s->data);
346  break;
347  }
348 
349  // Free stack
350  free_mem(s);
351 }
352 
#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
t_gen stack_peek(t_gen d, int idx)
Definition: stack.c:298
t_gen stack_pop_ll(t_gen d)
Pop an element from a link list based stack
Definition: stack.c:225
t_gen stack_push_arr_down(t_gen d, t_gen data)
Push an element into down growing stack
Definition: stack.c:158
t_gen create_stack(char *name, int max_size, e_stacktype stype, t_dparams *prm)
Create an instance of stack
Definition: stack.c:37
int stack_size(t_gen d)
get stack size
Definition: stack.c:103
t_gen stack_pop_arr_down(t_gen d)
Pop an element from down growing ll stack
Definition: stack.c:179
void stack_print(t_gen d)
print_stack_info
Definition: stack.c:267
t_gen stack_push_arr_up(t_gen d, t_gen data)
Push an element into up growing stack
Definition: stack.c:114
bool is_stack_empty(t_gen d)
Check stack empty
Definition: stack.c:93
void destroy_stack(t_gen d)
Destroy instance of the stack
Definition: stack.c:327
bool is_stack_full(t_gen d)
Check stack full
Definition: stack.c:83
f_gen2 stack_push[]
Look Up function ptrs for pushing to stack.
Definition: stack.c:24
t_gen stack_push_ll(t_gen d, t_gen data)
Push an element into a link list based stack
Definition: stack.c:202
f_gen stack_pop[]
Look Up function ptrs for poping to stack.
Definition: stack.c:27
t_gen stack_pop_arr_up(t_gen s)
Pop an element from up growing stack
Definition: stack.c:135
Contains declations of stack types, operations and structure.
e_stacktype
types of supported stacks
Definition: stack.h:12
@ eARRAY_STACK
Top Growing Stack.
Definition: stack.h:14
@ eLL_STACK
LinkList based Stack.
Definition: stack.h:13
@ eARRAY_STACK_DOWN
Down Growing Stack.
Definition: stack.h:15
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
stack struct defn
Definition: stack.h:19
f_len len
routine to get len of stack
Definition: stack.h:34
f_vgen destroy
routine to destroy stack contents
Definition: stack.h:36
f_print print
routine to print stack contents
Definition: stack.h:35
f_gen2 push
stack operations
Definition: stack.h:29
char * name
Stack instance name.
Definition: stack.h:21
f_empty empty
routine to check if stack is empty
Definition: stack.h:33
f_free free
routies for operating on data
Definition: stack.h:39
f_genidx peek
routine to peek elements in stack
Definition: stack.h:31
int max_size
Max Size of stack.
Definition: stack.h:23
f_print print_data
Definition: stack.h:40
f_full full
routine to check if stack is full
Definition: stack.h:32
e_stacktype type
Stack Type.
Definition: stack.h:25
int count
Total elems present in stack.
Definition: stack.h:22
f_gen pop
routine to pop element into stack
Definition: stack.h:30
t_gen * data
Definition: stack.h:27
int top
Stack Top.
Definition: stack.h:24
t_gen(* f_gen2)(t_gen, t_gen)
fn ptr that takes two gen ptr and return gen ptr
Definition: typedefs.h:47
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41
t_gen(* f_gen)(t_gen)
Generic data pointer definitions that are common to most data structure operations.
Definition: typedefs.h:46