21_Stacks - Click to edit Master subtitle style © 2009 Dr....

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style © 2009 Dr. Tim Data Structures and Algorithms I The Stack Dr. Tim Margush University of Akron © 2009 © 2009 Dr. Tim 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 © 2009 Dr. Tim 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 © 2009 Dr. Tim © 2009 Dr. Tim Stack Interface © 2009 Dr. Tim 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 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 © 2009 Dr. Tim © 2009 Dr. Tim () Matching © 2009 Dr. Tim 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 () Matching © 2009 Dr. Tim 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); } © 2009 Dr. Tim Postfix • Postfix notation means the operators follow the operand – Infix: a + b – Postfix: a b + • Postfix expressions do not need parentheses or operator precedence...
View Full Document

This note was uploaded on 02/22/2011 for the course CS 463 taught by Professor Dr. tim margush during the Fall '09 term at The University of Akron.

Page1 / 29

21_Stacks - Click to edit Master subtitle style © 2009 Dr....

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online