You are on page 1of 11

C

. 2011-2012

(pushdown stack)
:
() , PUSH(S,x)
(, pop)

, POP(S)
, EMPTY(S)
..

2/11


,
LIFO (last-in, first-out)

Push

Pop


.
.

3/11


C (stack.h)
1
2

#ifndef _STACK_H
#define _STACK_H

3
4
5

typedef int stackItemT ;


typedef struct stack_rep * stack ;

6
7

stack stack_create ( ) ;

8
9

void stack_destroy ( stack S ) ;

10
11

void stack_push ( stack S , stackItemT elem ) ;

12
13

void stack_pop ( stack S ) ;

14
15

stackItemT

stack_top ( stack S ) ;

16
17

int

stack_empty ( stack S ) ;

int

stack_size ( stack S ) ;

18
19
20
21

#endif

4/11


(stack.c)

1
2
3

#include < s t d i o . h>


#include < s t d l i b . h>
#include "stack.h"

4
5
6
7
8
9
10

struct stack_rep
{
stackItemT * array ;
int maxSize ;
int top ;
};

5/11


(stack.c)
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

stack stack_create_maxsize ( int maxSize )


{
if ( maxSize <= 0 ) {
fprintf ( stderr , "Wrongstacksize(%d)\n" , maxSize ) ;
abort ( ) ;
}
stack S = ( stack ) malloc ( sizeof ( struct stack_rep ) ) ;
if ( S == 0 ) {
fprintf ( stderr , "Insufficientmemorytoinitializestack.\n" ) ;
abort ( ) ;
}
stackItemT * items ;
items = ( stackItemT * ) malloc ( sizeof ( stackItemT ) * maxSize ) ;
if ( items == 0 ) {
fprintf ( stderr , "Insufficientmemorytoinitializestack.\n" ) ;
abort ( ) ;
}
S>array = items ;
S>maxSize = maxSize ;
S>top = 1;
return S ;
}
stack stack_create ( ) { return stack_create_maxsize ( 10 ) ; }

6/11


(stack.c)

40
41
42
43
44
45

void stack_destroy ( stack S )


{
if ( ! S | | ! S>array ) {
fprintf ( stderr , "Cannotdestroystack\n" ) ;
abort ( ) ;
}

46

free ( S>array ) ;
S>array = 0 ;
S>maxSize = 0 ;
S>top = 1;
free ( S ) ;

47
48
49
50
51
52

7/11


(stack.c)
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

void stack_doublesize ( stack S )


{
int i ;
if ( ! S | | ! S>array | | S>maxSize <= 0 ) {
fprintf ( stderr , "Cannotdoublestacksize\n" ) ;
abort ( ) ;
}
/ / c r e a t e double t h e s t a c k
struct stack_rep temp ;
temp . maxSize = 2 * S>maxSize ;
temp . array = ( stackItemT * ) malloc ( sizeof ( stackItemT ) * temp . maxSize ) ;
temp . top = S>top ;
if ( temp . array == 0 ) {
fprintf ( stderr , "Insufficientmemorytodoublethestack.\n" ) ;
abort ( ) ;
}
/ / copy t o new a r r a y
for ( i = 0 ; i <= S>top ; i++ )
temp . array [ i ] = S>array [ i ] ;
free ( S>array ) ;
S>array = temp . array ;
S>maxSize = temp . maxSize ;
}

8/11


Push, Pop and Top (stack.c)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

void stack_push ( stack S , stackItemT elem )


{
if ( S>top >= S>maxSize 1 )
stack_doublesize ( S ) ;
S>array [ ++S>top ] = elem ;
}
void stack_pop ( stack S )
{
if ( stack_empty ( S ) ) {
fprintf ( stderr , "Can'tpopelementfromstack:stackisempty.\n" ) ;
abort ( ) ;
}
S>top;
}
stackItemT stack_top ( stack S )
{
if ( stack_empty ( S ) ) {
fprintf ( stderr , "Stackisempty.\n" ) ;
abort ( ) ;
}
return S>array [ S>top ] ;
}

9/11


Empty and Size (stack.c)

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

int stack_empty ( stack S )


{
if ( S==0) {
fprintf ( stderr , "CannotworkwithNULLstack.\n" ) ;
abort ( ) ;
}
return S>top < 0 ;
}
int stack_size ( stack S )
{
if ( S==0) {
fprintf ( stderr , "CannotworkwithNULLstack.\n" ) ;
abort ( ) ;
}
if ( S>top < 0 )
return 0 ;
else
return S>top+1;
}

10/11


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

#include
#include
#include
#include

< s t d i o . h>
< s t d l i b . h>
< a s s e r t . h>
"stack.h"

int main ( )
{
stack S ;
int i ;
S = stack_create ( ) ;
for ( i = 0 ; i < 1000; i++ )
{
stack_push ( S , i ) ;
assert ( stack_top ( S ) == i ) ;
assert ( stack_empty ( S ) == 0 ) ;
assert ( stack_size ( S ) == i + 1 ) ;
}
for ( i = 999 ; i >= 0 ; i )
{
assert ( stack_top ( S ) == i ) ;
stack_pop ( S ) ;
}
stack_destroy ( S ) ;
return 0 ;
}

11/11

You might also like