Stack and queue

591 views

Published on

CSC-391. Data Structure and Algorithm course material

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
591
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
41
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Stack and queue

  1. 1. Stack and Queue CSC 391
  2. 2. 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"
  3. 3. Stack operation
  4. 4. Stack algorithm
  5. 5. 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; }
  6. 6. Stack: linked-list implementation
  7. 7. Stack : PUSH()
  8. 8. Stack : POP()
  9. 9. 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.
  10. 10. Fixed-capacity stack: array implementation
  11. 11. 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.
  12. 12. Queue algorithm
  13. 13. Queue operation
  14. 14. Enqueue
  15. 15. Queue: Implementation
  16. 16. 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.
  17. 17. 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>
  18. 18. 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
  19. 19. 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
  20. 20. Operator Precedence ?: conditional 3 right-to-left = += -= /= *= %= <<= >>= &= ^= = assignment 2 right-to-left , comma 1 left-to-right
  21. 21. 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
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. 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*-
  26. 26. 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 * - + *
  27. 27. 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>
  28. 28. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> Parsing HTML
  29. 29. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <head> Parsing HTML
  30. 30. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <head> <title> Parsing HTML
  31. 31. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <head> <title> Parsing HTML
  32. 32. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <head> Parsing HTML
  33. 33. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <body> Parsing HTML
  34. 34. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <body> <p> Parsing HTML
  35. 35. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <body> <p> <i> Parsing HTML
  36. 36. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <body> <p> <i> Parsing HTML
  37. 37. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <body> <p> Parsing HTML
  38. 38. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> <body> Parsing HTML
  39. 39. <html> <head><title>Hello</title></head> <body><p>This appears in the <i>browswer</i>.</p></body> </html> <html> Parsing HTML

×