You are on page 1of 17

STACK

What is a STACK ? Insertion Deletion

 Ordered list of elements of same data type


4 Top

 Linear list
 Top 3

 Last-In First-Out (LIFO) principle. 2

0
STACK

 Stack is an abstract data type with a bounded(predefined)


capacity.
 It is a simple data structure that allows adding and removing
elements in a particular order.
 The order may be LIFO(Last In First Out) or FILO(First In Last
Out).
 The element which is placed (inserted or added) last, is accessed
first. 
 Every time an element is added, it goes on the top of the stack
and the only element that can be removed is the element that is
at the top of the stack, just like a pile of objects.
Basic features of Stack

1.Stack is an ordered list of similar data type.


2.Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
3.push() function is used to insert new elements into the Stack and pop() function is used to
remove an element from the stack. Both insertion and removal are allowed at only one end
of Stack called Top.
4.Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.
operations are performed in the stack:

• Push: Adds an item in the stack. If the stack is full, then it is said to be


an Overflow condition.
• Pop: Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty, then it
is said to be an Underflow condition.
• Peek or Top: Returns top element of stack.
• isEmpty: Returns true if stack is empty, else false.
Applications of stack
• Balancing of symbols
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree traversals, stock span
problem, histogram problem.
• Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen
problem and sudoku solver
• In Graph Algorithms like Topological Sorting and Strongly Connected Components
Push Operation
The process of putting a new data element onto stack is known as a
Push Operation. 

• Step 1 − Checks if the stack is full.


• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success.
Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.

CASE 2:
Push( stack, MAX, Top, Ele)
{
IF Top=MAX then Top= 4 D Max= 4
Print OVERFLOW” and
Let us have Max= 4, Top=4
Exit C Ele=E
ELSE
Set Top=Top+1 B Step 1. Top=Max
Stack[Top]= Ele 4=4  True
Return Print OVERFLOW
A EXIT
}
Step 2. Return
Let us have Max= 4, Top=2
Max= 4 Ele=C

Step 1. Top=Max
2=4
2=4false
B Top= 2 Go to Else Part Max= 4
a. Top=Top+1
A =2+1 C Top= 3
Top=3
b. Stack[top]=Ele B
=C
Stack[3]=C
Step 2. Return
A
POP- OPERATION
Accessing the content while removing it from the stack, is known as a Pop
Operation.

• Step 1 − Checks if the stack is empty.


• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
POP Procedure: POP(STACK, TOP, ITEM)
1.If TOP=0 then
Print UNDERFLOW and Exit.
Else
Set Item:=STACK[TOP]
Set TOP:=TOP-1
2. Return

CASE 1 CASE 2
Let Max=4 TOP=3
Max= 4 Step 1: Top=0 Max= 4
Let Max=4 TOP=0
3=0False Step 1: Top=0
C Top= 3 0=0True
Go to ELSE Part
B Item=Stack[Top] Print UNDERFLOW
= Stack[3] Max= 4 2. Return
Item= C
A Top=Top-1 Top= 0
=3-1=2
Top=2 B Top= 2
2. Return
A
EVALUATION OF EXPRESSION
The way to write arithmetic expression is known as a notation.
An arithmetic expression can be written in three different but
equivalent notations.
These notations are −
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
Infix Notation
•We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands.
Prefix Notation
•In this notation, operator is prefixed to operands, i.e. operator is written ahead of
operands. For example, +ab.
•Prefix notation is also known as Polish Notation.
Postfix Notation
•This is known as Reversed Polish Notation.
•In this notation style, the operator is postfixed to the operands i.e., the operator is
written after the operands. For example, ab+.
Postfix Evaluation Algorithm
• Step 1 − scan the expression from left to right
• Step 2 − if it is an operand push it to stack
• Step 3 − if it is an operator pull operand from stack and perform
operation
• Step 4 − store the output of step 3, back to stack
• Step 5 − scan the expression until all operands are consumed
• Step 6 − pop the stack and perform operation
• Read all the symbols one by one from left to right in the given
Postfix Expression
• If the reading symbol is operand, then push it on to the Stack.
• If the reading symbol is operator (+ , - , * , / etc.,), then perform
TWO pop operations and store the two popped oparands in two
different variables (operand1 and operand2). Then perform reading
symbol operation using operand1 and operand2 and push result
back on to the Stack.
• Finally! perform a pop operation and display the popped value as
final result.
Step 1 − scan the expression from left to right
Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and
perform operation
Step 4 − store the output of step 3, back to stack
Step 5 − scan the expression until all operands are consumed
Step 6 − pop the stack and perform operation
Steps to convert infix expression to postfix
1. Scan the given infix expression from left to right.
2. If the scanned character is an operand, then output it.
3. Else,
•If the precedence of the scanned operator is greater than the precedence of the operator in the
top of the stack(or the stack is empty or if the stack contains a ‘(‘ ), push it.
•Else, Pop all operators from the stack whose precedence is greater than or equal to that of the
scanned operator. Then, push the scanned operator to the top of the stack. (If you encounter
parenthesis while popping then stop there and push the scanned operator in the stack.)
4. If the scanned character is ‘(‘, push it to the stack.
5. If the scanned character is ‘)’, pop the stack and output characters until ‘(‘ is encountered, and
discard both the parenthesis.
6. Repeat steps 2-5 until infix expression is scanned completely.
7. Print the output.
8. Pop and output from the stack.
INFIX EXPRESSION: a+(b-c/d)-e Read char d—operand (output it)
/
1. Scan the given infix expression from left to right. -
Read char a—operand (output it)
2. If the scanned character is an operand, then  (
stack->empty + abc d
output it.
 
3. Else, a Read char  )operator pop ( upto ( )
•If the precedence of the scanned operator is Read char  +operator push (+)  
greater than the precedence of the operator in the   + abc d/-
top of the stack(or the stack is empty or if the stack +
contains a ‘(‘ ), push it.
Read char  (operator push ( ( ) Read char  - operator push (-)
•Else, Pop all operators from the stack whose  (  -
precedence is greater than or equal to that of the + + abc d/-
scanned operator. Then, push the scanned
operator to the top of the stack. (If you encounter Read char b—operand (output it)
parenthesis while popping then stop there and  ( - and + have equal precedence but + have higher order. So
push the scanned operator in the stack.) + ab Pop + and Push -
4. If the scanned character is ‘(‘, push it to the Read char  - operator push (-)  
stack. - - abc d/-+
5. If the scanned character is ‘)’, pop the stack and  (
and output characters until ‘(‘ is encountered, and + Read char e—operand (output it)
discard both the parenthesis. Read char c—operand (output it)  
6. Repeat steps 2-5 until infix expression is scanned - - -
completely. -  (  ( abcd/-+ e
 ( + abc - Expression scanned completely ,pop element from stack
7. Print the output.
-Read char  /operator push (/) until it will empty.
8. Pop and output from the stack. +  
/
 ( -
+  ( + abcd/-+ e-
+
INFIX EXPRESSION: a+(b-c/d)-e
+

POSTFIX EXPRESSION: a b c d / - + e -

You might also like