21_Stacks - Data Structures and Algorithms I Click to edit...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style © 2009 Dr. Tim Margush Data Structures and Algorithms I The Stack Dr. Tim Margush University of Akron © 2009
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
© 2009 Dr. Tim Margush Goals Be able to describe the basic organization and operations for a stack ADT Know the tradeoffs for common implementations of a stack Be able to use a stack to solve problems Pared parenthesis Postfix evaluation
Image of page 2
© 2009 Dr. Tim Margush Stack A LIFO Collection Last in, First out Last in item is referred to as the top item The operation to add is push The operation to remove is push The operation to get is peek
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
© 2009 Dr. Tim Margush Stack Interface public interface Stack<E>{ void push(E anEntry); E pop(); E peek(); boolean isEmpty(); void clear(); int search(E anEntry); //0-based position of elt relative to top boolean isFull(); //generally stacks are not full }
Image of page 4
© 2009 Dr. Tim Margush Matching Parenthesis A parenthesized expression must be well-formed ({[{}]}), (){}(), {([]())[]} Not }, (], ([)], (())) A stack can be used to check well- formedness Open parens are pushed Closed parens are matched Hopefully
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
© 2009 Dr. Tim Margush () Matching private static final String OPEN_PAREN = "[{(<"; private static final String CLOSE_PAREN = "]})>"; public static boolean balanced(String exp){ Stack<Character> stk = new Stack<Character>(); boolean balanced = true; int i=0; while (balanced && i<exp.length()){ char c = exp.charAt(i++); if (OPEN_PAREN.indexOf(c)>=0){ stk.push(c); }else if (CLOSE_PAREN.indexOf(c)>=0){ balanced = !stk.isEmpty() && match(stk.pop(),c); } } return balanced && stk.isEmpty(); }
Image of page 6
© 2009 Dr. Tim Margush () Matching private static final String OPEN_PAREN = "[{(<"; private static final String CLOSE_PAREN = "]})>"; private static boolean match(char open, char close){ return OPEN_PAREN.indexOf(open) == CLOSE_PAREN.indexOf(close); }
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
© 2009 Dr. Tim Margush Postfix Postfix notation means the operators follow the operand Infix: a + b Postfix: a b + Postfix expressions do not need parentheses or operator precedence conventions Postfix evaluation uses a stack while scanning the expression from left to
Image of page 8
© 2009 Dr. Tim Margush Postfix Evaluation with Stack Operands are pushed
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern