linear

# linear - Computer Science 211 Data Structures Mount Holyoke...

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

Computer Science 211 Data Structures Mount Holyoke College Fall 2009 Topic Notes: Linear Structures The structures we’ve seen so far, Vector s and linked lists, allow insertion and deletion of ele- ments from any position in the structure. So there is an “order” to the structure, but that order does not restrict how we access or modify the structure. There may be significant differences in efficiency when modifying or accessing the structure in a way that is perfectly legal but may be hard to implement efficiently. Moreover, it may not be clear to the user of a structure which operations are efficient and which will incur a significant cost. Linear structures are more restricted, allowing only a single add and single remove method, neither of which allows us to specify a position within the structure. Why would we want to restrict our structures in such a way? More restrictions on a structure generally allows for more efficient implementation of its supported operations. Since we know how it will be used (and in fact enforce this by limiting the number of public methods), we can make sure we use an appropriate internal representation to ensure efficiency of the supported operations. The basic operations on our linear structure are specified in the Linear interface in the structure package. public interface Linear<E> extends Structure<E> { public void add(E value); // pre: value is non-null // post: the value is added to the collection, // the replacement policy not specified. public E remove(); // pre: structure is not empty. // post: removes an object from container public E get(); // pre: structure is not empty. // post: returns ref to next object to be removed. } We will look at two particular highly restricted linear structures: Stack : all additions and deletions at same end: LIFO (last-in, first-out) Queue : all additions at one end, all deletions from the other: FIFO (first-in, first-out)

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

View Full Document
CS 211 Data Structures Fall 2009 Stacks We start with stacks. The idea is very simple. Consider a stack of trays. New trays are added at the top and trays are also taken from the top when needed. (Sane) people don’t go in and try to take a tray from the middle or from the bottom of the stack. Stacks can be described recursively: A stack is either empty or has its top element sitting on a (smaller) stack. All additions and deletions take place at the top of the stack. We traditionally refer to addition as push and removal as pop , motivated by the analogy with a spring-loaded stack of trays. Here we’ll use both names interchangeably (and provide both methods) for the add or push and remove or pop . There are also two names for the operation of looking at the element on top of the stack without removing it: get or peek . So the three major operations allowed on a stack are:
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 02/15/2010.

### Page1 / 12

linear - Computer Science 211 Data Structures Mount Holyoke...

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

View Full Document
Ask a homework question - tutors are online