-
Notifications
You must be signed in to change notification settings - Fork 45
/
stack.c
52 lines (45 loc) · 822 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void stackInit(Stack *s, int size)
{
s->content = malloc(size * sizeof(void*));
s->size = size;
s->top = -1;
}
void stackPush(Stack *s, void* val)
{
/*if(s->top + 1 >= s->size) // If stack is full
{
(s->size)++;
s->content = (void**)realloc(s->content, s->size * sizeof(void*));
}*/
(s->top)++;
s->content[s->top] = val;
}
void* stackTop(Stack *s)
{
void *ret = NULL;
if(s->top >= 0 && s->content != NULL)
ret = s->content[s->top];
return ret;
}
void* stackPop(Stack *s)
{
void *ret = NULL;
if(s->top >= 0 && s->content != NULL)
ret = s->content[(s->top)--];
return ret;
}
int stackSize(Stack *s)
{
return s->top + 1;
}
void stackFree(Stack *s)
{
if (s->content)
free(s->content);
s->content = NULL;
s->size = 0;
s->top = -1;
}