Experiment 3 DS Student
Experiment 3 DS Student
Experiment No – 3
AIM: Classification of Data Structure and Implementation of Stack and Application of Stack in
Conversion of Infix Notation to Postfix Notation, and Implementation of Tower of Hanoi.
3.1 Implement a program for stack that performs following operations using array. (a) PUSH (b)
POP (c) PEEP (d) CHANGE (e) DISPLAY
3.2 Implement a program to convert infix notation to postfix notation using stack.
3.3 Write a program to implement Tower of Hanoi problem.
3.4 Identify widely used application which use stack data structure for implementation of its
important feature.
Date:
Theory:
Stack
A stack is a data structure that follows the last-in first-out (LIFO) principle, meaning that objects
are inserted and removed from the container in a particular order. In pushdown stacks, only two
operations are allowed: pushing an item onto the stack, and popping an item off the top of the stack.
Access to the stack is limited, as elements can only be added and removed from the top. When an
item is pushed onto the stack, it becomes the new top item. Conversely, when an item is popped off
the stack, it is removed from the top.
To illustrate this concept, consider a stack of books. Just as you can only remove the top book, you
can only add a new book to the top of the stack. A stack can also have a limited capacity. If the
stack is already full and there is no space to add a new item, it is said to be in an overflow state. On
the other hand, if the stack is empty and an item is removed, it is in an underflow state, meaning
that no items are present in the stack to be removed.
A stack is an abstract data structure that operates on the LIFO principle, where the last item added
Page No
Data Structure (3130702)
is the first item to be removed. Items can be inserted and deleted at one end called the top, creating
a structure that resembles a closed tube on one side.
(1) PUSH
(2) POP
3.1 Implement a program for stack that performs following operations using array.
(a) PUSH (b) POP (c) PEEP (d) CHANGE (e) DISPLAY
Program:
#include<stdio.h>
#define size 5
struct stack
{
int a[size],top;
int temp[size];
}s;
// PUSH Operation
void push()
{
int value;
printf(" Enter value to be pushed: ");
scanf("%d", &value);
s.top = s.top + 1;
Page No
Data Structure (3130702)
s.a[s.top] = value;
}
// POP Operation
void pop()
{
printf(" Popped element is %d\n", s.a[s.top]);
s.top = s.top - 1;
}
// PEEP Operation
void peep()
{
printf(" The value at top position is : %d\n", s.a[s.top]);
}
// DISPLAY Operation
void display()
{
int i;
printf(" The stack contains: ");
for(i=s.top; i>=0; i--)
{
printf("\t%d", s.a[i]);
}
printf("\n");
}
// CHANGE Operation
void change(int index, int new_element)
{
int i, j=-1;
for(i=s.top; i>index; i--)
{
s.temp[++j] = s.a[s.top--];
}
s.a[s.top] = new_element;
for(i = j; i>-1; i--)
{
s.a[++s.top] = s.temp[j--];
}
}
void main()
{
s.top = -1;
int choice, index, new_element;
do
{
printf("\n STACK IMPLEMENTATION PROGRAM");
printf("\n 1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT\n");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
if(s.top == size-1)
{
printf("\tSTACK OVERFLOW\n");
}
else
{
push(); Page No
Data Structure (3130702)
}
break;
case 2:
if(s.top == -1)
{
printf("\tSTACK UNDERFLOW\n");
}
else
{
pop();
}
break;
case 3:
if(s.top == -1)
{
printf("\tStack is empty.\n");
}
else
{
peep();
}
break;
case 4:
printf(" Enter index no : ");
scanf("%d",&index);
if(index<0 || index>s.top)
{
printf("\tINVALID INDEX NUMBER\n");
}
else
{
}
break;
case 5:
printf(" Enter new element: ");
scanf("%d", &new_element);
change(index, new_element);
if(s.top == -1)
{
printf("\t Stack is empty.\n");
}
else
{
}
break;
case 0:
display();
printf("\tEND OF PROGRAM");
break;
default :
printf("\tINVALID CHOICE\n");
}
} while(choice != 0);
}
Page No
Data Structure (3130702)
Output:
Page No
Data Structure (3130702)
3.2 Implement a program to convert infix notation to postfix notation using stack.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
result[resultIndex] = '\0';
printf("Postfix notation of: %s\n is given by : %s", s, result);
}
// Driver code
int main() {
char exp[] = "a+b*(c^d-e)";
// Function call
infixToPostfix(exp);
return 0;
}
Output:
Program:
#include <stdio.h>
int main()
{
int n = 4; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
Output:
3.4 Identify widely used application which use stack data structure for implementation of its
important feature.
Stack Applications:
1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into postfix/prefix form.
4. In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
5. During a function call the return address and arguments are pushed onto a stack and on
return they are popped off.
6. Depth first search uses a stack data structure to find an element from a graph.
Conclusion:
Page No
Data Structure (3130702)
Quiz:
Page No
Data Structure (3130702)
Suggested Reference:
Marks
Page No