stacks

# stacks - Stacks A stack is a data structure in which access...

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

Stacks A stack is a data structure in which access is only allowed from one end. Imagine the candy Pez dispensers You insert and remove candy from the same end. The first piece in is the last piece out. You have a stack of books - to get to the bottom, you have to remove the ones on top. This is called LIFO - Last-in/First-out

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

View Full Document
java.util.Stack Provides the stack class Constructor isEmpty or empty peek - get the top item without removing it pop - remove the top item push - push a new item onto the stack size - the number of items in a stack
Our Stack Push T T T I Push I T I N Push N Pop N T I Pop I T Pop T

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

View Full Document
Evaluating Arithmetic Expressions with a Stack Lets assume that the parentheses match up ( (1 + 2)/3) * (6 - 4) ) ( (3 / 3) * (6-4) ) ( 1 * (6 - 4) ) ( 1 * (2 ) ) 1 * 2 = 2
Evaluating Arithmetic Expressions using a Stack ( ( (1 + 2) / 3) * (6 - 4) ) Let’s use 2 stacks One storing numbers the other the operations 1 + 2 Read 1, + , 2 Then we hit a Right parenthesis so evaluate stack 3

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.

## This note was uploaded on 11/09/2010 for the course CSE 71682 taught by Professor Nakamura during the Spring '10 term at ASU.

### Page1 / 10

stacks - Stacks A stack is a data structure in which access...

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

View Full Document
Ask a homework question - tutors are online