Professional Documents
Culture Documents
Objectives
At the end of the lesson, the student should be able to:
Introduction
Operations
Operations
Push
Pop
Operations
public interface Stack{
public int size(); /* returns the size of the stack */
public boolean isEmpty(); /* checks if empty */
public Object top() throws StackException;
public Object pop() throws StackException;
public void push(Object item) throws StackException;
}
class StackException extends RuntimeException{
public StackException(String err){
super(err);
}
}
Implementations
Sequential Representation
Implementations
public class ArrayStack implements Stack{
public static final int CAPACITY = 1000;
public int capacity;
Object S[];
int top = -1;
public ArrayStack(){
this(CAPACITY);
}
public ArrayStack(int c){
capacity = c;
S = new Object[capacity];
}
public int size(){
return (top+1);
}
public boolean isEmpty(){
return (top < 0);
}
Data Structures Stacks
Implementations
public Object top(){
if (isEmpty()) throw new StackException("Stack empty.");
return S[top];
}
public Object pop(){
Object item;
if (isEmpty()) throw new StackException("Stack underflow.");
item = S[top];
S[top--] = null;
return item;
}
public void push(Object item){
if (size()==capacity)
throw new StackException("Stack overflow.");
S[++top]=item;
}
Implementations
Linked Representation
10
Implementations
11
Implementations
public Object pop(){
Node temp;
if (isEmpty())
throw new StackException("Stack underflow.");
temp = top;
top = top.link;
return temp.info;
}
12
Application: Pattern
Recognition Problem
1.Get next character a or b from input string and push onto the stack; repeat
until the symbol c is encountered.
2.Get next character a or b from input string, pop stack and compare. If the
two symbols match, continue; otherwise, stop the string is not in L.
Additional states where the input string is not in L:
1.The end of the string is reached but no c is encountered.
2.The end of the string is reached but the stack is not empty.
3.The stack is empty but the end of the string is not yet reached.
13
Application: Pattern
Recognition Problem
Example
Input
abbabcbabba
abbabcbabba
bbabcbabba
babcbabba
abcbabba
bcbabba
cbabba
babba
abba
bba
ba
a
-
Action
-----Push a
Push b
Push b
Push a
Push b
Discard c
Pop, compare b and b --> ok
Pop, compare a and a --> ok
Pop, compare b and b --> ok
Pop, compare b and b --> ok
Pop, compare a and a --> ok
Success
Data Structures Stacks
Stack
(bottom) --> (top)
a
ab
abb
abba
abbab
abbab
abba
abb
ab
a
-
14
Some properties:
15
Operator
^
*/
+-
Priority
3
2
1
Property
right associative
left associative
left associative
Example
a^b^c = a^(b^c)
a*b*c = (a*b)*c
a+b+c = (a+b)+c
16
17
Priority numbers:
Token, x
Operand
+*/
^
(
icp(x)
0
1
3
6
7
isp(x)
2
4
5
0
Rank
1
-1
-1
-1
18
The algorithm:
1. Get the next token x.
2. If x is an operand, then output x.
3. If x is the ( , then push x onto the stack.
4. If x is the ), then output stack elements until an ( is
encountered. Pop stack once more to delete the (. If top = 0,
the algorithm terminates.
5. If x is an operator, then while icp(x) < isp(stack(top)),
output stack elements; else, if icp(x) > isp(stack(top)),
then push x onto the stack.
6. Go to step 1.
19
Advanced Topic:
Multiple Stacks
20
Advanced Topic:
Multiple Stacks
Base pointers, B(i) - defines the start of m stacks in a vector S with size
n. B[i] points to the space one cell below the stack's first actual cell:
B[i]
= n/m * i - 1
0i<m
B[m]
= n-1
Top addresses, T(i)
T[i] = B[i] ,
0im
21
Advanced Topic:
Multiple Stacks
22
Advanced Topic:
Multiple Stacks
23
Advanced Topic:
Multiple Stacks
Unit-shift method
void unitShift(int i) throws StackException{
int k=-1;
/* Points to the 'nearest' stack w/
free space*/
/*Scan stacks above the overflowed stack*/
for (int j=i+1; j<m; j++)
if (T[j] < B[j+1]) {
k = j;
break;
}
24
Advanced Topic:
Multiple Stacks
Unit-shift method
25
Advanced Topic:
Multiple Stacks
Unit-shift method
26
Advanced Topic:
Multiple Stacks
Garwick's algorithm
27
Advanced Topic:
Multiple Stacks
28
Advanced Topic:
Multiple Stacks
29
Advanced Topic:
Multiple Stacks
30
Summary
31