stack and queue

40
Stack and Queue CSC 391

Upload: shakila-mahjabin

Post on 02-Jun-2015

188 views

Category:

Education


2 download

DESCRIPTION

CSC-391. Data Structure and Algorithm course material

TRANSCRIPT

Page 1: Stack and queue

Stack and QueueCSC 391

Page 2: Stack and queue

Fundamental data types.

Value: collection of objects.Operations: insert, remove, iterate, test if empty.

Stack and Queue

Stack. Examine the item most recently added.Queue. Examine the item least recently added.

LIFO = "last in first out“

FIFO = "first in first out"

Page 3: Stack and queue

Stack operation

Page 4: Stack and queue

Stack algorithm

Page 5: Stack and queue

Maintain pointer first to first node in a singly-linked list.

Push new item before first.

Pop item from first.

Stack: linked-list implementation

inner classprivate class Node{String item;Node next;}

Page 6: Stack and queue

Stack: linked-list implementation

Page 7: Stack and queue

Stack : PUSH()

Page 8: Stack and queue

Stack : POP()

Page 9: Stack and queue
Page 10: Stack and queue

Fixed-capacity stack: array implementationUse array s[] to store N items on stack.

push(): add new item at s[N].

pop(): remove item from s[N-1].Defect. Stack overflows when N exceeds capacity.

Page 11: Stack and queue

Fixed-capacity stack: array implementation

Page 12: Stack and queue

Queue: Implementation

Maintain one pointer first to first node in a singly-linked list.

Maintain another pointer last to last node.

Dequeue from first.

Enqueue after last.

Page 13: Stack and queue

Queue algorithm

Page 14: Stack and queue

Queue operation

Page 15: Stack and queue

Enqueue

Page 16: Stack and queue

Queue: Implementati

on

Page 17: Stack and queue

Application

Parsing in a compiler.Undo in a word processor.Back button in a Web browser.PostScript language for printers.Implementing function calls in a compiler.Parsing code:Matching parenthesisXML (e.g., XHTML)Reverse-Polish calculatorsAssembly language

Goal. Evaluate infix expressions.

Page 18: Stack and queue

Polish notation

Infix notation : a + bPrefix notation : + a bPostfix notation: a b + (Reverse Polish Notation)

Prefix notation was introduced by the Polish logician Lukasiewicz, and is sometimes called “Polish notation”.

Postfix notation is sometimes called “reverse Polish notation” or RPN.

infix postfix prefix(a + b) * c a b + c * * + a b ca + (b * c) a b c * + + a * b c

Infix form : <identifier> <operator> <identifier>Postfix form : <identifier> <identifier> <operator> Prefix form : <operator> <identifier> <identifier>

Page 19: Stack and queue

Token Operator Precedence Association

( ) [ ] -> .

function call array element struct or union member

17 left-to-right

-- ++ increment, decrement2 16 left-to-right

-- ++ ! - - + & * sizeof

decrement, increment3

logical not one’s complement unary minus or plus address or indirection size (in bytes)

15 right-to-left

(type) type cast 14 right-to-left

* / % mutiplicative 13 Left-to-right

Operator Precedence

Page 20: Stack and queue

Operator Precedence

+ - binary add or subtract 12 left-to-right

<< >> shift 11 left-to-right

> >= < <=

relational

10 left-to-right

== != equality 9 left-to-right

& bitwise and 8 left-to-right

^ bitwise exclusive or 7 left-to-right

bitwise or 6 left-to-right

&& logical and 5 left-to-right

logical or 4 left-to-right

Page 21: Stack and queue

Operator Precedence

?: conditional 3 right-to-left

= += -= /= *= %= <<= >>= &= ^= =

assignment

2 right-to-left

, comma

1 left-to-right

Page 22: Stack and queue

Prefix operation using stack

Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses:

( ( A + B) * ( C + D ) )

( + A B * ( C + D ) )

* + A B ( C + D )

* + A B + C D

Page 23: Stack and queue

Infix Stack Prefix Operation( ( ( A + B ) * ( C - E ) ) / ( F + G ) ) ---- ---- ----

( ( A + B ) * ( C - E ) ) / ( F + G ) ) ( ----- push

( A + B ) * ( C - E ) ) / ( F + G ) ) (( ---- push

A + B ) * ( C - E ) ) / ( F + G ) ) ((( ---- push

+ B ) * ( C - E ) ) / ( F + G ) ) ((( A output

B ) * ( C - E ) ) / ( F + G ) ) (((+ A push

) * ( C - E ) ) / ( F + G ) ) (((+ AB output

* ( C - E ) ) / ( F + G ) ) (( AB+ pop

( C - E ) ) / ( F + G ) ) ((* AB+ push

C - E ) ) / ( F + G ) ) ((*( AB+ push

- E ) ) / ( F + G ) ) ((*( AB+C output

E ) ) / ( F + G ) ) ((*(- AB+C push

) ) / ( F + G ) ) ((*(- AB+CE output

Prefix operation using stack

Page 24: Stack and queue

Infix Stack Prefix Operation) / ( F + G ) ) ((* AB+CE- pop

/ ( F + G ) ) ( AB+CE-* pop

( F + G ) ) (/ AB+CE-* push

F + G ) ) (/( AB+CE-* push

+ G ) ) (/( AB+CE-*F output

G ) ) (/(+ AB+CE-*F push

)) (/(+ AB+CE-*FG output

) (/ AB+CE-*FG+ pop

---- ---- AB+CE-*FG+/ pop

Prefix operation using stack

Page 25: Stack and queue

Postfix operation using stack

Infix Stack Postfix OperationA * (B + C) – D / E ---- ---- ----

* (B + C) – D / E ---- A output

(B + C) – D / E * A push

B + C) – D / E *( A push

+ C) – D / E *( AB output

C) – D / E *(+ AB push

) – D / E *(+ ABC output

– D / E * ABC+ pop

D / E - ABC+* pop and push

/ E - ABC+*D output

E -/ ABC+*D push

----- -/ ABC+*DE output

----- ---- ABC+*DE/- pop

The precedence of operator on the top of Stack ‘*‘ is more than that of Minus. So we pop multiply

Page 26: Stack and queue

Postfix operation using stack

Infix Postfix

2+3*4 a*b+5 (1+2)*7 a*b/c (a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c

234*+ ab*5+ 12+7* ab*c/ abc-d+/ea-*c* ab/c-de*ac*-

Page 27: Stack and queue

Convert from Infix to Prefix and Postfix (Practice)

• x• x + y• (x + y) - z• w * ((x + y) - z)• (2 * a) / ((a + b) * (a - c))

Convert from Postfix to Infix (Practice)

• 3 r -• 1 3 r - +• s t * 1 3 r - + +• v w x y z * - + *

Page 28: Stack and queue

Parsing HTML

HTML is made of nested– opening tags, e.g., <some_identifier>, and– matching closing tags, e.g., </some_identifier>

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

Page 29: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html>

Parsing HTML

Page 30: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <head>

Parsing HTML

Page 31: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <head> <title>

Parsing HTML

Page 32: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <head> <title>

Parsing HTML

Page 33: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <head>

Parsing HTML

Page 34: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <body>

Parsing HTML

Page 35: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <body> <p>

Parsing HTML

Page 36: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <body> <p> <i>

Parsing HTML

Page 37: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <body> <p> <i>

Parsing HTML

Page 38: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <body> <p>

Parsing HTML

Page 39: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html> <body>

Parsing HTML

Page 40: Stack and queue

<html><head><title>Hello</title></head><body><p>This appears in the <i>browswer</i>.</p></body>

</html>

<html>

Parsing HTML