Stack Abstract Data Types ADTs l An abstract
Stack
Abstract Data Types (ADTs) l An abstract data type (ADT) l is an abstraction l l of a data structure An ADT specifies: l Data stored l Operations on the data l Error conditions l associated with operations l Example: ADT modeling a simple stock trading system l The data stored are buy/sell orders l The operations supported are l l order buy(stock, shares, price) order sell(stock, shares, price) void cancel(order) Error conditions: l l Buy/sell a nonexistent stock Cancel a nonexistent order
The Stack ADT l l stores arbitrary objects Auxiliary stack operations: l object top(): l l Insertions and deletions l follow the last-in first-out scheme l l Think of a l l push(object): l l integer size(): l spring-loaded plate dispenser Main stack operations: l returns the last inserted element without removing it inserts an element object pop(): l removes and returns the last inserted element l returns the number of elements stored boolean is. Empty(): l indicates whether no elements are stored
Stack: operations and their effects
Example. I using built-in Stack: Reversing an Array l Refer to Array. Reverse. App project l The elements of an array can be reversed l Using the Java built-in class called Stack l l Which is part of the java. util package Number is a base class for l Big. Decimal, Big. Integer l Byte, Double, Float, Integer, Long and Short
Example. II using built-in Stack: Reversing an Array l Refer to Secret. Message. App project l The secret message is l Encoded by having each individual word reversed l And then decoded by means of an auxiliary stack l Character are displayed in reverse order when l Popped from the auxiliary stack variable
Our Stack Interface l Java interface corresponding to our Stack ADT l Requires the definition of class Stack. Exception l Different from the built-in Java class java. util. Stack public interface Stack { public int size(); public boolean is. Empty(); public Object top() throws Stack. Exception; public void push(Object o) throws Stack. Exception; public Object pop() throws Stack. Exception; }
Exceptions l Attempting the execution l of an operation of ADT may l l In the Stack ADT, operations l pop and top cannot be sometimes cause an error condition, called an exception l l l Exceptions are said to be l “thrown” by an operation that cannot be executed push cannot be l l performed if the stack is empty performed if the stack is full Attempting the execution l of pop or top on an empty stack throws an Stack. Exception l of push on a full stack throws an Stack. Exception
Array-based Stack l A simple way of implementing the Stack ADT l l We add elements l l uses an array from left to right A variable keeps track l of the index of the top element Algorithm size() return t + 1 Algorithm pop() if is. Empty() then throw Stack. Exception else t t 1 return S[t + 1] … S 0 1 2 t
Array-based Stack (cont. ) l The array l l storing the stack elements may become full A push operation will then throw a Stack. Exception l Limitation of the array-based implementation l Not intrinsic to the Stack ADT Algorithm push(o) if t = S. length 1 then throw Stack. Exception else t t+1 S[t] o … S 0 1 2 t
Example III using our Stack: Parentheses Matching l l “(”, “{”, or “[” must be paired with a matching “)”, “}”, or “[” l correct: ( )(( )){([( )])} l correct: ((( )){([( )])} l incorrect: )(( )){([( )])} l incorrect: ({[ ])} l incorrect: ( Refer to Grouping. Symbols. App project
Parentheses Matching Algorithm Paren. Match(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let S be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then S. push(X[i]) else if X[i] is a closing grouping symbol then if S. is. Empty() then return false {nothing to match with} if S. pop() does not match the type of X[i] then return false {wrong type} if S. is. Empty() then return true {every symbol matched} else return false {some symbols were never matched}
- Slides: 12