Stack.PDF

# &
(Stack) # &
Y
LX < I
eP" ; ,% 1, h #%
&#
Qh /I%#$} /
7 Q%
c ,T : 2A
.Q%
QO
'h Z@K B #:O #$} Z@K $; 2& :O, 0A
(Top)2A /j
K6X
"1 h Q9
K6X
O
Q9
2A R
(bottom)2A A
, I BO,
&I
$; 7 /Z@K ,@ {:@1 , (Last in First out) LIFO mm2A
:; , & I
$; O, & I
J
7 /Z@K ,@ :@1 ,(First in Last out) FILO 2A
,P2 T` 9P2 K* /j FP2 $; . 7 s" FP2 :@ : & / O , 2A 'K
/& !7 , 9@ Eh
7 2A $ '$
FP2 , 7 9P2 L6$ . O, O
7 Q%
. 7  /
!7 2A '$
7 ; I
2A I
"1 h
# & . 92 [
Push = Write = ADD

Pop = Delete = Read = Remove
2A /j (:: Q7) #
h ,@ (push) 'K
.2A /j I
Q7 < O
,@ (pop)L0M 'K
#
& . :O , 2A U@K n7
:M n 7 Q%
n JX /: < ; I
#%
2A R /
3 #%
.:O, 2A /j Z@K 1#O
7 top N /€ 2A & # & #C R #Y top > n 1
. mm1, mmh m2A 7 Q%
,7 item # n 1 , ^ 2A /
7 /
; JX
.O , #
Stackfull()J
e%m stackfull 2A :OmmA /mmV$ :Omm
procedure Addstack (item :items) ;
begin
if
top <> n
then
begin
top := tap + 1;
stack[top] := item;
end ;
else
stackfull() ;
end;
# & 9 R \O@ stackempty() J
e% 7 L0M ,7 ; I
, Q%
,$ 2A ,@ O top = 0 1
O, 3
)1
procedure
Deletestack (var item : items);
Begin
if top = 0 then stackempty() ;
else
item := stack[top];
top := top – 1 ;
end;
=
# & .CR
(Math Expression Evaluation) ,G /9K %( ,OI
.
(Subroutines Call) J
I ,
$
" .=
(Recursive Procedures) ,21I /& &J
I .H
LY .F[ LC-59 #FJ
:@7 ,I
3 :@ Q%
Y I K :@ ,K
x = A / B − C + D ∗ E − A∗C
g(U '
% /% < : & I O #
; ,U$ /@ &)
A :O O
&T < 1
,M
/
: I
,G K & .Q%
#:O "1 ^ 7 /
,%@ /&'M #
#I
. :O /
O 7 Q%
Y
E,D,C,B,A j K l6n . Q%
#:O $% (Delimiter) #:@@7
: (Operator) !K (Operand) :!K
JK
{O, N*
9; / 7 Q%
,K
T
9; :P :0A, /P 7 :@T& :!K i@A
. :
&#
S
zV !K :@ ,T /j gV% /9I n7
. :@O, U 'h &!K
: J
%
( ^ )
(/ ) TPB ( ∗ )FGB(-) z B(+)E :,G /&!K
(g(U QTh $)div (g(U #:h) mod (-)Minus ::@ ( Unary Operators ),Y /&!K
:N %
/T1) B (< >)` B (=)/T B (<) Y 7 B (>)1) ::@ (Relational)/
V
/&!K
(<=)/TY 7(>=)
.: 7 & (Strings) &O 3I
A /
Q%
Y Q%
,G /&:!K / 2 &!K 7
.:O, True, False Qq I
,Y 2& Q%
/
V
/&!K 'O 7 ,K *
: N% %
XOR , OR , AND , NOT :@ :@T& ,PV@ /&!K
,G !K
/
V
!K
,PV@ !K
Unary
binary
<
NOT
-
+
>
AND
Mod
-
=
OR
Div
*
<>
XOR
/
>=
^
<=
H
/&!K . &, QT (Priority) Q
!K & O O
&K I
,TY ,OI
2& Y; /
. h ,PV@ (NOT) ƒP : Q
j (Minus),Y z .:
,TY Q
/
V
.:O :&
$ N*
Q%
„ I
,OI
:@O & * Q
& !K 1
: JY%A h
(Priority) "/]: .:
N:P j unary /& !K .
.Q%
/: N:P NOT .=
,G !K .H
.:
N:P A /
V
/& !K .
Q
!K
5
(unary) -
4
NOT
3
2
1
* /
^
div mod AND
+ - OR XOR
<> >= <=
<
>
=
:O , N*
U I K :Jn
A/B*C
>A O, N*
FG >?% TP Q%
„ I
J
JY%A h zX :@T& Q
& TP FG
A/B*C
⇒
(A/B) * C
LY .F[ B0 .5 ^0
A+B
+AB
(Infix)/: R .
(Prefix),T9 /:2A R .=
AB+ (Postfix)
,T9 /:TA R .H
RPN (Reverse Polish Notation) #0 L0, B0
,@& B &K O ,G J /&3 . Q%
:@% T ,G /&K ,I
/
2A $%
(Lukaseiwicz) o%7mm ,mmT9 :G .7 :&
$ *
,6Y2 B :O, ,I
?7 e% 9K
ld R 3 .:O #
2 (Prefix) /:2A 'YO :@
, ,G /9K 7 Q%
#
mm2
R (Postfix)/:TA R .1, h :!K I
'h !K 3 . :@1 , ,T9 R
.Q%
c%@ 2A !K /
,T9 3 .:@&, h :!K I
>A !K ; N RPN ,T9
:Jn
:O, ': I 'YO A * B + C * D K
A ∗ B + C ∗ D ⇒ AB ∗ CD ∗ +
./0,& F[ #C ./00- F[ /F: 7 /
#
,!7 V
: &!K Q
J: I
#%
J
3
2A '%Q
O , 2A 7,!%Q
ISP(in stack priority)
ICP(incoming priority)
)
…
…
^ , -(unary)
3
4
*,/
2
2
+,-
1
1
(
0
4
'%
.&, h c Q%
„ I
2A &:!K T, 2A &!K Q%
#% T 7 3
ISP I
O 2A :&
$, 7 ,!% Q
,@ ; ICP : :&, h 2A '$
7 /!K :@7 Qh
&!K : O /
h :O h ICP(x) > ISP(y)) O YU .:O 2 "1 h 2A 7 ,!%
c 2A I
&!K , Q%
„ I
,I
A I
>A . & h &:!K / @7 pop 2A I
.@7 , pop A j I
:Jn
:& R A * B + C * D /:TA K
Q%
:!K B #; . , 2A '$
∗ >?% . T , 2A ; @, A :!K :
2A :
, >A ICP(+) < ISP(∗) Q
J: zX :@7 Qh . 1, ^ + T, A @7
>?% .T, 2A K @7 C @7
.T@ AB @7 @7 pop 2A I
∗ : I
h
@7 D #$j 7 push 2A '$
FG , Q%
ICP(∗) > ISP(+) %, FG +
:;, FG :
@7 pop l67 2A : @7
Q%
#:O N ,I
7 . &, h 2A /&K
. Q%
#:O 'UM k" K Postfix A . E !K : *
*
A
(1)
A∗ B + C ∗ D
*
+
AB
(2)
AB*
(3)
+
AB*C
(4)
*
+
AB*C
(5)
+
AB*CD
(6)
⇒ postfix : AB *CD * +
:Jn
: :T@ I K postfix
A + ( B − C ) * ( B + C ) / ( D + 2) − E
: 'M
T I )
A 7 & : %, )
A 7 ,@& :@7 Qh { Q%
#:O #
2 I 7 'M
@7 pop 2A I
-
(1)
(
(
*
+
+
+
+
+
(2)
(3)
(4)
(5)
(6)
+
(
+
(
*
(
*
*
/
/
(
/
+
+
+
+
+
+
(7)
(8)
(9)
(10)
(11)
(12)
/
4
+
-
(13)
(14)
(15)
⇒ postfix : ABC − BC + *D 2 + / + E −
@7 pop 2A I
T I )
A 7 & : % , T )
A 7 ,@& :@7 Qh : #_0
'M #
/
:O, ?7 /) 'h Q%
,U
3 postfix .%( 3 @, 7V&
.Q%
#:O #
postfix .%( /
/
#% ,T 3 I .:O, qr :;7 ,!$ ,T
./0,& F[ #FJ .C . N " N
@7 "G
K &)
A NIj : : & Q
Q%
,"7.Q%
N% & /
01)
A 3 3 K I
!K QT"7 ,@ T@ postfix K & /
@7 SO )
A , I
#;
T@ &; :Jn
:T@ /
01 )
A 3 I
#%
'P K postfix
B ∗C + D ∗ 2
B ∗C + D ∗ 2
⇒
((B * C ) + (D * 2))
⇒ postfix : BC * D 2 * +
:Jn
:T@ /
01 )
A 3 I
#%
I K postfix
A + (B - C) * (B + C)/(D + 2) - E ⇒ (( A + (((B - C)*(B + C)) / (D + 2))) - E)
BC-
BC+
D2+
BC-BC+*
BC-BC+*D2+/
ABC-BC+*D2+/+
ABC-BC+*D2+/+E-
C
A + (B - C) * (B + C)/(D + 2) - E ⇒ postfix : ABC-BC+*D2+/+E-
LX !K h /* QT"7 eP" Q%
c%@ & (prefix)/:2A K %( /
@w& 3 & h „ /% !K &:!K Q%
:Jn
:T@ I K prefix
B*C+D/E
⇒ ( ( B*C ) + ( D/E ) )
*BC
/DE
+*BC/DE
Prefix : + * B C / D E
:Jn
::T@ I K postfix,prefix
A*B / C +(A – B)^ C +D/ E * F ⇒ (((A*B) / C) + (( A – B) ^ C))+((D/E)* F))
AB*
AB-
AB*C/
AB-C^
DE/
DE/F*
AB*C/AB-C^+
AB*C/AB–C^+DE/F*+
Postfix: AB*C/AB-C^+DE/F*+
prefix : ++/*ABC^-ABC*/DEF
LY F[ S ./[ /] LC9
!K < #1& .@7, RA Q%
„ I
K postfix 7 Q%
'YO ,G K < ,I
) #:; Q% :K @7, L0M :!K #; @7, JK
,!h :!K / ; 7 $
.@Y $ / !K 7 &, :P; 7 .@7,
D
.:@7 ,I
3 * 4 + 5 * 6 K :Jn
.@7 , RA T , postfix , ! Q%
„ I
3 * 4 + 5 * 6 ⇒ postfix : 34 * 56 * + = 42
6
4
5
5
30
3
3
12
12
12
12
42
3 :
$
Push 4
%(
3*4
Push 5
Push 6
%(
5*6
%(
12+30
1
2
3
4
5
6
7
:Jn
.:@7 FTM –Q^4 :P :O Q = 2 1
-Q^4 ⇒ postfix : Q 4 ^ - = 24^- =16
4
2
2
16
-16
2 :
$
Push 4
%(
2^4
!K JK
…
8