1 / 83

# Stack ADT - PowerPoint PPT Presentation

Stack ADT. Operations Push, Pop, Top, isEmpty Application: Expression Evaluation Arithmetic Expression Infix-to-Postfix Postfix to Quadruples Boolean Expressions (Assignment #1) Infix-to-Postfix Postfix to Quadruples. Implementing a Stack. Using Vector /Array

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

• Operations

• Push, Pop, Top, isEmpty

• Application: Expression Evaluation

• Arithmetic Expression

• Infix-to-Postfix

• Boolean Expressions (Assignment #1)

• Infix-to-Postfix

• Using Vector /Array

• requires estimate of maximum list length

• may grow dynamically

• Ø = empty slots

• Can contain varied data/objects (not necessarily homogeneous)

212 rules!

Ø

Ø

Ø

30−0

Golf #1

top

• flexible, adjusts to problem size

top

Ø

212 rules!

Golf #1

30−0

0

1

2

3

4

5

6

7

8

2

-1

7

-1

1

4

3

6

0

Ø

• cursor implementation

212 rules!

Ø

Ø

30−0

top = 5

Golf #1

freelist = 8

Ø

Ø

Ø

• push O(1)* O(1)

• pop O(1) O(1)

• top/peek O(1) O(1)

• isEmpty O(1) O(1)

*assuming no need for Vector/Array expansion

infix expression: z = a * ( x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression:

@

infix expression: z= a * ( x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z

@

infix expression: z= a * ( x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z

=

@

infix expression: z=a * ( x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a

=

@

infix expression: z=a* ( x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a

*

=

@

infix expression: z=a*( x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a

(

*

=

@

infix expression: z=a*(x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x

(

*

=

@

infix expression: z=a*(x + y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x

+

(

*

=

@

infix expression: z=a*(x +y ) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y

+

(

*

=

@

infix expression: z=a*(x +y) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y +

(

*

=

@

infix expression: z=a*(x +y) + z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y +

*

=

@

infix expression: z=a*(x +y)+ z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y + *

=

@

infix expression: z=a*(x +y)+ z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y + *

+

=

@

infix expression: z=a*(x +y)+z * c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y + *z

+

=

@

infix expression: z=a*(x +y)+z* c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y + *z

*

+

=

@

infix expression: z=a*(x +y)+z*c ^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y + *z c

*

+

=

@

infix expression: z=a*(x +y)+z*c^ ( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y + *z c

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2 − ( − d + w ) ) / x ;

postfix expression: z a x y + *zc

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2− ( − d + w ) ) / x ;

postfix expression: z a x y + *z c 2

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2− ( − d + w ) ) / x ;

postfix expression: z a x y + *z c 2

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(− d + w ) ) / x ;

postfix expression: z a x y + *z c 2

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(− d + w ) ) / x ;

postfix expression: z a x y + *z c 2

~

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d + w ) ) / x ;

postfix expression: z a x y + *z c 2 d

~

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+ w ) ) / x ;

postfix expression: z a x y + *z c 2 d

~

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+ w ) ) / x ;

postfix expression: z a x y + *z c 2 d ~

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+ w ) ) / x ;

postfix expression: z a x y + *z c 2 d ~

+

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w ) ) / x ;

postfix expression: z a x y + *z c 2 d ~ w

+

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w) ) / x ;

postfix expression: z a x y + *z c 2 d ~w

+

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w) ) / x ;

postfix expression: z a x y + *z c 2 d ~w +

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w) ) / x ;

postfix expression: z a x y + *z c 2 d ~w +

(

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w) ) / x ;

postfix expression: z a x y + *z c 2 d ~w +

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w)) / x ;

postfix expression: z a x y + *z c 2 d ~w +

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w)) / x ;

postfix expression: z a x y + *z c 2 d ~w + −

(

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w)) / x ;

postfix expression: z a x y + *z c 2 d ~w + −

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/ x ;

postfix expression: z a x y + *z c 2 d ~w + −

^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/ x ;

postfix expression: z a x y + *z c 2 d ~w + − ^

*

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/ x ;

postfix expression: z a x y + *z c 2 d ~w + − ^ *

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/ x ;

postfix expression: z a x y + *z c 2 d ~w + − ^ *

/

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/x ;

postfix expression: z a x y + *z c 2 d ~w + − ^ * x

/

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/x;

postfix expression: z a x y + *z c 2 d ~w + − ^ * x

/

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/x;

postfix expression: z a x y + *z c 2 d ~w + − ^ * x /

+

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/x;

postfix expression: z a x y + *z c 2 d ~w + − ^ * x / +

=

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/x;

postfix expression: z a x y + *z c 2 d ~w + − ^ * x / + =

@

infix expression: z=a*(x +y)+z*c^( 2−(−d+w))/x;

postfix expression: z a x y + *z c 2 d ~w + − ^ * x / + =

postfix expression: z a x y + * z c 2 d ~w + − ^ * x / + =

z

postfix expression: za x y + * z c 2 d ~w + − ^ * x / + =

a

z

postfix expression: zax y + * z c 2 d ~w + − ^ * x / + =

x

a

z

postfix expression: zaxy + * z c 2 d ~w + − ^ * x / + =

y

x

a

z

postfix expression: zaxy+* z c 2 d ~w + − ^ * x / + =

y

x

a

z

postfix expression: zaxy+* z c 2 d ~w + − ^ * x / + =

t0

a

z

+ x y t0

postfix expression: zaxy+*z c 2 d ~w + − ^ * x / + =

+ x y t0

t0

a

z

postfix expression: zaxy+*z c 2 d ~w + − ^ * x / + =

+ x y t0

* a t0 t1

t1

z

postfix expression: zaxy+*zc 2 d ~w + − ^ * x / + =

+ x y t0

* a t0 t1

z

t1

z

postfix expression: zaxy+*zc2 d ~w + − ^ * x / + =

+ x y t0

* a t0 t1

c

z

t1

z

postfix expression: zaxy+*zc2d ~w + − ^ * x / + =

+ x y t0

* a t0 t1

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w + − ^ * x / + =

+ x y t0

* a t0 t1

d

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w + − ^ * x / + =

+ x y t0

* a t0 t1

d

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w + − ^ * x / + =

+ x y t0

* a t0 t1

~ d t2

t2

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w + − ^ * x / + =

+ x y t0

* a t0 t1

~ d t2

w

t2

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w+ − ^ * x / + =

+ x y t0

* a t0 t1

~ d t2

w

t2

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w+ − ^ * x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

t3

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w+− ^ * x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

t3

2

c

z

t1

z

postfix expression: zaxy+*zc2d~w+− ^ * x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 2 t3

- w t3 t4

t4

c

z

t1

z

postfix expression: zaxy+*zc2d~w+−^* x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

t4

c

z

t1

z

postfix expression: zaxy+*zc2d~w+−^* x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

t5

z

t1

z

postfix expression: zaxy+*zc2d~w+−^*x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

t5

z

t1

z

postfix expression: zaxy+*zc2d~w+−^*x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

t6

t1

z

postfix expression: zaxy+*zc2d~w+−^*x / + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

x

t6

t1

z

postfix expression: zaxy+*zc2d~w+−^*x/ + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

x

t6

t1

z

postfix expression: zaxy+*zc2d~w+−^*x/ + =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

t7

t1

z

postfix expression: zaxy+*zc2d~w+−^*x/+ =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

t7

t1

z

postfix expression: zaxy+*zc2d~w+−^*x/+ =

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

+ t1 t7 t8

t8

z

postfix expression: zaxy+*zc2d~w+−^*x/+=

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

+ t1 t7 t8

t8

z

postfix expression: zaxy+*zc2d~w+−^*x/+=

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

+ t1 t7 t8

= t8 z

postfix expression: zaxy+*zc2d~w+−^*x/+=

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

+ t1 t7 t8

t8

z

postfix expression: zaxy+*zc2d~w+−^*x/+=

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

+ t1 t7 t8

= t8 z

postfix expression: zaxy+*zc2d~w+−^*x/+=

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

+ t1 t7 t8

= t8 z

infix expression: z=a * (x +y)+z * c^( 2 −(−d+w))/x;

--- t0 --- -t2-

----- t1 ------ ---- t3 ----

-------- t4 --------

----------- t5 -----------

-------------- t6 -------------

----------------- t7 --------------

---------------------- t8 ---------------------------

+ x y t0

* a t0 t1

~ d t2

+ t2 w t3

- w t3 t4

^ c t4 t5

* Z t5 t6

/ t6 x t7

+ t1 t7 t8

= t8 z

postfix expression: zaxy+*zc2d~w+−^*x/+=