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

Data structure & Algo

The document provides an overview of stacks, defining them as Last In First Out (LIFO) data structures with operations such as PUSH and POP. It includes algorithms for these operations, applications of stacks in evaluating expressions and memory management, and methods for converting infix expressions to postfix. Additionally, it discusses the advantages and disadvantages of using stacks.

Uploaded by

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

Data structure & Algo

The document provides an overview of stacks, defining them as Last In First Out (LIFO) data structures with operations such as PUSH and POP. It includes algorithms for these operations, applications of stacks in evaluating expressions and memory management, and methods for converting infix expressions to postfix. Additionally, it discusses the advantages and disadvantages of using stacks.

Uploaded by

Dibyangana Bose
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NAME: DIBYANGANA BOSE

STREAM: CSE , 2nd YEAR


SECTION: A
CLASS ROLL: 09
UNIVERSITY ROLL: 10900121009
SUBJECT : DATA STRUCTURE & ALGORITHM (PCC-CS301)

TOPIC- STACKS
STACKS

➢ Definition:-
▪ A stack is an ordered list in which insertion and deletion are done at one end, called TOP.

▪ The last element inserted in a stack is the first one to be deleted. Hence, it is called
the Last In First Out (LIFO) or the First In Last Out (FILO) list.

▪ A pile of plates in a cafeteria is a good real life example of a stack. The plates are added to the top one
after another. And when required, it is taken from the top of the stack. There are other examples like
stack of books etc.
❑Operations on stack :
There are two kinds of operations that can be performed on a stack. They
are-
• PUSH
• POP

➢ PUSH : This function adds elements/ items into the stacks. Whenever
we add an element into the stack the pointer "TOP" gets incremented as -
TOP = TOP + 1 Or, TOP++

This is used to show the position of the element in the stack. When there is
no more space left in the stack, it indicates "STACK OVERFLOW".

➢ POP : This function deletes elements/ items from the stacks. Whenever
we remove an element from the stack the pointer "TOP" gets
decremented as -
TOP = TOP – 1 Or, TOP- -

This is used to show the position of the element in stack. When there is no
more element in the stack it indicates "STACK UNDERFLOW / EMPTY". Push & Pop operation
ALGORITHM FOR PUSH OPERATION

PUSH ( Stack, top, item, maxstack )


ALGORITHM FOR POP OPERATION
Step 1: If top = maxstack then
Print ("STACK OVERFLOW") and POP ( Stack, top, item )
Return
Step 1 : If top = 0 then
Step 2 : else set top = top + 1 Print ( "STACK UNDERFLOW" ) and return

Step 3 : Set stack [ top] = item Step 2 : else item = stack [ top]

Step 4 : Return / exit Step 3 : else set item = stack [top]

Step 4 : set top = top – 1

Step 5 : return / exit


PROGRAM IMPLEMENTATION OF PUSH & POP FUNCTION

1 2 3 4

#include<stdio.h> void push(int x) if(top==-1)


{
#define MAX 3 if(top==(MAX-1)) { {
int top=-1; { printf("STACK int i;
int stack[MAX]; printf("STACK UNDERFLOW");
int main() OVERFLOW"); } if(top==-1)
{ } else
push(1); else { printf("STACK UNDE
push(2); { item=stack[top]; RFLOW");
push(3); top=(top+1); top=(top-1);
print(); stack[top]=x; printf("DELETED for(i=top;i>=0;i--)
int data=pop(); printf("%d",x); ITEM%d",item); {
data=pop(); printf("\n"); printf("\n");
data=pop(); } } printf("%d",stack[i]);
data=pop(); } }
void pop() void print() printf("\n");
printf("%d",data); { }
int item; }
} int value;
❑Application of the Stack :

• A Stack can be used for evaluating expressions consisting of operands and operators.

• Stacks can be used for Backtracking, i.e., to check parenthesis matching in an expression.

• It can also be used to convert one form of expression to another form.

• It can be used for systematic Memory Management.

❑ Expression Representation:

There are three popular methods used for representation of an expression:

Infix A+B Operator between


operands.
Prefix + AB Operator before operands.
Postfix AB + Operator after operands.
➢ Conversion of Infix to Postfix :

Algorithm for Infix to Postfix

Step 1: Consider the next element in the input.

Step 2: If it is operand, display it.

Step 3: If it is opening parenthesis, insert it on stack.

Step 4: If it is an operator, then


• If stack is empty, insert operator on stack.
• If the top of stack is opening parenthesis, insert the operator on stack
• If it has higher priority than the top of stack, insert the operator on stack.
• Else, delete the operator from the stack and display it, repeat Step 4.

Step 5: If it is a closing parenthesis, delete the operator from stack and display them until an opening
parenthesis is encountered. Delete and discard the opening parenthesis.

Step 6: If there is more input, go to Step 1.

Step 7: If there is no more input, delete the remaining operators to output.


Example: Suppose we are converting 3*3/(4-1)+6*2 expression into postfix form
EXPRESSION STACK OUTPUT ( POSTFIX )
3 Empty 3
* * 3
3 * 33
/ / 33*
( /( 33*
4 /( 33*4
- /(- 33*4
1 /(- 33*41
) / 33*41-
+ + 33*41-/
6 + 33*41-/6
* +* 33*41-/6
2 +* 33*41-/62
EMPTY 33*41-/62*+

Hence the postfix expression is 33*41-/62*+


❑Advantages of Stack :
• A Stack helps to manage the data in the ‘Last in First out’ method.

• When the variable is not used outside the function in any program, the Stack can be used.

• It allows you to control and handle memory allocation and deallocation.

• It helps to automatically clean up the objects.

❑Disadvantages of Stack :
• It is difficult in Stack to create many objects as it increases the risk of the Stack overflow.

• It has very limited memory.

• In Stack, random access is not possible.


THANK YOU !

You might also like