11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

118 Chap. 4 Lists, Stacks, <strong>and</strong> Queues/** Stack ADT */public interface Stack {/** Reinitialize the stack. The user is responsible forreclaiming the storage used by the stack elements. */public void clear();/** Push an element onto the top of the stack.@param it The element being pushed onto the stack. */public void push(E it);/** Remove <strong>and</strong> return the element <strong>at</strong> the top of the stack.@return The element <strong>at</strong> the top of the stack. */public E pop();/** @return A copy of the top element. */public E topValue();/** @return The number of elements in the stack. */public int length();};Figure 4.18 The stack ADT.The array-based stack implement<strong>at</strong>ion is essentially a simplified version of thearray-based list. The only important design decision to be made is which end ofthe array should represent the top of the stack. One choice is to make the top be<strong>at</strong> position 0 in the array. In terms of list functions, all insert <strong>and</strong> removeoper<strong>at</strong>ions would then be on the element in position 0. This implement<strong>at</strong>ion isinefficient, because now every push or pop oper<strong>at</strong>ion will require th<strong>at</strong> all elementscurrently in the stack be shifted one position in the array, for a cost of Θ(n) if thereare n elements. The other choice is have the top element be <strong>at</strong> position n − 1 whenthere are n elements in the stack. In other words, as elements are pushed ontothe stack, they are appended to the tail of the list. Method pop removes the tailelement. In this case, the cost for each push or pop oper<strong>at</strong>ion is only Θ(1).For the implement<strong>at</strong>ion of Figure 4.19, top is defined to be the array index ofthe first free position in the stack. Thus, an empty stack has top set to 0, the firstavailable free position in the array. (Altern<strong>at</strong>ively, top could have been defined tobe the index for the top element in the stack, r<strong>at</strong>her than the first free position. Ifthis had been done, the empty list would initialize top as −1.) Methods push <strong>and</strong>pop simply place an element into, or remove an element from, the array positionindic<strong>at</strong>ed by top. Because top is assumed to be <strong>at</strong> the first free position, pushfirst inserts its value into the top position <strong>and</strong> then increments top, while pop firstdecrements top <strong>and</strong> then removes the top element.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!