You are on page 1of 4

19 2011

59 .
(push) (pop)
.
- `' .
1 n (
)
i, i .
,
i- .
i ''
. ,
i+1
i
. -
( ).
1 .
Algorithm 1
1: Push
2: // p, index, element //
3: ! p
4: ! index element

5: i i + 1
6: p[i] element
7: Push

Algorithm 2
1: Pop
2: // p, index //
3: ! p
4: ! index
5: !
6: ! -
7: i = 0
8:
apotelesma -
9:
10:
apotelesma p[i]
11:
ii-1
12: _
13: // apotelesma //
14: Pop
2 .

Pascal. .
:
1
2
3

program stack_program ;
const
stack_size = 20;

4
5
6

type
stack = array [1.. stack_size ] of integer ;

, stack
array[1..stack_size] of integer
1 stack_size. stack_size
20
.
push listing 1. 1 listing 1
.

. ,
2

Listing 1: push
1
2
3
4
5
6
7
8
9
10
11

function push(var s: stack ; var idx: integer ; e: integer ) : boolean ;


begin
if idx = stack_size then
push := false
else
begin
idx := idx + 1;
s[idx] := e;
push := true;
end;
end;

Listing 2: pop
1
2
3
4
5
6
7
8
9
10
11

function pop(var s: stack ; var idx: integer ; var e: integer ) : boolean ;


begin
if idx = 0 then
pop := false
else
begin
e := s[idx ];
idx := idx - 1;
pop := true;
end;
end;

.

.
. 3-4.
false. 7 8
1 true.
2 listing 2.
.
( )
-.
3

.
.
( )
( -).
:
true false
.
functions Pascal .
4 9.
false .
true
.
true.

. Pascal
var , e.
stack.zip

You might also like