Stack and queue

MJabin 1,284 views 40 slides Jul 01, 2014
Slide 1
Slide 1 of 40
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40

About This Presentation

CSC-391. Data Structure and Algorithm course material


Slide Content

Stack and Queue CSC 391

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"

Stack operation

Stack algorithm

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 class private class Node { String item; Node next; }

Stack: linked-list implementation

Sta ck : PUSH()

Sta ck : POP()

Fixed-capacity stack: array implementation Use 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.

Fixed-capacity stack: array implementation

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 .

Queue algorithm

Queue operation

Enqueue

Queue : Implementation

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 parenthesis XML (e.g., XHTML) Reverse-Polish calculators Assembly language Goal. Evaluate infix expressions.

Polish notation Infix notation : a + b Prefix notation : + a b Postfix 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 c a + (b * c) a b c * + + a * b c Infix form : <identifier> <operator> <identifier> Postfix form : <identifier> <identifier> <operator> Prefix form : <operator> <identifier> <identifier>

Operator Precedence

Operator Precedence

Operator Precedence

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

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

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

Postfix operation using stack Infix Stack Postfix Operation A * (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

Postfix operation using stack

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 * - + *

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>

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

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

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

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

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

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

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

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

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

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

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

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