Unit-III: Introduction To Stacks Applications of Stacks
Unit-III: Introduction To Stacks Applications of Stacks
Unit-III: Introduction To Stacks Applications of Stacks
Introduction to stacks
applications of stacks
Stack
In stacks, special terms are given for insert and delete. i.e push
for insert and pop is for delete.
void push();
void pop();
void display();
int size();
void isEmpty();
void isFull();
Exceptions in
Stack
• Attemptin the execution of an operation
g may cause an error condition,
sometimes called an
exception. are said to be “thrown” by an operation
• Exceptions
that cannot be executed.
• In the Stack ADT, operations pop and top cannot
be performed if the stack is empty. The execution of
pop (or) top operation on an empty stack throws an
exception called as underflow.
• Trying to push an element in a full stack throws an
exception called as overflow.
Applications of stacks
Balancing symbols
Parenthesis matching.
Evaluation of postfix expressions.
Infix to prefix conversions.
Infix to postfix conversions.
Implementing function
calls(Recursion)
Web browser history
Undo operations in text editors
Matching tags in HTML and XML
Quick sort.
Implementing stack using arrays
Algorithm for inserting element into the stack:
Algorithm push()
1. if top=(SIZE-1)
then write (‘stack overflow’)
else
2. read item or data
3. top←top+1
4. stack[top]← item
5. stop
Explanation:
The stack is of size max. This procedure inserts an element
item on to the top of a stack which is represented by an
array stack.
The first step of this algorithm checks for an overflow
condition.
Overflow means inserting element into a stack which is
full.
If the top value reaches to maximum size of the stack then
elements cannot be inserted into the stack i.e. stack is full.
Otherwise top is incremented by one and element is
inserted into the stack.
Algorithm to delete elements from the stack:
Algorithm pop()
1. if top=-1
then write (‘stack underflow’)
else
2. item ← stack[top]
3. top ← top-1
Explanation:
This procedure deletes an element from the stack.
A B
A B C
A B C D
Stack is full when E is inserted create a new stack with double size
A B C D E and copy all the old stack elements to
new stack and named it as old stack
Representing Stack with Linked List
A variable, top, holds the address of the first cell in the list.
50 1500 110
0
40 1800 1500
30 1200 1800
20 1400 1200
Ex: ((A+B)*C)
This is a valid expression because in this no of
left parenthesis (2) = no: of right parenthesis (2).
Conversion of expressions
Arithmetic expressions can be represented in three ways:
Infix notation
Prefix notation
Postfix notation
1.Infix notation- In which operator should be placed in between the two operands.
Example- A+B C-D E*F G/H.
2. Prefix notation (polish notation)-
•Operator preceded by the operand is called prefix notation
Examples
+AB -CD *EF \GH.
3. Postfix notation(reverse polish notation or suffix notation)-
•Operator should be placed after operands.
AB+ CD- EF* GH\
Notations – Conversions
2 + 3 + 2 2 3 +
3
2 + 3 * 5 + 2 * 3 5 2 3 5 * +
(2 + 3) * 5 * + 2 3 5 2 3 + 5 *
– * 2 3 + 2 3 * 5 9 +
2 * 3 – (5 + 9)
5 9 –
Infix to post fix(RPN) Conversion
Algorithm to convert infix expression to postfix expression(RPN):
1. Declare a stack and postfix array(output : postfix expression)
2. Repeat the following steps until the end of the infix expression is reached.
1. Get input token (constant, variable, arithmetic
operator, left parenthesis, right parenthesis) in the infix
expression.
2. If the token is
1. A left parenthesis: Push it
onto the stack.
2. A right parenthesis:
1.Pop the stack elements and add to postfix array until a left
parenthesis is on the top of the stack.
2. Pop the left parenthesis also, but do not add to postfix
array
3. An operator:
1. While the stack is nonempty and token has lower or equal
priority than stack top element, pop and add to postfix array.
2. Push token onto the stack.
4. An operand: add to postfix
Note
• the lower precedence operator never placed
on top of the higher precedence.
• (A-B)*(D/E)
Operator Precedence
Infix expression- (A+B)^C-(D*E)/F
Infix Stack Post fix
( ( Empty
A ( A
+ (+ A
B (+ AB
) Empty AB+
^ ^ AB+
C ^ AB+C
- - AB+C^
( -( AB+C^
D -( AB+C^D
* -(* AB+C^D
E -(* AB+C^DE
) - AB+C^DE*
/ -/ AB+C^DE*
F -/ AB+C^DE*F
Empty AB+C^DE*F/-
Postfix (reverse polish notation) expression
evaluation
• Algorithm
1. Scan expression from left to right and repeat steps 2 and 3 for each
element of expression.
2.If an operand is encountered, push it on to stack.
3.If an operator op1 is encountered then
1. remove the top two elements of stack, where A is the top and B is the next
top element.
2. evaluate B op1 A
3. push the result back on to the stack.
4. set the top value on stack.
5. stop
Evaluate the expression
Postfix:- 5,6,2,+,*,12,4,/,-
•
Symbol stack content
scanned 5
5
5 6
6
5 6
2 2
+ 5 8
*
40
12 40 12
4 40 12 4
/ 40 3
- 40-3
37
Convert infix to postfix
1. expression
(A-B)*(D/E)
2. (A+B^D)/(E-F)+G
3. A*(B+D)/E-F*(G+H/K)
4. ((A+B)*D)^(E-F)
5. (A-B)/((D+E)*F)
6. ((A+B)/D)^((E-F)*G)
7. 12/(7-3)+2*(1+5)
8. 5+3^2-8/4*3+6
9. 6+2^3+9/3-4*5
10. 6+2^3^2-4*5
Evaluate the postfix expression
1. 5,3,+,2,*,6,9,7,-,/,-
2. 3,5,+,6,4,-,*,4,1,-,2,^,+
3. 3,1,+,2,^7,4,1,-,2,*,+,5.-