22 - 02/25/09 22:50:17 CS 61B: Lecture 22 Friday, March 13,...

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

View Full Document Right Arrow Icon
02/25/09 22:50:17 1 22 CS 61B: Lecture 22 Friday, March 13, 2009 Today’s reading: BASIC DATA STRUCTURES ===================== Stacks ------ A _stack_ is a crippled list. You may manipulate only the item at the top of the stack. The main operations: you may "push" a new item onto the top of the stack; you may "pop" the top item off the stack; you may examine the "top" item of the stack. A stack can grow arbitrarily large. | | | | | | -size()-> 2 |d| -top()-> d | | |b| -pop()-> | | -push(c)-> |c| |c| | | -top()-- |a| | |a| |a| -push(d)--> |a| --pop() x 3--> | | | --- v --- --- --- --- v b EmptyStackException public interface Stack { public int size(); public boolean isEmpty(); public void push(Object item); public Object pop() throws EmptyStackException; public Object top() throws EmptyStackException; } In any reasonable implementation, all these methods run in O(1) time. A stack is easily implemented as a singly-linked list, using just the front(), insertFront(), and removeFront() methods. Why talk about Stacks when we already have Lists? Mainly so you can carry on discussions with other computer programmers. If somebody tells you that an algorithm uses a stack, the limitations of a stack give you a hint how the algorithm works. Sample application: Verifying matched parentheses in a String like "{[(){[]}]()}". Scan through the String, character by character. o
Background image of page 1

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

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

This note was uploaded on 02/21/2010 for the course CS 61B taught by Professor Canny during the Spring '01 term at University of California, Berkeley.

Page1 / 2

22 - 02/25/09 22:50:17 CS 61B: Lecture 22 Friday, March 13,...

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

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