Chapter 2 BCA DSA Stack
Chapter 2 BCA DSA Stack
Kashiram pokharel
Stack definition:
• This implementation uses a one dimensional array to store the data and top of stack
is an integer value (an index of an array) that indicates the top position of a stack.
• Each time data is added or removed, top is incremented or decremented
accordingly, to keep track of current top of the stack.
• In Array we can push elements one by one from 0th position 1st position ……n-1th
position. Any element can be added or deleted at any place in an array but we can
push or pop the element from the top of the stack only.
• When there is no place for adding the element in the array, then this is called stack
overflow. So first we check the value of top with size of array.
• When there is no element in stack, then value of top will be -1. so we check the
value of top before deleting the element of stack
• Here, Stack is implemented with array, size of stack array is 7 and value of top is 2.
Let Stack[MAX] be an array to implement the stack. The variable top denotes the top
of the stack.
Algorithm for creating a stack
#define MAX 10
struct STACK
{
int stack[MAX]; //Declaring an array to store items
int top; //Top of a stack
};
typedef struct STACK st;
Algorithm for push( )
• Here stack is an array with MAX locations. TOP points to the top most
element. This procedure produces the top element of stack and
assigns it to the variable ITEM without removing it.
Algorithm: PEAK( )
1. Check stack underflow condition.
If TOP==-1, then
Print stack UNDERFLOW or Stack is empty, and End.
2. Assign TOP element to ITEM
Set ITEM = stack[TOP]
3. End.
Algorithm for traversing a stack
• Here stack is an array with MAX locations. TOP points to the top most element.
This procedure produces all the values (pushed on stack) without removing them.
• Algorithm: Traverse ( )
1. Check stack underflow condition
If TOP==-1, then
Print: stack UNDERFLOW or Stack is empty, and End.
2. Put TOP into T so that its value not to be changed
T = TOP
3. Repeat While T ≥ 0
Print stack [ T ]
Set T = T –1
4. End.
Mathematical expression:
• Infix Expression:
A+(B*C-(D/E^F)*G)*H
• Reverse the infix expression:
H*)G*)F^E/D(-C*B(+A
• Make Every left parenthesis “ ( ” as
right “ ) ” and every right
parenthesis “ ) ” as left “ ( ”
(H * ( G * ( F ^ E / D ) - C * B ) + A)
Convert the infix string C-B+A into prefix string.
Evaluation of prefix string:
• Scan the symbol of array pre-fix one by one from right to left.
• If symbol is operand, read its corresponding value and push in to stack.
• If symbol is operator, then pop last two element of stack and evaluate it as
[top] operator [top-1] & push it to stack.
• Do the same process until character comes in scanning.
• Pop the element of stack which will be value of evaluation of post fix
arithmetic expression.
Convert infix to prefix and evaluate the prefix expression
((A+B)-C)+D
EVALUATION (A=1,B=2,C=3,D=4)
Steps Input Stack prefix
symbol
1 D D s.n symbol val op1 op2 Value valstk
2 + + D (op1<op>op2)
3 ) +) D 1 D 4 - - - 4
4 C +) DC 2 C 3 - - - 4,3
5 - +)- DC 3 B 2 - - - 4,3,2
6 ) +)-) DC
4 A 1 - - - 4,3,2,1
7 B +)-) DCB
8 + +)-)+ DCB 5. + - 1 2 3 4,3,3
9 A +)-)+ DCBA 6 - - 3 3 0 4,0
10 ( +)- DCBA+ 7 + - 0 4 4 4
11 ( DCBA+-+
Prefix expression +-+ABCD
A*(B-C) GIVEN A=3,B=2,C=6 then infix 3*(2-6)result= -12
Symbol Stack Prefix
1 ) ) -
2 C ) C
3 - )- C
4 B )- CB
5 ( CB-
6 * * CB-
7 A * CB-A
8 CB-A*
Prefix expression is *A-BC
EVALUATION: *A-BC *3-26 A=3,B=2,C=6
SYMBOL VALUE OP1 OP2 VALUE STACK
1 C 6 6
2 B 2 6,2
3 - 2 6 -4 -4 ( [top] operator [top-1])
4 A 3 -4,3
5 * 3 -4 -12 -12
VALUE -12
Input Prefix_Stack Stack
Infix to prefix conversion G
+
G
G
(empty)
+
) G +)
) G +))
F GF +))
- GF +))-
• Let have an Infix Expression E GFE +))-
• ( (A+B) * (C+D) / (E-F) ) + G ( GFE- +)
/ GFE- +)/
• Reading Expression from “right to left” ) GFE- +)/)
character by character. D GFE-D +)/)
• We have Input, Prefix_Stack & Stack. + GFE-D +)/)+
• Now converting this expression to C GFE-DC +)/)+
( GFE-DC+ +)/
Prefix
* GFE-DC+ +)/*
) GFE-DC+ +)/*)
B GFE-DC+B +)/*)
+ GFE-DC+B +)/*)+
A GFE-DC+BA +)/*)+
( GFE-DC+BA+ +)/*
( GFE-DC+BA+*/ +
(empty) GFE-DC+BA+*/+ (empty)
Evaluate: