Data Structures (Chapter-04 Stack)
Data Structures (Chapter-04 Stack)
STRUCTURES
WITH COMPETITIVE
CODING
Presented By: Mr. Satyananda Swain
Assistant Professor, Computer Science Engineering,
❑ Fixed Size Stack: As the name suggests, a fixed size stack has a fixed size and cannot grow or
shrink dynamically. If the stack is full and an attempt is made to add an element, an overflow error
occurs. If the stack is empty and an attempt is made to remove an element, an underflow error
occurs.
❑ Dynamic Size Stack: A dynamic size stack can grow or shrink dynamically. When the stack is full, it
automatically increases its size to accommodate the new element; when the stack is empty, it
decreases. This type of stack is implemented using a linked list, as it allows for easy resizing the
stack.
❑ The basic operations that can be performed on a stack include push, pop, and peek. There are two
ways to implement a stack –
▪ Using Array
❑ In a linked list-based implementation, the push operation is implemented by creating a new node
with the new element and setting the next pointer of the current top node to the new node. The pop
operation is implemented by setting the next pointer of the current top node to the next node and
returning the value of the current top node.
▪ Here, the STACK is a Linear Array of elements following the Step 3: If (TOP=0), then:
[End of if of Step-3]
▪ TOP is a variable storing the index value of the top element
Step 4: Set ITEM: = STACK[TOP].
of the STACK.
Step 5: Set TOP: = TOP - 1.
This algorithm is used to delete the TOP element of the STACK and
Step 6: Print “Item Deleted=”, ITEM.
assign it to ITEM.
Step 7: Stop.
❑ Pick operation returns the top element of the stack. Step 1: Start.
Step 2: Let I.
❑ Algorithm: PEEP_STACK (STACK, TOP)
Step 3: If (TOP=0), then:
▪ Here, the STACK is a Linear Array of elements Step 3.1) Print “Empty Stack” and Return.
false.
is false.
so return true.
//C program to simulate the stack operations. //Function to check whether the stack is empty or not
#include<process.h> {
{ else
int top; }
};
{ printf("Stack Overflow\n");
if (s->top==STACK_SIZE-1) else
else s->top++;
} }
{ int i;
else {
item = s->items[s->top]; /* Access the top element */ for (i= s->top;i>=0;i --)
}} }}}
{ case 1:
do case 2:pop(&s);break;
{ case 3:display(&s);break;
scanf("%d",&choice); }
switch(choice)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Applications of Stack 14
▪ Recursion
▪ Undo/Redo Operations
▪ Browser History
▪ Function Calls
❑ The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is
called a recursive function.
❑ A recursive function solves a particular problem by calling a copy of itself and solving smaller subproblems of the
original problems.
❑ Properties of Recursion:
▪ Base condition is needed to stop the recursion otherwise infinite loop will occur.
❑ Types of Recursions:
Recursion are mainly of two types depending on whether a function calls itself from within itself or more than one
function call one another mutually. The first one is called direct recursion and another one is called indirect recursion.
❑ Recursion uses more memory, because the recursive function adds to the stack with each recursive call, and
keeps the values there until the call is finished. The recursive function uses LIFO (LAST IN FIRST OUT)
Structure just like the stack data structure.
❑ In the recursive program, the solution to the base case is provided and the solution to the bigger problem is
expressed in terms of smaller problems. In this example, the base case for n < = 1 is defined and the
int fact(int n) larger value of a number can be solved by converting to a
{ smaller one till the base case is reached.
if (n < = 1) // base case
return 1; Note: Why Stack Overflow error occurs in recursion?
else If the base case is not reached or not defined, then the stack
return n*fact(n-1); } overflow problem may arise.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Arithmetic Expression Evaluation 17
❑ Mathematical formulas often involve complex expressions that require a clear understanding of the
order of operations. To represent these expressions, we use different notations, each with its own
advantages and disadvantages. Three common expression notations: infix, prefix, and postfix.
▪ Infix Expressions: Infix expressions are mathematical expressions where the operator is placed
between its operands. For example, the expression “a + b”.
▪ Prefix Expressions (Polish Notation): Prefix expressions are also known as Polish notation, are a
mathematical notation where the operator precedes its operands. In prefix notation, the operator is
written first, followed by its operands. For example, the infix expression “a + b” would be written as “+
a b” in prefix notation.
▪ Postfix Expressions (Reverse Polish Notation): Postfix expressions are also known as Reverse Polish
Notation (RPN), are a mathematical notation where the operator follows its operands. In postfix
notation, operands are written first, followed by the operator. For example, the infix expression “a +
b” would be written as “a b +” in postfix notation.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Convert Infix expression to Postfix expression 18
❑ Algorithm:
INFIX_TO_POSTFIX_CONVERSION(Q,P,STACK) Step 1: START.
Step 2: Push “ ( ” onto STACK and add a “ ) ” to the end of Q.
▪ Here, Q is an expression in infix Step 3: Scan Q from Left to right and repeat steps 4 to 7 for each
element of Q until the STACK is empty:
notation. Step 4: If an operand is encountered, Add it to P.
Step 5: If a left Parenthesis “ ( ” is encountered, Push it onto STACK.
▪ P is the converted expression in postfix Step 6: If an Operator is encountered, then:
form. A) Repeatedly pop from the STACK and add to P each operator(on the
top of STACK) which has the same precedence as or higher
▪ STACK is the data structure used for precedence than the operator currently encountered.
B) Add the operator to STACK. [end of if of step -6]
conversion.
Step 7 if a right “ )” is encountered, then:
A) Repeatedly pop from STACK and add to P each operator until a
This algorithm is used to convert an
left parenthesis is encountered.
expression in infix form to its equivalent B) Remove the left parenthesis.[End of if of step-7][End of loop of
step - 3]
postfix form.
Step 8: STOP
Step 1: START.
❑ Algorithm:
Step 2: Add a “ ( ” at the beginning of P.
EVALUATION_OF_PREFIX_EXPRESSION(P,STACK)
Step 3: Scan P from right to left and repeat steps 4 and 5 for each
▪ Here, P is the expression in postfix form. element of P until the “ ( “ is encountered:
Step 4: If an operand is encountered, Put it on STACK.
▪ STACK is the data structure used in
Step 5: If an Operator is encountered, then:
evaluation.
A) Remove the top two elements of STACK.(Where A is the top element
This algorithm evaluates an expression in prefix and B is the next-to-top element.)
❑ When we invoke function A, which contains a call to function B, its processing will not
be completed until function B has completed its execution and returned. Similarly, for
functions B and C. So we observe that function A will only be completed after function
B and function B will only be completed after function C. Therefore, function A is first
to be started and last to be completed. To conclude, the above function activity
matches the last in first out behavior and can easily be handled using Stack.
❑ The given figure shows that return addresses appear in the Stack in the reverse order
in which the functions were called. After each function is completed, the pop
operation is performed, and execution continues at the address removed from the
Stack. Thus, the stack data structure can optimally handle the program that calls
several functions in succession. Control returns to each function at the correct place,
which is the reverse order of the calling sequence.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Advantages of Stack 27
▪ Simplicity: Stacks are a simple and easy-to-understand data structure, making them suitable for
various applications.
▪ Efficiency: Push and pop operations on a stack can be performed constantly (O(1)), providing efficient
access to data.
▪ Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last element added to the
stack is the first one removed. This behavior is useful in many scenarios, such as function calls and
expression evaluation.
▪ Limited memory usage: Stacks only need to store the elements that have been pushed onto them,
making them memory-efficient compared to other data structures.
▪ Limited access: Elements in a stack can only be accessed from the top, making it difficult to retrieve or
modify elements in the middle of the stack.
▪ Potential for overflow: If more elements are pushed onto a stack than it can hold, an overflow error will
occur, resulting in data loss.
▪ Not suitable for random access: Stacks do not allow for random access to elements, making them
unsuitable for applications needing access in a specific order.
▪ Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number of elements that
need to be stored is unknown or highly variable.