05 - CIS 121 Spring 2010 Data Structures and Introduction...

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

View Full Document Right Arrow Icon
1 2/1/10 Lecture Notes 5 1 CIS 121 Spring 2010 Data Structures and Introduction to Algorithms using JAVA Lecture Notes 5: Stacks, queues, lists © 1999-2010 S.Kannan, V.Tannen, S.Guha & K.Daniilidis © 2006 M.Goodrich & R.Tamassia (with permission) 2/1/10 Lecture Notes 5 2 1) Textbook: read sections 6.5 and 6.6 and chapters 15, 16, 17. 2) Review CIS 120 lecture notes for lists and their implementation. Recommended Reading
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 2/1/10 Lecture Notes 5 3 Stacks Lists (CIS120) but with “only one end.” • Objects are added and removed from the list at the same end. • Stacks are also called Last-In-First-Out (LIFO) data structures. Operations on a stack… (informal): add (“push” onto), remove (“pop” out of), peek at top of the stack, test if stack is empty. 2/1/10 Lecture Notes 5 4 A Stack Interface public interface Stack <E> { // Not in java.util , but see // the class java.util.Stack public E peek (); // Called top() in textbook. // Returns (without removing) the // element at the top of this stack. // Throws EmptyStackException. public void push (E e); // Adds the specified element e // at the top of this stack. public E pop (); // Deletes and returns the element // at the top of this stack. // Throws EmptyStackException . public boolean empty (); // Tests if this stack is empty. }
Background image of page 2
3 2/1/10 Lecture Notes 5 5 Array Implementation of Stacks Similar to lists from arrays (CSE 120). Use the variable tos (top of stack) to keep track of index of top. In an empty stack set tos to -1 . class ArrayStack<E> implements Stack<E> { private E[] theArray; private int tos; static final int DEFAULT_CAPACITY = 16; public ArrayStack( ) { theArray = (E[]) new Object[ DEFAULT_CAPACITY ]; tos = -1; } ... Inevitable because of limitations on generics. Yields compile-time warning . 2/1/10 Lecture Notes 5 6 public boolean empty() { return ( tos == -1 ); } public void push(E x) { if ( tos + 1 == theArray.length ) doubleArray(); //Change array size dynamically tos++; theArray[tos] = x; } public E peek() { if ( tos == -1 ) throw new java.util.EmptyStackException(); return theArray[tos]; } public E pop() { if ( tos == -1 ) throw new EmptyStackException(); tos--; return theArray[tos + 1] } This is an unchecked exception.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 2/1/10 Lecture Notes 5 7 public void doubleArray( ) { int newSize = 2*theArray.length; E[] temp = (E[]) new Object[newSize]; for (int i = 0; i < theArray.length; i++) temp[i] = theArray[i]; theArray = temp; } // tos is not modified by doubleArray, // but now it is used as an index in a // bigger array 2/1/10 Lecture Notes 5 8 Amortized analysis It is clear that all of the stack operations are O(1) except for the situation when we have to move elements to an array of double size. Should we really be doubling the size? What happens if we just increase the size of the array by 1
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/19/2010 for the course CIS 121 taught by Professor Tannen during the Spring '09 term at UPenn.

Page1 / 14

05 - CIS 121 Spring 2010 Data Structures and Introduction...

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

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