-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniStack.cpp
More file actions
84 lines (73 loc) · 1.47 KB
/
MiniStack.cpp
File metadata and controls
84 lines (73 loc) · 1.47 KB
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
#include <assert.h>
#include <memory.h>
#define MAX_STACK_SIZE 256
typedef struct stack
{
int top;
void* slot[MAX_STACK_SIZE];
int count_of_push;
int count_of_pop;
}Stack_t;
static Stack_t*
get_new_stack(void);
static int push(Stack_t *stack, void *node);
static void* pop(Stack_t *stack);
static int isStackEmpty(Stack_t *stack);
static void free_stack(Stack_t *stack);
static void* StackGetTopElem(Stack_t *stack);
Stack_t*
get_new_stack(void)
{
Stack_t* stack = (Stack_t*)calloc(1,sizeof(Stack_t));
if(!stack)
return NULL;
memset(stack,0,sizeof(Stack_t));
stack->top = -1;
stack->count_of_push = 0;
stack->count_of_pop = 0;
return stack;
}
void*
StackGetTopElem(Stack_t* stack)
{
if(!stack || stack->top == -1)return NULL;
return stack->slot[stack->top];
}
int
isStackEmpty(Stack_t* stack)
{
assert(stack);
if(stack->top == -1)return 1;
return 0;
}
void*
pop(Stack_t *stack)
{
void* ret = NULL;
if(!stack)return NULL;
if(stack->top == -1)return NULL;
ret = stack->slot[stack->top];
stack->slot[stack->top] = NULL;
stack->top--;
stack->count_of_pop++;
return ret;
}
int
push(Stack_t* stack,void* node)
{
if(!stack || !node)return -1;
if(!stack->top < MAX_STACK_SIZE)
{
stack->top++;
stack->slot[stack->top] = node;
stack->count_of_push++;
return 0;
}
return -1;
}
void
free_stack(Stack_t* stack)
{
if(!stack)return;
free(stack);
}