6 Stack ADTs Stack concepts Stack applications A

  • Slides: 23
Download presentation
6 Stack ADTs • Stack concepts. • Stack applications. • A stack ADT: requirements,

6 Stack ADTs • Stack concepts. • Stack applications. • A stack ADT: requirements, contract. • Implementations of stacks: using arrays, linked lists. • Stacks in the Java class library. © 2001, D. A. Watt and D. F. Brown

Stack concepts (1) • A stack is a last-in-first-out sequence of elements. Elements can

Stack concepts (1) • A stack is a last-in-first-out sequence of elements. Elements can added and removed only at one end (the top of the stack). • The depth of stack is the number of elements it contains. • An empty stack has depth zero.

Stack concepts (2) • Illustration (stack of books): Initially: Rob Roy War & Peace

Stack concepts (2) • Illustration (stack of books): Initially: Rob Roy War & Peace Moby Dick After removing a book: War & Peace Moby Dick After adding “Misérables”: After adding “ 2001”: Misérables War & Peace Moby Dick 2001 Misérables War & Peace Moby Dick

Stack applications • Interpreter (e. g. , the Java Virtual Machine) § maintains a

Stack applications • Interpreter (e. g. , the Java Virtual Machine) § maintains a stack containing intermediate results during evaluation of complicated expressions § also containing arguments and return addresses for method calls and returns. • Parser (e. g. , a component of the Java compiler) § maintains a stack containing symbols encountered during parsing.

Example 1: text-file reversal • A text file is a sequence of (zero or

Example 1: text-file reversal • A text file is a sequence of (zero or more) lines. • To reverse the order of these lines, we must store them in a first-in-last-out sequence. • Text-file reversal algorithm: To output the lines of file in reverse order: 1. 2. 3. 4. Make line-stack empty. For each line read from file, repeat: 2. 1. Add line to the top of line-stack. While line-stack is not empty, repeat: 3. 1. Remove a line from the top of line-stack into line. 3. 2. Output line. Terminate.

Example 2: bracketing (1) • A phrase is well-bracketed if: § for every left

Example 2: bracketing (1) • A phrase is well-bracketed if: § for every left bracket, there is a later matching right bracket § for every right bracket, there is an earlier matching left bracket § the subphrase between a pair of matching brackets is itself wellbracketed. • Examples (mathematical expressions): s (s – a) (s – b) (s – c) (– b + [b 2 – 4 ac]) / 2 a s (s – a) (s – b (s – c) s (s – a) s – b) (s – c) (– b + [b 2 – 4 ac)] / 2 a well-bracketed ill-bracketed

Example 2 (2) • Bracket matching algorithm: To test whether phrase is well-bracketed: 1.

Example 2 (2) • Bracket matching algorithm: To test whether phrase is well-bracketed: 1. 2. Make bracket-stack empty. For each symbol sym in phrase (scanning from left to right), repeat: 2. 1. If sym is a left bracket: 2. 1. 1. Add sym to the top of bracket-stack. 2. 2. If sym is a right bracket: 2. 2. 1. If bracket-stack is empty, terminate with false. 2. 2. 2. Remove a bracket from the top of bracket-stack into 2. 2. 3. left. If left and sym are not matched brackets, terminate 3. with false. Terminate with true if bracket-stack is empty, or false otherwise.

Stack ADT: requirements • Requirements: 1) It must be possible to make a stack

Stack ADT: requirements • Requirements: 1) It must be possible to make a stack empty. 2) It must be possible to add (‘push’) an element to the top of a stack. 3) It must be possible to remove (‘pop’) the topmost element from a stack. 4) It must be possible to test whether a stack is empty. 5) It should be possible to access the topmost element in a stack without removing it.

Stack ADT: contract (1) • Possible contract, expressed as a Java interface: public interface

Stack ADT: contract (1) • Possible contract, expressed as a Java interface: public interface Stack { // Each Stack object is a stack whose elements are objects. //////// Accessors //////// public boolean is. Empty (); // Return true if and only if this stack is empty. public Object get. Last (); // Return the element at the top of this stack.

Stack ADT: contract (2) • Possible contract (continued): //////// Transformers //////// public void clear

Stack ADT: contract (2) • Possible contract (continued): //////// Transformers //////// public void clear (); // Make this stack empty. public void add. Last (Object elem); // Add elem as the top element of this stack. public Object remove. Last (); // Remove and return the element at the top of this stack. }

Implementation of stacks using arrays (1) • Represent a bounded stack (depth maxdepth) by:

Implementation of stacks using arrays (1) • Represent a bounded stack (depth maxdepth) by: § variable depth, containing the current depth § array elems of length maxdepth, containing the stacked elements in elems[0… depth– 1]. topmost element Invariant: 0 1 element length=0 unoccupied depth– 1 depth element maxdepth– 1 1 maxdepth– 1 Empty stack: Illustration (maxdepth = 6): 1 0 2 Moby War & Rob Dick Peace Roy depth=3 4 5

Implementation using arrays (2) • Java implementation: public class Array. Stack implements Stack {

Implementation using arrays (2) • Java implementation: public class Array. Stack implements Stack { private Object[] elems; private int depth; //////// Constructor //////// public Array. Stack (int max. Depth) { elems = new Object[max. Depth]; depth = 0; }

Implementation using arrays (3) • Java implementation (continued): //////// Accessors //////// public boolean is.

Implementation using arrays (3) • Java implementation (continued): //////// Accessors //////// public boolean is. Empty () { return (depth == 0); } public Object get. Last () { if (depth == 0) throw …; return elems[depth-1]; }

Implementation using arrays (4) • Java implementation (continued): //////// Transformers //////// public void clear

Implementation using arrays (4) • Java implementation (continued): //////// Transformers //////// public void clear () { for (int i = 0; i < depth; i++) elems[i] = null; depth = 0; } public void add. Last (Object elem) { if (depth == elems. length) throw …; elems[depth++] = elem; }

Implementation using arrays (5) • Java implementation (continued): public Object remove. Last () {

Implementation using arrays (5) • Java implementation (continued): public Object remove. Last () { if (depth == 0) throw …; Object top. Elem = elems[--depth]; elems[depth] = null; return top. Elem; } } • Analysis: § Operations is. Empty, get. Last, add. Last, remove. Last have time complexity O(1). § Operation clear has time complexity O(n).

Implementation of stacks using SLLs (1) • Represent an (unbounded) stack by an SLL,

Implementation of stacks using SLLs (1) • Represent an (unbounded) stack by an SLL, such that the first node contains the topmost element Invariant: element Empty stack: Illustration: Rob Roy War & Peace Moby Dick

Implementation using SLLs (2) • Java implementation: public class Linked. Stack implements Stack {

Implementation using SLLs (2) • Java implementation: public class Linked. Stack implements Stack { private SLLNode top; //////// Constructor //////// public Linked. Stack () { top = null; }

Implementation using SLLs (3) • Java implementation (continued): //////// Accessors //////// public boolean is.

Implementation using SLLs (3) • Java implementation (continued): //////// Accessors //////// public boolean is. Empty () { return (top == null); } public Object get. Last () { if (top == null) throw …; return top. element; }

Implementation using SLLs (4) • Java implementation (continued): //////// Transformers //////// public void clear

Implementation using SLLs (4) • Java implementation (continued): //////// Transformers //////// public void clear () { top = null; } public void add. Last (Object elem) { top = new SLLNode(elem, top); }

Implementation using SLLs (5) • Java implementation (continued): public Object remove. Last () {

Implementation using SLLs (5) • Java implementation (continued): public Object remove. Last () { if (top == null) throw …; Object top. Elem = top. element; top = top. succ; return top. Elem; } } • Analysis: § All operations have time complexity O(1).

Stacks in the Java class library • Java provides no Stack interface or class

Stacks in the Java class library • Java provides no Stack interface or class as such. • However, the java. util. Linked. List class provides all the above Stack operations. • Illustration: import java. util. Linked. List; Linked. List book. Stack = new Linked. List(); book. Stack. add. Last("Moby Dick"); book. Stack. add. Last("War & Peace"); book. Stack. add. Last("Rob Roy"); System. out. println(book. Stack. remove. Last());

Example 3: text-file reversal revisited (1) • Recall the text-file reversal algorithm of Example

Example 3: text-file reversal revisited (1) • Recall the text-file reversal algorithm of Example 1. • Implementation: public static void reverse ( Buffered. Reader input, Buffered. Writer output) throws IOException { Linked. List line. Stack = new Linked. List(); for (; ; ) { String line = input. read. Line(); if (line == null) break; // end of input line. Stack. add. Last(line);

Example 3 (2) • Implementation (continued): while (! line. Stack. is. Empty()) { String

Example 3 (2) • Implementation (continued): while (! line. Stack. is. Empty()) { String line = line. Stack. remove. Last(); output. write(line + "n"); } output. close(); }