10 - CMPSCI 187: Programming With Data Structures Lecture...

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

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture 10: The ArrayStack Class 30 September 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
The ArrayStack Class • Implementing a Stack With an Array • A Generic Stack is an Array of What? • The Fields and the Basic Methods • Stack Over±ow and Dynamic Resizing • Running Time Analysis • Exception Handling • Project 3: The Dropout Stack
Background image of page 2
Implementing a Stack With an Array • An array is a powerful data structure -- we have random access to it, meaning that we can get out any item if we know its address. • A stack has limited access, only at the top, which is not hard to simulate with an array. • Arrays are Fxed in size, while stacks (at least in principle) have unbounded memory available. • The idea is that we will mark the position of the top of the stack, which will move within the array as we push and pop. • Pushes and pops will not affect the position of the other elements and so may be implemented in O(1) time.
Background image of page 3

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

View Full DocumentRight Arrow Icon
A Generic Stack is an Array of What? • We want to create a generic class ArrayStack<T>, where T will be a variable ranging over a type inside our code. An ArrayStack<Dog> will be implemented by an array of Dog elements. Inside our code we will speak of an array of T elements, whose type is T [ ]. • But in our constructor we can’t say “T [ ] stack = new T[capacity];” because the
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 11/22/2011 for the course COMPSCI 187 taught by Professor Barrington during the Fall '11 term at UMass (Amherst).

Page1 / 9

10 - CMPSCI 187: Programming With Data Structures Lecture...

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