Docsity

Stack and its application, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Stack and its application and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

LECTURE 08 – STACK APPLICATIONS

Applications of Stack

  • Stack is one of the most important data structures in computer science
    • Compilers use stacks to evaluate expressions
    • Stacks are great for reversing things, matching up related pairs of things, etc.
    • Stack programming problems:
      • Examine an algebraic expression to see if its braces { } and other operators match
      • Convert infix expressions to postfix or prefix
      • Reverse letters in a string, reverse words in a line, or reverse a list of numbers

Why do we need Infix to Pre/Postfix Conversion?

  • Infix notations are easily understood by Humans; however, the operator precedence has

to be resolved, i.e., 2-35+6 = 2-(35)+

  • The precedence of rules are easily understood by humans, what about machines?
  • The biggest of advantage of Pre/Postfix notations is that the governing rules are

satisfied while parsing the expression

Infix to Postfix

Infix to Postfix using Stack else { while (c has a precedence lower than or equal to the top of the stack) Output += Pop(S) Push(c, S) } } if (c == ')') { while (Pop(S) != '(') Output += Pop(S) } } while (!isEmpty(S)) Output += Pop(S)

23/(2-1)+53 to 2321-/53+

  • 3(4+5)/2 to 345+2/ =