21_Stacks

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

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

Click to edit Master subtitle style © 2009 Dr. Tim Margush Data Structures and Algorithms I The Stack Dr. Tim Margush University of Akron © 2009

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

View Full Document
© 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
© 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

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

View Full Document
© 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 }
© 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

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

View Full Document
© 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(); }
© 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); }

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

View Full Document
© 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
© 2009 Dr. Tim Margush Postfix Evaluation with Stack Operands are pushed

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern