-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTK.C
More file actions
73 lines (68 loc) · 1.28 KB
/
Copy pathSTK.C
File metadata and controls
73 lines (68 loc) · 1.28 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
#include<stdint.h>
#include<stdlib.h>
#include<stddef.h>
#include<assert.h>
#include "stack.h"
stack stack_new(uint32_t size)
{
size=((size>0&&size<32)?size:MAX_DEPTH);
stack s={size,-1,{0}};
return s;
}
uint32_t stack_full(const stack *stk)
{
assert(stk!=NULL);
return(stk->top+1==stk->size);
}
uint32_t stack_empty(const stack *stk)
{
assert(stk!=NULL);
return(stk->top==-1);
}
stack* stack_push(stack *stk, _STACK_CONTENT_TYPE_ ele, stack_result *res)
{
assert(stk!=NULL);
if(stk->top+1 < stk->size)
{
stk->data[++stk->top]=ele;
res->data=ele;
res->status=STACK_OK;
}
else
{
res->status=STACK_FULL;
}
assert((res->status==STACK_OK)||(stk->top+1==stk->size));
return stk;
}
stack* stack_pop(stack *stk, stack_result *res)
{
assert(stk!=NULL);
if(stk->top > -1)
{
res->data=stk->data[stk->top];
--stk->top;
res->status=STACK_OK;
}
else
{
res->status=STACK_EMPTY;
}
assert((res->status==STACK_OK)||(stk->top==-1));
return stk;
}
stack* stack_peek(stack *stk, stack_result *res)
{
assert(stk!=NULL);
if(stk->top > -1)
{
res->data=stk->data[stk->top];
res->status=STACK_OK;
}
else
{
res->status=STACK_EMPTY;
}
assert((res->status==STACK_OK)||(stk->top==-1));
return stk;
}