0% found this document useful (0 votes)
2 views

Unit-3

The document provides a comprehensive overview of stacks, a linear data structure that follows the Last-In, First-Out (LIFO) principle. It details stack operations such as PUSH, POP, and PEEK, along with their implementations using arrays and linked lists. Additionally, it covers applications of stacks, infix, postfix, and prefix notations, and algorithms for converting between these notations.

Uploaded by

Sanika Deshmukh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-3

The document provides a comprehensive overview of stacks, a linear data structure that follows the Last-In, First-Out (LIFO) principle. It details stack operations such as PUSH, POP, and PEEK, along with their implementations using arrays and linked lists. Additionally, it covers applications of stacks, infix, postfix, and prefix notations, and algorithms for converting between these notations.

Uploaded by

Sanika Deshmukh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UNIT 2

Stacks
Introduction
• Stack is an important data structure which stores its elements in
an ordered manner.

• Take an analogy of a pile of plates where one plate is placed on


top of the other. A plate can be removed only from the topmost
position. Hence, you can add and remove the plate only at/from
one position, that is, the topmost position.
Another plate will be The topmost plate will be
added on top of this plate removed first
PUSH OPERATION
POP OPERATION
Stacks
• A stack is a linear data structure which uses the same principle,
i.e., the elements in a stack are added and removed only from
one end, which is called the top.

• Hence, a stack is called a LIFO (Last-In, First-Out) data structure as


the element that is inserted last is the first one to be taken out.

• Stacks can be implemented either using an array or a linked list.


OPERATIONS OF STACKS

• PUSH

• POP

• PEEK/PEEP
Array
Representation
of
Stacks

© Oxford University Press 2013. All rights reserved.


Array Representation of Stacks
• In computer’s memory stacks can be represented as a linear array.
• Every stack has a variable TOP associated with it.
• TOP is used to store the address of the topmost element of the
stack. It is this position from where the element will be added or
deleted.
• There is another variable MAX which will be used to store the
maximum number of elements that the stack can hold.
• If TOP = NULL, then it indicates that the stack is empty and if TOP =
MAX -1, then the stack is full.
Push Operation
• The push operation is used to insert an element in to the stack.
• The new element is added at the topmost position of the stack.
• However, before inserting the value, we must first check if
TOP=MAX-1, because if this is the case then it means the stack is
full and no more insertions can further be done.
• If an attempt is made to insert a value in a stack that is already full,
an OVERFLOW message is printed.

A B C D E

0 1 2 3 TOP = 4 5 6 7 8 9

A B C D E F

0 1 2 3 4 TOP =5 6 7 8 9
Pop Operation
• The pop operation is used to delete the topmost element from the
stack.
• However, before deleting the value, we must first check if
TOP=NULL, because if this is the case then it means the stack is
empty so no more deletions can further be done.
• If an attempt is made to delete a value from a stack that is already
empty, an UNDERFLOW message is printed.
A B C D E

0 1 2 3 TOP = 4 5 6 7 8 9

A B C D

0 1 2 TOP = 3 4 5 6 7 8 9
Peek Operation
• Peek is an operation that returns the value of the topmost
element of the stack without deleting it from the stack.
• However, the peep operation first checks if the stack is empty or
contains some elements.
• If TOP = NULL, then an appropriate message is printed else the
value is returned.
A B C D E

0 1 2 3 TOP = 4 5 6 7 8 9

Here Peep operation will return E, as it is the value of the


topmost element of the stack.
Algorithms for PUSH Operations
Algorithms for POP Operations

Step 1: IF TOP = NULL, then


PRINT “UNDERFLOW”
Goto Step 4
[END OF IF]
Step 2: SET STACK[TOP] = VALUE
Step 3: SET TOP = TOP - 1
Step 4: END
Algorithm for Peep Operation
Step 1: IF TOP =NULL, then
PRINT “STACK IS EMPTY”
Go TO Step 3
[END OF IF]
Step 2: RETURN STACK[TOP]
Step 3: END
LINKED LIST
Representation
of
Stacks

© Oxford University Press 2013. All rights reserved.


Linear Representation of Stacks
• In a linked stack, every node has two parts – one that stores data
and another that stores the address of the next node.

• The START pointer of the linked list is used as TOP.

• If TOP is NULL then it indicates that the stack is empty.

1 7 3 4 2 6 5 X

TOP
Push Operation on a Linked Stack
Step 1: Allocate memory for the new node and name
it as New_Node
Step 2: SET New_Node->DATA = VAL
Step 3: IF TOP = NULL, then
SET New_Node->NEXT = NULL
SET TOP = New_Node
ELSE
SET New_node->NEXT = TOP
SET TOP = New_Node
[END OF IF]
Step 4: END
1 7 3 4 2 6 5 X

TOP

9 1 7 3 4 2 6 5 X

TOP
Pop Operation on a Linked Stack
Step 1: IF TOP = NULL, then
PRINT “UNDERFLOW”
Goto Step 5
[END OF IF]
Step 2: SET PTR = TOP
Step 3: SET TOP = TOP ->NEXT
Step 4: FREE PTR
Step 5: END
9 1 7 3 4 2 6 5 X

TOP

1 7 3 4 2 6 5 X

TOP
Applications of Stacks
• Reversing a list

• Parentheses Checker

• Conversion of an infix expression into a postfix expression

• Evaluation of a postfix expression

• Conversion of an infix expression into a prefix expression

• Evaluation of a prefix expression

• Recursion

• Tower of Honai
Infix Notation
• Infix, Postfix and Prefix notations are three different but equivalent
notations of writing algebraic expressions.
• While writing an arithmetic expression using infix notation, the
operator is placed between the operands. For example, A+B; here,
plus operator is placed between the two operands A and B.
• Although it is easy to write expressions using infix notation,
computers find it difficult to parse as they need a lot of information
to evaluate the expression.
• Information is needed about operator precedence, associativity
rules, and brackets which overrides these rules.
• So, computers work more efficiently with expressions written using
prefix and postfix notations.
Postfix Notation
• Postfix notation was given by Jan Lukasiewicz who was a Polish
logician, mathematician, and philosopher. His aim was to develop
a parenthesis-free prefix notation (also known as Polish notation)
and a postfix notation which is better known as Reverse Polish
Notation or RPN.
• In postfix notation, the operator is placed after the operands. For
example, if an expression is written as A+B in infix notation, the
same expression can be written as AB+ in postfix notation.
• The order of evaluation of a postfix expression is always from left
to right.
Postfix Notation

• The expression (A + B) * C is written as:

AB+C* in the postfix notation.

• A postfix operation does not even follow the rules of operator

precedence. The operator which occurs first in the expression is

operated first on the operands.

• For example, given a postfix notation AB+C*. While evaluation,

addition will be performed prior to multiplication.


Prefix Notation
• In a prefix notation, the operator is placed before the operands.
• For example, if A+B is an expression in infix notation, then the
corresponding expression in prefix notation is given by +AB.
• While evaluating a prefix expression, the operators are applied to
the operands that are present immediately on the right of the
operator.
• Prefix expressions also do not follow the rules of operator
precedence, associativity, and even brackets cannot alter the
order of evaluation.
• The expression (A + B) * C is written as:
*+ABC in the prefix notation
Evaluation of an Infix Expression to Post fix
Algorithm to convert an Infix notation into postfix notation
Step 1: Add ‘)” to the end of the infix expression
Step 2: Push “(“ on to the stack
Step 3: Repeat until each character in the infix notation is scanned
IF a “(“ is encountered, push it on the stack
IF an operand (whether a digit or an alphabet) is encountered,
add it to the postfix expression.
IF a “)” is encountered, then;
A. Repeatedly pop from stack and add it to the postfix expression until a
“(” is encountered.
B. Discard the “(“. That is, remove the “(“ from stack and do not add it
to the postfix expression
IF an operator X is encountered, then;
Repeatedly pop from stack and add each operator (popped from the
stack) to the postfix expression which has the same precedence or a
higher precedence than X
b. Push the operator X to the stack
Step 4: Repeatedly pop from the stack and add it to the postfix expression
Infix Notation to Post fix Notation

Step 1: Add ‘)” to the end of the infix expression


Step 2: Push “(“ on to the stack
Step 3: Repeat until each character in the infix notation is scanned
IF a “(“ is encountered, push it on the stack
IF an operand (whether a digit or an alphabet) is encountered,
add it to the postfix expression.
IF a “)” is encountered, then;

Repeatedly pop from stack and add it to the postfix expression until a “(”
is encountered.

Discard the “(“. That is, remove the “(“ from stack and do not add it to
the postfix expression
Evaluation of an Infix Expression
Step 1: Add a “)” at the end of the postfix expression
Step 2: Scan every character of the postfix expression and repeat
steps 3 and 4 until “)”is encountered
Step 3: IF an operand is encountered, push it on the stack
IF an operator X is encountered, then
a. pop the top two elements from the stack as A and B
b. Evaluate B X A, where A was the topmost element and B was
the element below A.
c. Push the result of evaluation on the stack
[END OF IF]
Step 4: SET RESULT equal to the topmost element of the stack
Step 5: EXIT
Evaluation of an Infix Expression
• Let us now take an example that makes use of this algorithm.
• Consider the infix expression given as “9 - (( 3 * 4) + 8) / 4”.
• The infix expression "9 - (( 3 * 4) + 8) / 4" can be written as “9 3 4 * 8
+ 4 / -“ using postfix notation.
• Look at table which shows the procedure.

Character scanned Stack


9 9
3 9, 3
4 9, 3, 4
* 9, 12
8 9, 12, 8
+ 9, 20
4 9, 20, 4
/ 9, 5
- 4
Convert Infix Expression into Prefix Expression
Consider an infix expression: (A – B / C) * (A / K – L)
• Step 1: Reverse the infix string. Note that while reversing the string
you must interchange left and right parenthesis.
(L – K / A) * (C / B – A)
• Step 2: Obtain the corresponding postfix expression of the infix
expression obtained as a result of Step 1.
• The expression is: (L – K / A) * (C / B – A)
• Therefore, [L – (K A /)] * [ (C B /) - A ]
= [LKA/-] * [ CB/A-]
=LKA/-CB/A-*
• Step 3: Reverse the postfix expression to get the prefix expression
• Therefore, the prefix expression is * - A / B C - / A K L

You might also like