forked from AllAlgorithms/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
77 lines (63 loc) · 1.97 KB
/
stack.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
/*
* Author : Satyajit Ghana (satyajit_ghana) https://github.com/satyajitghana
* Stack : An attempt to implementing a generic data type stack in C
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "stack.h"
#define ds(s) printf("\nDEBUG--*"#s " : %s*\n", s);
#define dc(c) printf("\nDEBUG--%"#c " : %c%\n", c);
#define di(i) printf("\nDEBUG--#"#i " : %d#\n", i);
Stack* newStack(int max_size) {
Stack* mystack = malloc(sizeof *mystack);
init_stack(mystack, max_size);
return mystack;
}
void init_stack(Stack* mystack, int max_size) {
mystack -> top = -1;
mystack -> data = NULL;
mystack -> MAX = max_size;
mystack -> push = push;
mystack -> pop = pop;
mystack -> display = display;
mystack -> peek = peek;
mystack -> isEmpty = isEmpty;
}
void push(Stack* mystack, void* data) {
if (mystack -> top >= mystack -> MAX) {
printf("\n*Stack Overflow Detected !*\n");
return;
}
/* This code sucks, you know it and i know it.
* Move on and call me an idiot later */
mystack -> data = realloc(mystack -> data, (mystack -> top + 2) * sizeof *(mystack -> data));
if (mystack -> data != NULL) {
(mystack -> data)[(++mystack -> top)] = data;
} else {
printf("\n*cannot allocate memory !*\n");
}
}
void* pop(Stack* mystack) {
if (mystack -> top < 0) {
printf("\n*Stack Underflow detected !*\n");
return NULL;
}
mystack -> top--;
/* Resize the data array as the elements are removed */
mystack -> data = realloc(mystack -> data, (mystack -> top + 2) * sizeof *(mystack-> data));
return (mystack -> data)[(mystack -> top)+1];
}
void display(Stack* mystack) {
for (int i = mystack -> top ; i >= 0 ; i--) {
printf("%s ", *(char**)(mystack->data + i));
}
}
void* peek(Stack* mystack) {
return (mystack -> data)[mystack -> top];
}
bool isEmpty(Stack* mystack) {
if (mystack -> top < 0)
return true;
return false;
}