Docsity

Implementing Stacks and Queues: A Comparative Study, Slides of Aeronautical Engineering

Toggle sidebar
bg1
Stack
• Stack: A list with insertion and deletion
both take place at one end: the top
– Main operations
• Push
– by one
• Pop
– one
– Other operations
• …
LIFO policy
addition makes older items inaccessible
Stack
Implementing Stack using Array
5 3 12 0 42
S 8
Top[S]=6
5 3 12 0 42 17 2
S 8
Top[S]=8
5 3 12 0 42 17 2
S 8
Top[S]=7
Empty(S)
if top[S]=1 then
return true
else
return false
New item added on top of stack, size of stack increases
Top-item removed from stack, decreasing stack size by
Initialize, Empty, Size, Top, Stack_Top, Display,
•Implements a
–New
• Example use of stacks:
• Implementations used:
– Arrays or linked lists
1234 5 6 7 9 10
1234 5 6 7 9 10
1234 5 6 7 9 10
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Implementing Stacks and Queues: A Comparative Study and more Slides Aeronautical Engineering in PDF only on Docsity!

Stack

•^

Stack

: A list with

insertion

and

deletion

both take place at

one

end: the top

–^

Main operations •^

Push^ –

by one

-^

Pop^ –

one

–^

Other operations •^

…

LIFO

policy

addition

makes older items inaccessible

Stack

Implementing Stack using Array

S

Top[S]=

S

Top[S]=

S

Top[S]=

Empty(

S

if

top

[ S

]=1 then

return

true

else

return

false

New item added on top of stack, size of stack increasesTop-item removed from stack, decreasing stack size by Initialize, Empty, Size, Top, Stack_Top, Display,

Implements a–^

New

• Example use of stacks:• Implementations used:

  • Arrays or linked lists

Push (

S

x

if

STACK-FULL(

S

)^

then

error “

overflow

else

S

[ top

[

S

]] :=

x

top

[ S

] :=

top

[ S

]+

Pop (

S

if

STACK-EMPTY(

S

)^

then

error “

underflow

else

top

[ S

] :=

top

[ S

]-

return

S

[ top

[ S

]]

test_stack.adb

Performance and Limitations

of

My_Stack

stack defined a priori and cannot be changedstack causes an implementation-specificexception

Queue FIFO

policy

my_stack.adsmy_stack.adb

• Performance

  • Let N be the number of elements in the– The space used is O(N)– Each operation used is O(1)

• Limitations

  • The maximum size of the stack must be– Trying to push a new element into a full

• A list of elements with

  • Insertion: at end of list, tail– Deletion: at start of list, head

• Implements a

  • Queues are fair when someone has to wait

• Examples:

Infix vs. Postfix

Examples Using Stacks

[^

]

How to Evaluate Postfix

A program can evaluate postfix expressions for

I in 1 .. length

loop

If

Is_Number(expr(I)) = true

then

push expr(I)

If

Is_Operator(expr(I))

then

pop two numbers from the stackperform operationpush result onto stack

result is on top of stack

Evaluating infix expressions

•^

one for numbers and one

for operators for

I in 1 .. length

loop

If

Is_Number(expr(I)) = true

then

push expr(I) onto operand_stack If

Is_Operator(expr(I))

then

push expr(I) onto operator_stack If

expr(I) = ‘)’

then

pop 2 numbers from operand_stackpop an operator from the operator_stackperform operationpush the result onto the operand_stack

• Infix vs. postfix

  • How to evaluate postfix– How to evaluate infix

• Convert Infix to Postfix

my_expression_converter.adsmy_expression_converter.adbconverter_test.adb^ use these for this weeks PSET

by reading the expression from left to right end loop How is a bad postfix expression indicated?

Need two stacks, Infix Expressions end loop -- The top of stack contains the result.

Corresponding Postfix

(b * b – 4 * a * c) / (2 * a)

b b * 4 a * c * - 2 a * /

Infix to Postfix: Example

Infix Expression

Postfix Expression

Infix to Postfix

post_fix := “”Create(Op_Stack) for

I

in

1 .. Length

loop

If

Is_Operand(expr(I)) = true

then

Append(post_fix, expr(I))

If

Is_Operator(expr(I)) = true

then

Process_Next_Operator(expr(I))

end loop

Process_Next_Operator

Done := False loop

If

Is_Empty(Op_Stack)

or

next_op is ‘(‘,

set Done to True Elsif

precedence(next_op) > precedence(top_operator) Push next_op onto Op_stack--

ensures higher precedence operators evaluated first Set Done to True Else

Pop the operator_stack If

operator popped is ‘(‘

set Done to True

Else

Done = True

-- string post_fix has the result

push next_op onto Op_Stack

append operator popped to post_fix string

exit when end loop