Title: IT2201 Data Structures
1PREPARING FOR THE BIT
IT2201 Data Structures Algorithms
dsa_at_ict.cmb.ac.lk
Preparing for BIT 17/05/2001
2STACKS
- A stack is a data structure in which all the
access is restricted to the most recently
inserted items.
3STACK MODEL
Input to a stack is by push
Access is by top
Deletion is by pop
4STACK MODEL
Pop,Top
Push
B
A
5STACK PROPERTIES
- The last item added to the stack is placed on the
top and is easily accessible. - Thus the stack is appropriate if we expect to
access on the top item, all other items are
inaccessible.
6Important stack applications
- Compiler Design
- Mathematical Expression Evaluation
- Balanced Spell Checker
- Simple Calculator
7IMPLEMENTATION OF STACKS
- There are two basic ways to arrange for constant
time operations. - The first is to store the items contiguously in
an array. - And the second is to store items non-contiguously
in a linked list
8ARRAY IMPLEMENTATION
- To push an item into an empty stack, we insert it
at array location 0. ( since all java arrays
start at 0) - To push the next item into at location 0 over to
location to make room for new item.
9- This is easily done by defining an auxiliary
integer variable known as stack pointer,which
stores the array index currently being used as
the top of the stack. - A stack can be implemented with an array and an
integer. - The integer TOS (top of stack) provides the array
index of the top element of the stack, when TOS
is 1 , the stack is empty
10Stacks specifies two data fields such as
- The array ( which is expanded as needed stores
the items in the stack) - Topofstack (TOS) ( gives the index of the current
top of the stack, if stack is empty , this index
is 1.
11ALGORITHMS FOR PUSHING POPPING
- PUSH
- If stack is not full then
- Add 1 to the stack pointer.
- Store item at stack pointer location.
12ALGORITHMS FOR PUSHING POPPING
- POP
- If stack is not empty then
- Read item at stack pointer location.
- Subtract 1 from the stack pointer.
13How to stack routines workempty
stackpush(a),push(b)pop
TOS (0)
a
Push (a)
Stack is empty TOS(-1)
TOS (1)
b
TOS (0)
a
a
pop
Push (b)
14Java implementation
- Zero parameter constructor for array based stack
- Public stackar( )
- / construct the stack
-
- thearray new objectdefault-capacity
- Tos -1
15Isempty returns true if stack is empty, false
otherwise
- Isempty( )
- Public Boolean Isempty( )
-
- Return tos -1
-
16Isfull( ) returns true if stack is full , false
otherwise
- Isfull( )
- Public Boolean isfull( )
-
- Return tos stacksize-1 (default capacity)
-
17push method for array based stack
- Insert a new item into the stack
- Public void push (object x)
-
- If isfull( )
- Throw new stackexception ( stack is full)
- Thearraytosx
-
18Pop method for array based stack
- Remove the most recently inserted item from the
stack - Exception underflow if the stack is empty
- Public void pop( ) throws underflow
-
- If (isempty( ))
- Throw new underflow (stackpop)
- Tos - -
-
19Top method for array based stack
- Return the most recently inserted item from the
stack - Exception underflow if the stack is empty
- Public object top( ) throws underflow
-
- If (isempty( ))
- Throw new underflow( stacktop)
- Return thearraytos
20Top and pop method for array based stack
- Return and remove the most recently inserted item
from the stack - Exception underflow if the stack is empty
- Public object topandpop( ) throws underflow
-
- If isempty( ) )
- Throw new underflow (stack topandpop)
- Return the arraytos - -
-
21Infix,postfix and prefix notations
- Consider the sum of A and B , we think of
applying the operator to the operands A and B
and write the sum as AB. - This particular representation is called infix.
- There are two alternative notations for
expressing the sum of A and B using the symbols
A,B and , these are - AB prefix
- AB postfix
22Suppose that we would like to rewrite ABC in
postfix
- Applying the rules of precedence,we obtained
- ABC
- A(BC) Parentheses for emphasis
- A(BC)Convert the multiplication,Let DBC
- A(D) Convert the addition
- ABC Postfix Form
23Evaluation a postfix expression (Eg ABC )
- Each operator in a postfix string refers to the
previous two operands in the string. - Suppose that each time we read an operand we push
it into a stack. When we reach an operator, its
operands will then be top two elements on the
stack - We can then pop these two elements, perform the
indicated operation on them, and push the result
on the stack. - So that it will be available for use as an
operand of the next operator.
24Algorithm for evaluating a postfix expression
- Initialize a stack, opndstk to be empty.
- scan the input string reading one element at a
time into symb - While there are more characters in the input
string - Do begin
- Symb next input character
- (Read symbol )
25Algorithm for evaluating a postfix expression
(Cond.)
- If symb is an operand
- Then push (opndstk,symb)
- Else symbol is an operator
- Begin
- Opnd2pop(opndstk) Opnd1pop(opndnstk)
- Value result of applying symb to opnd1 opnd2
- Push(opndstk,value)
- End else begin
- End while do begin
- Result pop (opndstk)
26Question 1 Evaluate the following expression
in postfix 623-382/23
- Final answer is
- 49
- 51
- 52
- 7
- None of these
27Evaluate- 623-382/23
- Symbol opnd1 opnd2 value opndstk
- 6 6
- 2 6,2
- 3 6,2,3
- 2 3 5 6,5
- - 6 5 1 1
- 3 6 5 1 1,3
28Evaluate- 623-382/23
- Symbol opnd1 opnd2 value opndstk
- 8 6 5 1
1,3,8 - 2 6 5 1
1,3,8,2 - / 8 2
4 1,3,4 - 3 4 7 1,7
- 1 7 7 7
- 2 1 7 7
7,2 - 7 2 49
49 - 3 7 2 49
49,3 - 49 3 52
52
29Question 2
- Consider the following arithmetic expression p
written in postfix notation - P5,6,2,,,12,4, /, -
- (commas are used to separate the elements of p
so that 5,6,2 is not interpreted as the number
562
30Which of the following statements are correct ?
- I Equivalent infix is 5(62)-12/4
- ii Final stack value is 37
- iii There is a one stack intermediate value . It
is equal to 40 - iv One of the stack intermediate value is 32
- v Equivalent infix is 95(62)-12)/4