Data Structure Using C Programs
Data Structure Using C Programs
#include<stdio.h>
#include<conio.h>
OUTPUT
Enter two numbers
4
8
GCD of 4 and 8 is:4
void main()
{
int rows,c=1,blk,i,j;
clrscr();
printf("Enter the number of rows:");
scanf("%d",&rows);
for(i=0;i<rows;i++)
{
for(blk=1;b1k<=rows-i;b1k++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j..elli..0)
c=1;
else
c=c*(i-j+1)/j;
printf("%4d",c);
}
printf("\n");
}
}
OUTPUT
Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
/*3.C program to generate N Fibonacci numbers using recursive
function*/
#include <stdio.h>
#include<conio.h>
int fibo(int n)
{
if(n==0)
return 0;
else
if(n==1)
return 1;
else
return(fibo(n-1)+fibo(n-2));
}
void main( )
{
int i,n;
clrscr();
printf("\n Enter the number of terms :");
scanf("%d",&n);
printf("\n Fibonacci series \n");
for(i=1;i<=n;i++)
printf("%d \n",fibo(i));
}
OUTPUT
Enter the number of terms :10
Fibonacci series
1
1
2
3
5
8
13
21
34
55
/* 4. C program to implement Tower of Hanoi */
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,big,small;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
big=small-a[0];
for(i=1;i<n;i++)
{
if(a[i]>big)
big=a[i];
if(a[i]<small)
small=a[i];
}
printf("Largest element=%d\n",big);
printf("Smallest element=%d\n",small);
}
OUTPUT
Enter the size of the array:6
Enter 6 elements
12 45
78 10
54 5
Largest element=78
Smallest element=5
/* 6.C program to arrange the city names alphabetically */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j;
char str[20][20],s[20];
clrscr();
printf("\n Enter the Number of Cities:\n");
scanf("%d",&n);
printf("Enter %d City Names\n",n);
for(i=0;i<n;i++)
scanf("%s",str[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j]>0)
{
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("City Name in Alphabetical Order:\n");
for(i=0;i<n;i++)
printf("%s\n",str[i]);
}
OUTPUT
Enter the Number of Cities:6
Enter 6 City Names
Kalaburagi
Raichur
Bidar
Yadgir
Hubballi
City Name in Alphabetical Order:
Bidar
Hubballi
Kalaburagi
Raichur
Yadgir
/* 7.0 program to sort the given list using selection sort
method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[15],i,j,k,n,pass,min; clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
/*selection sort begins*/
for(pass=0;pass<n-1;pass++)
{
min=pass;
for(j=pass+1;j<n;j++)
{
if(a[min]>a[j]) min=j;
}
if(min!=pass)
{
k=a[pass];
a[pass]=a[min];
a[min]=k;
}
}
printf("\n Sorted elements of array are:\n"); for(i=0;i<n;i+
+)
printf("%d\t",a[i]);
}
OUTPUT
Enter the size of the array:6
Enter 6 elements
35 25 15 78 65 54
Array elements are:
35 25 15 78 65 54
Sorted elements of array are:
15 25 35 54 65 78
/* 8.0 program to sort the given list using bubble sort
method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[15],n,i,j,t;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
OUTPUT
Enter the size of the array: 6
Enter 6 elements
78 25 67 56 45 16
#include <stdio.h>
#include<conio.h>
Enter 6 elements
58 26 16 37 78 45
void main()
{
int a[10],i,item,n,f=0,1oc;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the item to be searched:");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==a[i])
{
f=1;
loc=i+1;
break;
}
}
if(f==1)
printf("Successful, item found in %d location \n",loc);
else
printf("Unsuccessful, item not found in the list \n");
}
OUPUT
Enter the size of array: 6
Enter 6 elements
15 78 25 64 45 56
Enter the item to be searched: 78
Successful, item found in 2 location.
/*12. C program to search an element using binary search
method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t,item,n,beg,mid,end;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/* sorting the elements */
for(i=0;i<n-1;i++)
{
for(j=i+1;1<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\n Sorted elements of array are:\
n"); for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n Enter the item to be
searched:"); scanf("%d",&item);
/* binary search beging */
beg=1;
end=n;
do
{
mid=(beg+end)/2;
if(item>a[mid])
beg=mid+1;
if(item<a[mid])
end=mid-1;
}
while(item!=a[mid] && beg<=end);
if(item==a[mid])
printf("Search is successful \n");
else
printf("Search is unsucessful,item not found in the list \
n");
OUPUT
Enter the size of array: 6
Enter 6 elements
45 35 16 26
78 65
Sorted elements of array are:
16 26 35 45 65 78
Enter the item to be searched: 45
Search is successful
Enter the item to be searched: 85
Search is unsuccessful
/*13. C program to implement Stack */
#include<stdio.h>
#include<conio.h>
#define size 4
OUTPUT
1.Push 2.Pop 3 Display 1.Push 2.Pop 3 Display
Enter your choice: 1
Enter your choice: 2
Enter the item to be pushed:
Popped item is: 56 -
34
Press 0 For NO and 1 for YES: Press 0 For NO and 1 for YES:
l l
1.Push 2.Pop 3 Display
1.Push 2.Pop 3 Display
Enter your choices: 3
Enter your choice: 1
45 34
Enter the item to be pushed:
45 Press 0 For NO and 1 for YES: 0
Press 0 For NO and 1 for YES:
l
Enter your choice: 1
Enter the item to be pushed: 56
Press 0 For NO and 1 for YES: l
1.Push 2.Pop 3 Display
/*14. C program to convert an infix expression to postfix*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch,stack[50],infix[50];
char ans='y’;
ant 151, top=0;
clrscr();
while(ans=="y'|| ans=="Y')
{
Stack[0]="(";
printf("\n Enter An Infix Expression:");
gets(infix);
fflush(stdin);
j=strlen(infix);
printf("\n The Postfix Expression is:\n");
for(i=0;i<j;i++)
{
ch=infix[i];
if((ch>='0"&& ch<="9")||(ch>="a'&& ch<="z")||(ch>='A"&& ch>="Z"))
printf(“\n %c",ch);
if(ch==’(‘)
stack[++top]=ch;
if(ch=='*’||ch==’/’)
{
while(stack{topl=="*"||stack[top]=='/’)
printf("\n %c",stack[top--]);
stack[++top]=ch;
}
if(ch==’+’||ch==’-’)
{
while(stack[top]==’+’||stack[top]=='-’)
{
printf(“\n %c” ;stack[top]);
top--;
}
stack[++top]=ch;
}
if(ch==’)’)
{
while(stack[top]!=’(‘)
printf ("%c",stack[top--]);
top--;
}
}
while(stack[top]!=’(’)
printf("%c",stack[top--]);
printf("\n Do you want to continue(y/n)?:");
scanf("%c",&ans);
fflush(stdin);
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#define size 5
void ginsert(int);
void qdelete();
void display();
int Queue[size],front=-1,rear=-1;
void main()
{
clEserl
ginsert(10);
ginsert(20);
ginsert(30);
ginsert(40);
ginsert(50);
display();
qdelete();
display();
}
void qdelete()
{
if(front==-1)
printf ("\n Queue Underflow");
else
{
printf("Deleted Element:%d",Queue[front]);
front++;
if(front>rear)
front=rear=-1;
}
}
void display()
{
int i;
if(rear==-1)
printf("\n Queue is Empty");
else
{
printf("\n Queue elements are:\n");
for(i=front;i<=rear;i++)
printf ("%d\t",Queue[i]);
}
printf(“\n”);
}
OUTPUT
Inserted ->10
Inserted ->20
Inserted ->30
Inserted ->40
Inserted ->50
struct list
{
int info;
struct list “next;
};
typedef struct list node;
node *header;
OUTPUT