Beruflich Dokumente
Kultur Dokumente
Khandari Farm,Agra
DATA STRUCTURES
Practical File
Sub code:-RCA252
Output:-
#include<stdio.h>
#include<stdio.h>
void move(int, char, char, char);
int main()
{
int n;
printf("\nEnter the no of rings");
scanf("%d",&n);
move(n,'a','c','b');
getch();
return 0;
}
void move(int n,char a,char c, char b)
{
if(n==1)
printf("\nmove from %c to %c",a,c);
else
{
move(n-1,a,b,c);
move(1,a,c,b);
move(n-1,b,c,a);
}
}
Output:-
#include<stdio.h>
#include<conio.h>
int fabo(int);
int main()
{
int n,i;
printf("Enter the term");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Fabonacci(%d)=%d\n",i,fabo(i));
getch();
return 0;
}
int fabo(int num)
{
if (num<=2)
return 1;
return (fabo(num-1)+fabo(num-2));
}
Output:-
Enter the term 8
Fabonacci(0)=1
Fabonacci(1)=1
Fabonacci(2)=1
Fabonacci(3)=2
Fabonacci(4)=3
Fabonacci(5)=5
Fabonacci(6)=8
Fabonacci(7)=13
4- PROGRAM FOR
1.PUSH 2.POP 3.PEEP 4.Display 5.CLEAR STACK
#include<stdio.h>
#include<conio.h>
#define MAX 10
int st[MAX],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]);
void display(int st[]);
void clear_stack();
int main()
{
int val,a;
do
{
printf("\n\nmain menu");
printf("\n1. push");
printf("\n2. pop");
printf("\n3. peep");
printf("\n4.display");
printf("\n5. Clear stack");
printf("\nEnter your option");
scanf("%d",&a);
switch(a)
{
case 1:
printf("\n Enter the number to be pushed on to the stack");
scanf("%d",&val);
push(st,val);
break;
case 2:
val= pop(st);
printf("\n The value deleted from the stack is %d",val);
break;
case 3:
val= peep(st);
printf("\n The value stored at the top of the stack is %d",val);
break;
case 4:
display(st);
break;
case 5:
clear_stack();
break;
}
}while(a !=6);
getch();
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERRFLOW");
}
else
{
top++;
st[top]= val;
}
}
int pop(int st[])
{
int val;
if(top==-1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val=st[top];
top--;
return val;
}
}
void display (int st[])
{
int i;
if (top==-1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n%d",st[i]);
}
}
int peep(int st[])
{
if (top==-1)
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return (st[top]);
}
void clear_stack()
{
int val,i;
val=st[top];
for(i=top;i>=0;i--)
{
val=st[i];
top--;
}
Output:-
main menu
1. push
2. pop
3. peep
4.display
5. Clear stack
Enter your option2
main menu
1. push
2. pop
3. peep
4.display
5. Clear stack
Enter your option
5- PROGRAM FOR Reverse a String
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100], reverse_str[100],temp;
int i,j=0;
printf("Enter the string");
gets(str);
temp=strlen(str);
for(i=temp-1;i>=0;i--)
{
reverse_str[j]=str[i];
j++;
}
reverse_str[j]='\0';
printf("\n The reversed string is %s",reverse_str);
getch();
return 0;
}
Output:-
Enter the string Raja balwant singh
OUTPUT:-
MAIN MENU
choose an option :-
4.EXIT
7- PROGRAM FOR Print Factorial
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,b;
printf("Enter any no");
scanf("%d",&n);
b=fact(n);
printf("factorial of %d=%d",n,b);
}
int fact(a)
{
int i,c;
for(i=1;i<a;i++)
{
c=a*fact(a-1);
return (c);
}
OUTPUT:-
Enter any no6
factorial of 6=720