Presentation is loading. Please wait.

Presentation is loading. Please wait.

CST230 -- Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.

Similar presentations


Presentation on theme: "CST230 -- Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing."— Presentation transcript:

1 CST230 -- Razdan et al http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing Studies razdan@asu.edu http://dcst2.east.asu.edu/~razdan/cst230/

2 CST230 -- Razdan et al2 Stacks A stack is a data structure of ordered items such that items can be inserted and removed only at one end (called the top). It is a last-in-first-out (LIFO) data structure typical interface: –isEmpty –peek (also called top) –pop –push –size

3 CST230 -- Razdan et al3 Sample Stack Implementation from Main pages 320 ++ array implementation (use array to store stack items) methods: clone, ensureCapactiy, getCapacity, isEmpty, peek, pop, push, size, trimtoSize

4 CST230 -- Razdan et al4 public class ObjectStack implements Cloneable{ private Object[] data; private int manyItems; public ObjectStack(){ final int INITIAL_CAPACITY = 10; manyItems = 0; data = new Object[ INITIAL_CAPACITY ]; } public ObjectStack( int initialCapacity ){ if( initialCapacity < 0 ) throw new IllegalArgumentException( "initialCapacity too small “ + initialCapacity); manyItems = 0; data = new Object[ initialCapacity ]; }

5 CST230 -- Razdan et al5 public Object clone(){ ObjectStack answer; try{ answer = (ObjectStack) super.clone(); } catch( CloneNotSupportedException e ){ throw new RuntimeException( "This class does not implement Cloneable."); } answer.data = (Object[]) data.clone(); return answer; }

6 CST230 -- Razdan et al6 public void ensureCapacity( int minimumCapacity ){ Object biggerArray[]; if( data.length < minimumCapacity ){ biggerArray = new Object[ minimumCapacity ]; System.arraycopy( data, 0, biggerArray, 0, manyItems ); data = biggerArray; } public int getCapacity(){ return data.length; } public boolean isEmpty(){ return( manyItems == 0 ); }

7 CST230 -- Razdan et al7 public Object peek(){ if( manyItems == 0 ) throw new EmptyStackException(); return data[manyItems-1]; } public Object pop(){ Object answer; if( manyItems == 0 ) throw new EmptyStackException(); answer = data[--manyItems]; data[manyItems] = null; return answer; }

8 CST230 -- Razdan et al8 public Object peek(){ if( manyItems == 0 ) throw new EmptyStackException(); return data[manyItems-1]; } public Object pop(){ Object answer; if( manyItems == 0 ) throw new EmptyStackException(); answer = data[--manyItems]; data[manyItems] = null; return answer; }

9 CST230 -- Razdan et al9 public void trimToSize(){ Object trimmedArray[]; if( data.length != manyItems ){ trimmedArray = new Object[manyItems]; System.arraycopy( data, 0, trimmedArray, 0, manyItems ); data = trimmedArray; }

10 CST230 -- Razdan et al10 Stack Applications Balanced Parentheses Expression evaluation Postfix Expression evaluation

11 CST230 -- Razdan et al11 Balanced Parentheses main idea: –scan expression from left to right. –whenever open paren(, {, [ is encountered, push on stack. –whenever close paren ), }, ] is encountered pop matching open paren off stack. if the matching paren is not at the top of the stack, parantheses were not balanced –the stack should be empty after all characters in the expression have been encountered. Example: {[X+Y*(Z+7)]*(A+B)}

12 CST230 -- Razdan et al12 Expression Evaluation evaluate well-parenthesized expression main idea: –Push open paren ( to stack –push numbers to stack –push operators to different stack –when close paren encountered, pop operation, two numbers, and open paren. perform calculation, and push result –stack should contain a single result after processing entire expression. Example: (((6+9) / 3) * (6 – 4))

13 CST230 -- Razdan et al13 Notations for Arithmetic Expressions we typically use infix notation for arithmetic expressions, in which the operator is placed in between the operands. –e.g., 2 + 3 other notation for arithmetic expressions: –prefix – operator placed before operands e.g., + 2 3 –postfix – operator placed after operands e.g., 2 3 +

14 CST230 -- Razdan et al14 Postfix Expressions nice characteristic of prefix and postfix notation is that no parentheses are EVER needed. convert (10 – (4 * 2)) to: –prefix –postfix

15 CST230 -- Razdan et al15 Evaluation of Postfix Expression do if( next input is number ) read input and push onto stack else{ read next character, which is operation pop two numbers off stack combine numbers using the operation push result onto stack } while( there is more input in expression ) answer is single value on stack

16 CST230 -- Razdan et al16 Example Evaluate: 5 3 2 * + 4 – 5 +


Download ppt "CST230 -- Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing."

Similar presentations


Ads by Google