Preeja Unit1 Stack
-
Upload
preeja-ravishankar-wayanad -
Category
Documents
-
view
220 -
download
0
Transcript of Preeja Unit1 Stack
-
8/11/2019 Preeja Unit1 Stack
1/38
Preeja
References:
Data Structures and Algorithms:Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman
A Practical Approach to Data Structures andAlgorithms: Sanjay Pahuja
-
8/11/2019 Preeja Unit1 Stack
2/38
STACK An Abstract Data Type
Special List : insertions & deletions at one
end(top)
Also called LIFO(Last In First Out) list.
8/27/2014 2Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
3/38
8/27/2014 Data Structures - Module1 3
-
8/11/2019 Preeja Unit1 Stack
4/38
8/27/2014 Data Structures - Module1 4
-
8/11/2019 Preeja Unit1 Stack
5/38
Operations performed on stack MAKENULL(S) : Make stack S an empty stack.
TOP(S) : Return the top element of stack S.
POP(S) : Delete the top element from the stack. {check
for stack empty condition}
PUSH(x,S) : Insert the element x at the top of stack S.{ Stack overflow condition should be checked}
EMPTY(S) : Returns true if S is an empty stack.
8/27/2014 5Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
6/38
Stack Implementation Static implementation(Using arrays)
Dynamic implementation(Using pointers: Linked list)
8/27/2014 6Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
7/38
Definition of Array-Based Stack typedef struct s
{
intdata[MAX];inttop;
}s;
8/27/2014 Data Structures - Module1 7
-
8/11/2019 Preeja Unit1 Stack
8/38
Push Operation on StackWe have declared data array in the above declaration.
Whenever we add any element in the data array then
it will be called as Pushing Data on the Stack.
8/27/2014 8Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
9/38
Initial stack
8/27/2014 Data Structures - Module1 9
3
2
1
0
Top = -1
-
8/11/2019 Preeja Unit1 Stack
10/38
Push (12, S)
8/27/2014 Data Structures - Module1 10
s.top:=s.top+1= -1 + 1 = 0;
s.elements[s.top]:= s.elements[0] = 12;
3
2
1
0 12 Top = 0
-
8/11/2019 Preeja Unit1 Stack
11/38
-
8/11/2019 Preeja Unit1 Stack
12/38
Push (47, S)
8/27/2014 Data Structures - Module1 12
s.top:=s.top+1= 1 + 1 = 2;
s.elements[s.top]:= s.elements[2] = 47;
3
2 47
1 34
0 12
Top = 2
-
8/11/2019 Preeja Unit1 Stack
13/38
-
8/11/2019 Preeja Unit1 Stack
14/38
POP Operation on StackWhenever we try to remove element from the stack
then the operation is called as POP Operation on
Stack.
8/27/2014 14Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
15/38
-
8/11/2019 Preeja Unit1 Stack
16/38
But pseudocode does not use specific programminglanguage syntax and therefore could be understood byprogrammers who are familiar with differentprogramming languages.
Additionally, transforming an algorithm presented inpseudocode to programming code could be mucheasier than converting an algorithm written in naturallanguage.
8/27/2014 Data Structures - Module1 16
-
8/11/2019 Preeja Unit1 Stack
17/38
Operation PUSH(Stack, Top, MAX Stack, Item)
1.Stack is already filled?
If top=MAX STK
Print :Over flow and Return
2.Set top=top+1
3.Set stack[top]=item
4.return
8/27/2014 17Data Structures - Module1
Algorithm for push
-
8/11/2019 Preeja Unit1 Stack
18/38
POP(Stack,top,Item)
1. Stack has an item to be removed.
If top=0
Print :underflow and return
2. Set item =stack[top]
3. Set top=top-1
Return
8/27/2014 18Data Structures - Module1
Algorithm for POP
-
8/11/2019 Preeja Unit1 Stack
19/38
Applications of stack Explicit and implicit recursive calls
Conversion of Infix to postfix
Postfix expression evaluation
Conversion of infix to prefix
Prefix expression evaluation
8/27/2014 19Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
20/38
Applications of stack Expression : operands together with operator
Infix notation(A+B)
Prefix notation( +AB)
Postfix notation( AB+) Operator precedence
^ (Exponential ) .Highest
*, /(mul, div) Next highest+, - (add,sub) Lowest
8/27/2014 20Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
21/38
Infix ,Prefix and Postfix Prefix is easy to program for in computers, but postfix
uses less memory.
Infix notation:X + Y Operators are written in-between
their operands. This is the usual way we writeexpressions.
An expression such asA * ( B + C ) / Dis usually takento mean something like: "First add B and C together,
then multiply the result by A, then divide by D to givethe final answer."
8/27/2014 21Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
22/38
-
8/11/2019 Preeja Unit1 Stack
23/38
Infix ,Prefix and Postfix Prefix notation : + X Y Operators are written before
their operands.
The expressions given above are equivalent
to / * A + B C D
As for Postfix, operators are evaluated left-to-right andbrackets are superfluous. Operators act on the two
nearest values on the right.
8/27/2014 23Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
24/38
Infix ,Prefix and Postfix
8/27/2014 24Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
25/38
Applications of stack
Conversion from infix to postfix
Rules
1. Parenthesize the expression starting from left to right
2. The operands associated with the operator having higherprecedence will be parenthesized first.
3. The sub expression which has been converted to postfix
is to be treated as single operand.
4. Once the expression is converted to postfix form,
remove the parenthesis
8/27/2014 25Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
26/38
Infix to postfix conversion
algorithm1. In this Algorithm we are reading token from Left to
Right and Postfix expression is generated.
2. So Entered Token maybe -a) Alphabet from A-Z or a-Z
b) Numerical Digit from 0-9
c) Operator
d) Opening And Closing Braces ( , )3. If Entered Character isAlphabetthen Following
Action Should be taken-
a. Print Alphabet as Output
8/27/2014 26Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
27/38
4. If Entered Character isDigitthen Following
Action Should be taken-1. Print Digit as Output
5. If Entered Character isOpening BracketthenFollowing Action Should be taken-
1. Push ( Onto Stack2. If any Operator Appears before ) then Push it onto
Stack.
3. If Corresponding ) bracket appears then Start
Removing Elements [Pop] from Stack till ( isremoved.
8/27/2014 27Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
28/38
6. If Entered Character isOperatorthen Following
Action Should be taken-a) Check Whether There is any Operator Already present
in Stack or not.
b) If Stack is Empty then Push Operator Onto Stack.
c) If Present then Check Whether Priority of IncomingOperatoris greater than Priority of Topmost StackOperator.
d) If Priority of Incoming Operator is Greater then Push
Incoming Operator Onto Stack.e) Else Pop Operator From Stack again goto Step 6.
8/27/2014 28Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
29/38
Infix to Prefix ConversionStep 1. Push ) onto STACK, and add ( to end of the A
Step 2. Scan A from right to left and repeat step 3 to 6 for
each element of A until the STACK is empty
Step 3. If an operand is encountered add it to B
Step 4. If a right parenthesis is encountered push it onto
STACK
8/27/2014 29Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
30/38
Step 5. If an operator is encountered then:
a. Repeatedly pop from STACK and add to B each
operator (on the top of STACK) which has same orhigher precedence than the operator.
b. Add operator to STACK
8/27/2014 30Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
31/38
Step 6. If left parenthesis is encountered then
a. Repeatedly pop from the STACK and add to B
(each operator on top of stack until a left parenthesis isencounterd)
b. Remove the left parenthesis
Step 7. Exit
8/27/2014 31Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
32/38
Evaluating Postfix expression(1) Add a right parenthesis )at the end of P[this acts as a
sentinel].
(2) Scan P from left to right and repeat steps 3 and 4 foreach element of P until the sentinel )is encountered.
(3) If an operand is encountered, put it on STACK.(4) If an operator # is encountered, then
(a) Remove the two top elements of STACK ,where A isthe top element and B is the next-to-top element.
(b) Evaluate B # A.(c) Place the result onto STACK.
5) RESULT equal to the top element on stack.
6) Exit
8/27/2014 32Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
33/38
8/27/2014 Data Structures - Module1 33
Evaluate 45+72-*
-
8/11/2019 Preeja Unit1 Stack
34/38
Evaluate 59+2*65*+
8/27/2014 Data Structures - Module1 34
-
8/11/2019 Preeja Unit1 Stack
35/38
Infix to prefix conversion algorithm
I => infix expressionO => output expression
P => prefix expression
(1) Push )onto stack and add (to the beginning of I.(2) Scan I from right to left and repeat steps 3 to 6 for each
element of I until the stack is empty.
(3) If an operand is encountered, add it to O.
(4) If an operator # is encountered, then
(a) Repeatedly pop from stack and add to O eachoperator (on the top of stack) which has higher
precedence than #.
8/27/2014 35Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
36/38
(b) Add # to stack.
5. If a right parenthesis is encountered, push it onto thestack.
6. If a left parenthesis is encountered then
(a) Repeatedly pop from stack and add to O, until a
right parenthesis is encountered.
(b) Remove the right parenthesis.
[ do not add the right parenthesis to O].
7. Reverse O to get the prefix expression P.8. Exit
# Symbolizes any operator in I.8/27/2014 36Data Structures - Module1
-
8/11/2019 Preeja Unit1 Stack
37/38
Convert (A*B-F*H)^D
8/27/2014 Data Structures - Module1 37
Symbol Scanned Operator Stack Output Expression
D ) D^ )^ D
) )^) D
H )^) DH
* )^)* DH
F )^)* DHF
- )^)- DHF*
B )^)- DHF*B
* )^)-* DHF*B
A )^)-* DHF*BA( )^ DHF*BA*-
( (Empty) DHF*BA*-^
Result: ^-*AB*FHD
-
8/11/2019 Preeja Unit1 Stack
38/38
Home Work Convert to prefix:
A+ [ B+C - D+E] * F / G
+A/*+-+BCDEFG (ab)/c*(d + e f / g)
*/-abc-+de/fg