0% found this document useful (0 votes)
19 views15 pages

Stack 2

Uploaded by

ssrindes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

Stack 2

Uploaded by

ssrindes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: CSH-231
Chapter: Stack
Subject Coordinator:
Mr. Vishal Kumar
(E12820)

Stack DISCOVER . LEARN .


Lecture No. 2.2.2 EMPOWER
Index
• Operations on stacks:
• PUSH

• POP

• Application of stacks:
• Parenthesis matching

2 2
OPERATIONS ON STACK
• The basic operation used for manipulation of stack
data structure are:
• (a) PUSH: To insert on item into a stack
• (b) POP: To delete on item from a stack
• Another operation used to know the top element of the
stack is PEEK().
STACK CONDITIONS
• If a stack has no item, this stack is called an empty
stack.
• However, PUSH operation can be performed on an
empty stack but a POP operation cannot be
applied.
• Hence before applying a POP operation one needs
to make sure that the stack is not empty.
• The two stack conditions to be checked before
performing operation on stack are:
(a) Underflow condition
• When POP operation is tried on an empty stack, it
is called underflow condition. This condition should
be false for successful POP operation and PEEK
operation.
(b) Overflow condition
• An overflow condition checks whether the stack is full or not i.e
whether memory space is available to push new elements onto
the stack. It is an undesirable condition where the program
requests for more memory space than what is allocated to a
particular stack.
Underflow condition: (empty (s) == TRUE)
Or
(TOP == -1)
Overflow condition: TOP == sizeof(s)
Where s is the stack on which PUSH and POP operations are to
be performed.
Push Operation on
Stack
Let n be the maximum size of stack and p be the element
to be inserted onto the stack s.
Push(s, Top, n, p)
{
if(Top = = n – 1)
{
printf(“stack overfl ow”);
exit(1);
}
Top ++;
S[Top] = p; //element p inserted on top of the stack
}
Pop Operation of Stack
Let n be the maximum size of stack and p be the variable which will
contain the value of top of the stack.
Pop(s, Top, n)
{
int p;
If(Top = = –1)
{
printf(“under fl ow condition”);
exit(1);
}
p = s[Top];
Top - - ;
return(p);
}
Application of Stack:
Some of the most popular application of stacks are:
(a) Expressions evaluation
(b) Fibonacci series
(c) Balanced parenthesis check
(d) Permutation
(e) Recursion
(f) Subroutines
(g) Tower of hanoi
Balanced Parentheses
• For checking balanced parentheses for every open brace there
should be a closing brace.
• The types of parenthesis used are:
(a) Simple or common bracket ( )
(b) Square bracket [ ]
(c) Curly bracket { }
Example:
{{ } [ ] ( )} Balanced Parenthesis
((((( ) ( ))))( ))
{[ ](}{( )) Imbalanced Parenthesis
[[ ][ ](( )
Continue..
(1) Push the open parenthesis onto the stack
(2) Whenever a close parenthesis is encountered then pop the
top of the stack which should be the matching parenthesis.
Note
The two conditions that must hold if the parenthesis in an
expression form an admissible pattern.
(1) The parenthesis count at the end of expression should be
0.This implies that there are as many left parenthesis as right
parenthesis.
(2) The parenthesis count should be non-negative at each
point in the expression.
Continue..
• Also, Nesting depth at a particular point in an
expression is the number off scopes that have been
• Consider : ((( ) ( ))( ))
Continue..
• The state of stack at various stages of processing
the expression for balanced parenthesis
{a + (b − [c + d]) * e − [(f + g)]}
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU

For queries
Email: [email protected]

15

You might also like