2. INTRODUCTION
08-04-2022
2
NIT, Jamshedpur
• It is a non-primitive data structure.
• It is an ordered list in which addition of new data item and deletion of already existing
data item is done from only one end known as top of stack (TOS).
• As all the deletion and insertion in a stack is done fromTOS, the last added element will
be the first to be removed from the stack.
• This is the reason why stack is also called Last-in-First-Out (LIFO) type of list.
• Note that the most frequently accessible element in the stack is the topmost element,
whereas the last accessible element is the bottom of the stack.
5. STACK IMPLEMENTATION
• Stacks can be implemented in two ways:
• Static implementation: It uses arrays to create stack. Though a very
simple technique, but is not a flexible way of creation as the size of
stack has to be declared during program design, after that the size
cannot be varied.
• Dynamic implementation: It is also called linked list representation and
uses pointers to implement the stack type of data structure.
08-04-2022
5
NIT, Jamshedpur
6. OPERATIONS ON STACK
• PUSH:
• The process of adding a new element to the top of stack is called
PUSH operation.
• Pushing an element in the stack invoke adding of element as the new
element will be inserted at the top.
• After every push operation, the top is incremented by 1.
• In case the array is full and no new element can be accommodated, it
is called STACK-FULL condition and is also known as STACK
OVERFLOW.
08-04-2022
6
NIT, Jamshedpur
7. OPERATIONS ON STACK
• POP:
• The process of deleting an element from the top of stack is called
POP operation.
• After every pop operation, the stack is decremented by 1.
• If there is no element on the stack and the pop is performed, it will
result into STACK UNDERFLOW condition.
08-04-2022
7
NIT, Jamshedpur
8. STACKTERMINOLOGY
08-04-2022
8
NIT, Jamshedpur
• Context: The environment is which a function executes.
• Stack frames: The data structure containing all the data needed
each time a procedure or function is called.
• MAXSIZE: Maximum size of the stack.
• TOP: It is used to check stack overflow or underflow conditions.
Initially Top stores -1. This assumption is taken so that whenever an
element is added to the stack, the Top is first incremented and then
the item is inserted into the location currently indicated by Top.
9. STACKTERMINOLOGY
08-04-2022
9
NIT, Jamshedpur
• STACK: It is an array of size MAXSIZE.
• Stack Underflow: This is the situation when the stack contains no
element. At this point, the top of stack is present at the bottom of
the stack.
• Stack Overflow: This is the situation when the stack becomes full
and no more elements can be pushed onto the stack. At this point,
the stack top is present at the highest location of the stack.
10. ALGORITHM FOR PUSH(STACK[MAXSIZE], ITEM)
08-04-2022
10
NIT, Jamshedpur
1. Initialize:
set top = -1
2. Repeat steps 3 to 5 until top<MAXSIZE – 1
3. Read Item
4. set top = top + 1
5. set stack[top] = item
6. Print “Stack Overflow”
11. ALGORITHM FOR POP(STACK[MAXSIZE], ITEM)
08-04-2022
11
NIT, Jamshedpur
1. Repeat steps 2 to 4 until top >= 0
2. set Item = stack[top]
3. set top = top - 1
4. print the item deleted
5. Print “Stack Underflow”
12. APPLICATIONS OF STACKS
08-04-2022
12
NIT, Jamshedpur
• Evaluation of Arithmetic Expressions
• Backtracking
• Delimiter Checking
• Reverse a Data
• Processing Function Calls
13. EVALUATION OF ARITHMETIC OPERATIONS
08-04-2022
13
NIT, Jamshedpur
• A stack is a very effective data structure for evaluating arithmetic
expressions in programming languages.
• An arithmetic expression consists of operands and operators.
• In addition to operands and operators, the arithmetic expression may also
include parenthesis like "left parenthesis" and "right parenthesis".
• Example: A + (B - C)
• To evaluate the expressions, one needs to be aware of the standard
precedence rules for arithmetic expression.
14. EVALUATION OF ARITHMETIC OPERATIONS
08-04-2022
14
NIT, Jamshedpur
• The precedence rules for the five basic arithmetic operators are:
• Evaluation of Arithmetic Expression requires two steps:
• First, convert the given expression into special notation.
• Evaluate the expression in this new notation.
Operators Associativity Precedence
^ exponentiation Right to left Highest followed by *Multiplication and /division
*Multiplication, /division Left to right Highest followed by + addition and -
subtraction
+ addition, - subtraction Left to right lowest
15. NOTATIONS FOR ARITHMETIC EXPRESSION
08-04-2022
15
NIT, Jamshedpur
• There are three notations to represent an arithmetic expression:
• Infix Notation: The infix notation is a convenient way of writing an expression in which each operator is placed
between the operands. Infix expressions can be parenthesized or unparenthesized depending upon the problem
requirement.
• Example: A + B, (C - D) etc.
• All these expressions are in infix notation because the operator comes between the operands.
• Prefix Notation: The prefix notation places the operator before the operands. This notation was introduced by
the Polish mathematician and hence often referred to as polish notation.
• Example: + AB, -CD etc.
• All these expressions are in prefix notation because the operator comes before the operands.
• Postfix Notation: The postfix notation places the operator after the operands. This notation is just the reverse
of Polish notation and also known as Reverse Polish notation.
• Example: AB +, CD+, etc.
• All these expressions are in postfix notation because the operator comes after the operands.
16. CONVERSION OF ARITHMETIC EXPRESSION INTO
VARIOUS NOTATIONS
08-04-2022
16
NIT, Jamshedpur
Infix Notation Prefix Notation Postfix Notation
A * B * A B AB*
(A+B)/C /+ ABC AB+C/
(A*B) + (D-C) +*AB - DC AB*DC-+
17. EVALUATING POSTFIX EXPRESSION
• Stack is the ideal data structure to evaluate the postfix expression because the
top element is always the most recent operand.
• The next element on the Stack is the second most recent operand to be
operated on.
• Before evaluating the postfix expression, the following conditions must be
checked. If any one of the conditions fails, the postfix expression is invalid.
• When an operator encounters the scanning process, the Stack must contain a pair of
operands or intermediate results previously calculated.
• When an expression has been completely evaluated, the Stack must contain exactly one
value.
08-04-2022
17
NIT, Jamshedpur
18. EVALUATING POSTFIX EXPRESSION
• Now let us consider the following infix
expression 2 * (4+3) - 5.
• Its equivalent postfix expression is 2 4 3 + *
5.
08-04-2022
18
NIT, Jamshedpur
19. REVERSING A DATA
08-04-2022
19
NIT, Jamshedpur
• To reverse a given set of data, we need to reorder the data so that the
first and last elements are exchanged, the second and second last element
are exchanged, and so on for all other elements.
• Example: Suppose we have a string Welcome, then on reversing it would
be Emoclew.
• There are different reversing applications:
• Reversing a string
• Converting Decimal to Binary
20. REVERSING A STRING
• A Stack can be used to reverse the characters of a string.
• This can be achieved by simply pushing one by one each character
onto the Stack, which later can be popped from the Stack one by one.
• Because of the last in first out property of the Stack, the first
character of the Stack is on the bottom of the Stack and the last
character of the String is on the Top of the Stack and after performing
the pop operation in the Stack, the Stack returns the String in Reverse
order.
08-04-2022
20
NIT, Jamshedpur
22. CONVERTING DECIMAL TO BINARY
08-04-2022
22
NIT, Jamshedpur
• Although decimal numbers are used in most business applications, some scientific and
technical applications require numbers in either binary, octal, or hexadecimal.
• A stack can be used to convert a number from decimal to binary/octal/hexadecimal form.
• For converting any decimal number to a binary number, we repeatedly divide the decimal
number by two and push the remainder of each division onto the Stack until the number
is reduced to 0.
• Then we pop the whole Stack and the result obtained is the binary equivalent of the
given decimal number.
• Example: Converting 14 number Decimal to Binary