-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.c
48 lines (30 loc) · 893 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
/*************************/
/* stack.c */
/*************************/
#include "stack.h"
#include "globals.h"
#define current_stacksize( p_S ) ( (p_S) -> top - (p_S) -> base )
extern status init_stack ( stack *p_S ) {
p_S -> top = p_S -> base ;
return OK ;
}
extern status pop( stack *p_S , generic_ptr *p_data ) {
if ( empty_stack ( p_S ) == TRUE ) return ERROR ;
p_S -> top-- ;
*p_data = *p_S->top ;
return OK ;
}
extern status top ( stack *p_S , generic_ptr *p_data ) {
if ( pop ( p_S , p_data ) == ERROR ) return ERROR ;
return push ( p_S , *p_data ) ;
}
extern bool empty_stack ( stack *p_S ) {
return ( ( p_S -> top == p_S -> base) ? TRUE : FALSE ) ;
}
extern status push ( stack *p_S , generic_ptr data ) {
if ( current_stacksize( p_S) == MAXSTACKSIZE )
return ERROR ;
*p_S -> top = data ;
p_S -> top++ ;
return OK ;
}