Stacks Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks
Stacks: Definitions and Operations A LIFO list: Last In, First Out. Restricted form of list: Insert/Remove only at front or TOP of list. Accessible Element: TOP Data Structures 2 Stacks
Stack Operations Create/Delete IsFull()/IsEmpty() Push (Insert), Pop (remove) TopValue() Stack Implementations Array Based. Linked List Based. Data Structures 3 Stacks
Array-Based Stack top D top A C B A top E B A top F A push (A) push (B) push (C) push (D) pop () pop () push (E) pop () pop () push (F) top pop () pop () Data Structures 4 Stacks
Stack Interface / Stack ADT / public interface Stack<E> { / R e i n i t i a l i z e the stack. The user i s responsible f o r e c l a i m i n g the storage used by the stack elements. public void c l e a r ( ) ; / Push an element onto the top of the stack. p u b l i c void push (E i t ) ; / Remove and r e t u r n the element at the top of the sta p u b l i c E pop ( ) ; / @return A copy of the top element. / public E topvalue ( ) ; / @return The number of elements i n the stack. / public i n t l e n g t h ( ) ; Data Structures 5 Stacks
Array-Based Stack / Array based stack implementation / class AStack<E> implements Stack<E> { private s t a t i c f i n a l i n t d e f a u l t S i z e = 10; private i n t maxsize ; / / Maximum size of stack private i n t top ; / / Index f o r top Object private E [ ] l i s t A r r a y ; / / Array holding stack / / Constructors AStack ( ) { this ( d e f a u l t S i z e ) ; AStack ( i n t size ) { maxsize = size ; top = 0; l i s t A r r a y = (E [ ] ) new Object [ size ] ; / / Create l i s t A r r a y Data Structures 6 Stacks
Array-Based Stack public void c l e a r ( ) / / R e i n i t i a l i z e stack { top = 0; public void push (E i t ) { / / Push i t onto stack a s s e r t top!= maxsize : Stack i s f u l l ; l i s t A r r a y [ top ++] = i t ; public E pop ( ) { / / Pop top element a s s e r t top!= 0 : Stack i s empty ; return l i s t A r r a y [ top ] ; public E topvalue ( ) { / / Return t o p element a s s e r t top!= 0 : Stack i s empty ; return l i s t A r r a y [ top 1]; public i n t l e n g t h ( ) { return top ; / / Return l e n g t h Data Structures 7 Stacks
Linked Stacks A simplified version of the linked list, where insertions/deletions are only through the head of the list. Freelist implementation is an example. No header node is used. Data Structures 8 Stacks
Linked Stack Implementation class LStack<E> implements Stack<E> { private Link<E> top ; / / P o i n t e r to f i r s t element private i n t size ; / / Number of elements / / Constructors public LStack ( ) { top = null ; s ize = 0; public LStack ( i n t sz ) { top = null ; s ize = 0; / / R e i n i t i a l i z e stack public void c l e a r ( ) { top = null ; s ize = 0; public void push (E i t ) { / / Put i t on stack top = new Link<E>( i t, top ) ; size ++; Data Structures 9 Stacks
Linked Stack Implementation public E pop ( ) { / / Remove i t from stack a s s e r t top!= null : Stack i s empty ; E i t = top. element ( ) ; top = top. next ( ) ; size ; return i t ; public E topvalue ( ) { / / Return top value a s s e r t top!= null : Stack i s empty ; return top. element ( ) ; public i n t l e n g t h ( ) { return size ; / / Return l e n g t h Data Structures 10 Stacks
Stack Application:Expression Evaluation Infix to Postfix Conversion A + B C D E // Infix Form ABC DE + // Postfix Form Evaluation of expressions in postfix form. Data Structures 11 Stacks
Stack Application:Recursion Subroutine calls need to preserve return addresses, parameters and local variables. Implemented using a program stack. Data Structures 12 Stacks
Stack Application:Infix To Postfix I n i t i a l i z e the stack. / / stack i s empty for ( each operator or operand i n the i n p u t expression ) { i f ( an operand ) add to P o s t x expression else i f ( an operator ) { i f ( stack i s empty ) push oper. i n t o stack else { while ( i n p u t oper. prec. < than stack top oper. prec ) { pop operator from stack, add to P o s t f i x expression i f stack i s empty, break / / out of the loop push i n p u t oper. i n t o stack while ( stack i s not empty ) { pop the stack ; add operator to P o s t x expression % Data Structures 13 Stacks
Stack Application:Postfix Expression Evaluation I n i t i a l i z e the stack. / / stack i s empty for ( each operator or operand i n the i n p u t expression ){ i f ( an operand ) push i n t o stack else i f ( an operator ) { opnd2 = pop stack opnd1 = pop stack r e s u l t = Evaluate opnd1 oper opnd2 push r e s u l t onto stack r e s u l t = pop stack Data Structures 14 Stacks