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

Data Structures Experiments

The document contains code for four experiments: 1) A recursive function to solve the towers of Hanoi problem, 2) Functions to implement a stack using an array, 3) Functions to evaluate a postfix expression using a stack, 4) Functions to implement a circular queue using an array.

Uploaded by

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

Data Structures Experiments

The document contains code for four experiments: 1) A recursive function to solve the towers of Hanoi problem, 2) Functions to implement a stack using an array, 3) Functions to evaluate a postfix expression using a stack, 4) Functions to implement a circular queue using an array.

Uploaded by

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

EXPERIMENT NO: 1 OUTPUT

CODE:
#include<stdio.h>
void move(int n, char src, char dest, char
spr);

void main()
{
int n;
printf("Enter the no of disks:");
scanf("%d", &n);
move(n, 'A', 'C', 'B'); // Corrected:
Destination and spare pegs swapped
}

void move(int n, char src, char dest, char


spr)
{
if (n == 1)
{
printf("\n move from %c to %c", src,
dest);
}
else
{
move(n - 1, src, spr, dest);
move(1, src, dest, spr);
move(n - 1, spr, dest, src);
}
}
EXPERIMENT NO: 2 CODE:
#include<stdio.h> if(x!=-1)
# define MAX 3 printf("\n The value deleted from stack is
%d.\n",x);
int stack[MAX], top=-1;
break;
void push(int stack[],int x);
}
int pop(int stack[]);
case 3:
void display(int stack[]);
{
int main()
display(stack);
{
break;
int x,choice;
}
do
}
{
}
printf("Enter the choice\n");
while(choice!=4);
printf("1.PUSH\n2.POP\n3.Display\n4.EXIT return 0;
\n");
}
scanf("%d",&choice);
switch(choice)
{
case 1:
void push(int stack[],int x)
{
{
printf("Enter the number to be pushed in
if(top==MAX-1)
stack:\n");
{
scanf("%d",&x);
printf("\n Stack overflow\n");
push(stack,x);
}
break;
else
}
{
case 2:
top++;
{
stack[top]=x;
x=pop(stack);
} }
} }

int pop(int stack[]) void display(int stack[])


{ {
int x; int i;
if(top==-1) if(top==-1)
{ printf("\nStack is empty\n");
printf("\n Stack underflow\n"); else
return -1; {
} for(i=top;i>=0;i--)
else printf("\n%d",stack[i]);
{ printf("\n");
x=stack[top]; }
top--; }
return x;

OUTPUT
EXPERIMENT NO: 3 }

CODE int x=stack[top];

//write a program to implement top--;


postfix expression evaluation return x;
//567+*8- }
#include<stdio.h>
# define MAX 100 int is_operator(char symbol)
int stack[MAX], top=-1; {
if(symbol=='+'||symbol=='-
void push(int x) '||symbol=='*'||symbol=='/')

{ {

if(top>=MAX-1) return 1;

{ }

printf("Stack Overflow"); return 0;

return; }

}
top++; int evaluate(char* expression)

stack[top]=x; {

} int i=0;
char symbol=expression[i];

int pop() int operand1,operand2,result;

{ while(symbol!='\0')

if(top==-1) {

{ if(symbol>='0' && symbol<='9')

printf("Stack Underflow\n"); {

return-1; int num=symbol-'0';


push(num);
} {
else if(is_operator(symbol)) char expression[MAX];
{ printf("INPUT:");
operand2=pop(); scanf("%s", expression);
operand1=pop(); int result= evaluate(expression);
switch(symbol) printf("Result=%d\n",result);
{ return 0;
case'+':result=operand1+operand2; }
break; OUTPUT
case'-':result=operand1-operand2;
break;
case'*':result=operand1*operand2;
break;
case'/':result=operand1/operand2;
break;
}
push(result);
}
i++;
symbol=expression[i];
}
result=pop();
return result;
}

int main()
EXPERIMENT NO :4 }
else
CODE:
{
//implement a menu driven program for
circular queue data structure that if(isempty())
performs following operations:
{
//1 enqueue
front=0;
//2 dequeue
}
//3 display
rear=(rear+1)%MAX;
items[rear]=x;
#include<stdio.h>
printf("Enqueue:%d\n",x);
# define MAX 100
}
int items[MAX], front=-1, rear=-1;
}
void queue();
void enqueue(int x);
int dequeue()
int dequeue();
{
void display();
int item=0;
int isfull();
if(isempty())
int isempty();
{
printf("queue is empty, cannot
void queue() dequeue.\n");
{ }
front=-1; else
rear=-1; {
} item=items[front];
if(front==rear)
void enqueue(int x) {
{ queue();
if(isfull()) }
{ else
printf("queue is full,cannot enqueue.\n"); {
front=(front+1)%MAX; {
} return(front==(rear+1)%MAX);
} }
return item; int main()
} {
void display() queue();
{ int choice,x;
if(isempty()) do
{ {
printf("queue is empty.\n"); printf("\nEnter your
choice:\n1.Enqueue\n2.Dequeue\n3.Displ
}
ay\n4.Exit\n");
else
scanf("%d",&choice);
{
switch(choice)
printf("Queue elements: ");
{
int i = front;
case 1:
do
printf("Enter value:");
{
scanf("%d",&x);
printf("%d ",items[i]);
enqueue(x);
i=(i+1)%MAX;
break;
}
case 2:
while(i!=(rear+1)%MAX);
if(!isempty())
printf("\n");
{
}
printf("Dequeued
} element:%d\n",dequeue());

int isempty() }

{ else

return front==-1; {

} printf("queue is empty");
int isfull() }
break; OUTPUT
case 3:
display();
break;
case 4:
printf("PROGRAM EXITED.\n");
break;
default: printf("INVALID CHOICE\n");
}
}
while(choice!=4);
return 0;
}

You might also like