LEct8_ADT_stack1 - ITI 1121. Introduction to Computing II...

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

View Full Document Right Arrow Icon
ITI 1121. Introduction to Computing II Marcel Turcotte School of Information Technology and Engineering Version of February 4, 2008 Abstract Abstract Data Type Stack
Background image of page 1

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

View Full DocumentRight Arrow Icon
Stacks Software stacks are abstract data types (structures) similar to physical stacks. Books PEZ dispenser Plates Trays The analogy with a plate dispenser in a cafeteria is particularly interesting: 1) for software stacks, just like physical stacks, only the top element is accessible and 2) the top element must be removed in order to access the remaining elements .
Background image of page 2
Definition A stack is a linear data structure that is always accessed from the same extremity, one element at a time, and that element is called the top of the stack. Stacks are also called LIFO data structures: last-in first-out . "alpha" "beta" "alpha" "alpha" s = new StackImp() s.push( "alpha" ) s.push( "beta" ) o = s.pop()
Background image of page 3

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

View Full DocumentRight Arrow Icon
Applications Stacks are widely used in applications and system programming: In compilers, and formal language analysis in general; To implement backtracking algorithms, which are used in automatic theorem provers (Prolog), games algorithms and artificial algorithms; For memory management during program execution, system stack, it supports the development of recursive algorithms; To support “undo” operations or “back” buttons inside a web browser.
Background image of page 4
Basic operations The basic operations of a stack are: push: add an element onto the top of the stack; pop: removes and returns the top element; empty: tests if the stack is empty.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Stack ADT public interface Stack { public abstract boolean isEmpty(); public abstract Object push( Object o ); public abstract Object pop(); public abstract Object peek(); }
Background image of page 6
Stack ADT (using Java 1.5) public interface Stack<E> { public abstract boolean isEmpty(); public abstract E push( E elem ); public abstract E pop(); public abstract E peek(); } Yes, that’s right, an interface can also be parametrized!
Background image of page 7

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

View Full DocumentRight Arrow Icon
Example public class Mystery { public static void main( String[] args ) { Stack<String> stack = new StackImplementation<String>(); for ( int i=0; i<args.length(); i++ ) stack.push( args[ i ] ); while ( ! stack.empty() ) System.out.print( stack.pop() ); } } What does this print: “ java Mystery a b c d e ”?
Background image of page 8
Remarks Elements come out of a stack in reverse order; Frequently occurring idiom: while ( ! stack.empty() ) { element = stack.pop(); // . .. } Make sure to not forget the pop()!
Background image of page 9

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

View Full DocumentRight Arrow Icon
Operations (contd) peek: returns the object at the top of this stack without removing it;
Background image of page 10
Implementations How would you implement this interface? There are two popular families of implementation techniques: Array-based; Using linked-nodes. Stack<Token> s; s = new ArrayStack<Token>(); s = new DynamicArrayStack<Token>(); s = new LinkedStack<Token>();
Background image of page 11

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

View Full DocumentRight Arrow Icon
Question One of the proposed implementations will be using an array, why not using an array instead of stack in our programs since the implementation of the interface stack will be using an array anyway?
Background image of page 12
Implementing a Stack using an array: ArrayStack What are the instance variables?
Background image of page 13

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

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

Page1 / 98

LEct8_ADT_stack1 - ITI 1121. Introduction to Computing II...

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

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