Date post: | 07-Apr-2018 |
Category: |
Documents |
Upload: | wcalingasan |
View: | 231 times |
Download: | 0 times |
of 18
8/6/2019 ADT Stacks09
1/18
ADT Stacks
8/6/2019 ADT Stacks09
2/18
Stacks
Principle: LIFO
Definition: An ordered collection of
data items which can be accessed atonly one location, called the top of
the stack
8/6/2019 ADT Stacks09
3/18
Basic Operations
Push
Pop
Make Null Empty
Full
8/6/2019 ADT Stacks09
4/18
Representation:
char STACK[10];
8/6/2019 ADT Stacks09
5/18
1. Push
-inserts the data item at the top of thestack, returns the modified stack.
{
if (Top >= (MaxStack - 1))
Success = False; //no room on stack
else
{
Top++; //increment stack top pointer
STACK[Top] = X; //copy X to stack
Success = True;
}}
8/6/2019 ADT Stacks09
6/18
2.Pop
-retrieves and removes the element at the top of the
stack, returns the top element of the stack and themodified stack
{
if (Top < 0)
Success = False; //empty stack
else
{
X = STACK[Top]; //pop top of stack into X
Top--; //decrement top of stack pointer
Success = True;
}
}
8/6/2019 ADT Stacks09
7/18
3. Make Null
{ Top = -1; }
4. Empty Stack
{ return (Top < 0); }
5. Full Stack
{ return(Top >= MaxStack);
}
8/6/2019 ADT Stacks09
8/18
Array Implementation
for a Stack of ints
public class ArrayStack
{
int store[];
int top;
static final int MAX = 100;
public ArrayStack()
{
store = new int[MAX];
top = 0;}
// ...
}
4top
12 24 37 17
top = index of thefirst FREEslot
...
8/6/2019 ADT Stacks09
9/18
ArrayStack class, continued
public class ArrayStack
{ // ...
public boolean empty()
{
return (top == 0);
}
public void push(int entry)
{
if (top < MAX)
store[top++] = entry;}
// ...
}
...
4top
12 24 37 17
Remember:top = index of the
first FREEslot
// in the code that uses
// the stack
stack.push( 95 );
95
5
8/6/2019 ADT Stacks09
10/18
ArrayStack class, continuedpublic class ArrayStack
{
// ...
public int pop()
throws Exception
{if ( empty() )
throw new Exception();
else
return store[--top];
}
}
...
5top
12 24 37 17 95
4
// in the code that uses
// the stack
int x = stack.pop();
// x gets 95,
// slot 4 is now free
Remember:top = index of the
first FREE slot
Store[4] still contains95, but its nowconsidered free.
8/6/2019 ADT Stacks09
11/18
Using the Stack
try
{
ArrayStack st = new ArrayStack();
st.push( 1 ); st.push( 3 ); st.push( 2 );
System.out.println( st.pop() );st.push( 5 );
System.out.println( st.pop() );
System.out.println( st.pop() );
}
catch ( Exception e ){
System.out.println( pop() on empty stack );
}
8/6/2019 ADT Stacks09
12/18
Application of Stacks
Checking for unpaired or paired (),{} in a
programming language
Evaluation of expressions.
8/6/2019 ADT Stacks09
13/18
A. Evaluation of Expression
To understand how stacks can be used in evaluatingexpressions, we first need to know some underlying principles ofbinary expressions, that is notation of expressions.
Notations of Expressions
Let X=A+B be an expression. Then the notations of expressions aredefined as:
1. Prefix notation: the operator is written before the two operands.Ex. +AB
2. Infix notation: the operator is written in between the twooperands. Ex. A+B
3. Postfix notation: the operator is written after the two operands.Ex. AB+
8/6/2019 ADT Stacks09
14/18
Illustration:
X=A+B/C-D
Find the 3 notations of the above expression.
Prefix: -+A/BCD
Infix: A+B/C-D
Postfix: ABC/+D-
8/6/2019 ADT Stacks09
15/18
Knowing the underlying principles on the
3 notations of expressions, we now areready to use stacks to evaluate expressions.
There are 2 possible ways to evaluate
expressions
1. Prefix method
2. Postfix method
8/6/2019 ADT Stacks09
16/18
A. Prefix method
Algo:
1. Let there be 2 stacks X and S. Assume that X contains
the prefix notation of the expression and S be initially
empty.S will be used to do the evaluation.
2. Get the top of X.
3. If x [i] is an operand, push the element into S.
If x[i] is an operator, pop the 2 topmost element of S.
Apply the operation defined by x[i] then push the result
into S
4. Repeat steps 2-3 until X is empty or S is left with only
one element.That element
Must be the result of the evaluation process.
8/6/2019 ADT Stacks09
17/18
Conversion of infix to postfix notation using stacks
.push only operations onto the stack.push the operator if the incoming operator has
a higher precedence than the top operator in the
stack.
.If the incoming operator has a priority
8/6/2019 ADT Stacks09
18/18
Priority table
Symbol instack priority Incoming Priority
^ 3 4
*, / 2 2
+, - 1 1
( 0 4
) - -
Example: A+(B*C) + D