0% found this document useful (0 votes)
73 views44 pages

C Program File

The document contains programs written in C programming language to perform various tasks related to arrays, matrices, series, searching, sorting, linked lists, stacks, queues and binary trees. It includes 14 programs with explanations and outputs to calculate the Fibonacci series, find minimum and maximum of arrays, calculate factorials, find GCD, solve quadratic equations, generate prime numbers, sort arrays, calculate trigonometric functions using series, perform matrix operations, and demonstrate linked lists, stacks, queues and binary tree traversals using recursion.

Uploaded by

Afraz Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views44 pages

C Program File

The document contains programs written in C programming language to perform various tasks related to arrays, matrices, series, searching, sorting, linked lists, stacks, queues and binary trees. It includes 14 programs with explanations and outputs to calculate the Fibonacci series, find minimum and maximum of arrays, calculate factorials, find GCD, solve quadratic equations, generate prime numbers, sort arrays, calculate trigonometric functions using series, perform matrix operations, and demonstrate linked lists, stacks, queues and binary tree traversals using recursion.

Uploaded by

Afraz Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

1

C programming
and
Data Structure

AFRAZ KHAN

AFRAZ KHAN
1616028
2

INDEX
P.NO PROGRAM NAME
1. GENERATE THE FIBONACCI SERIES AND ALSO PRINT THE NUMBER
OF ELEMENT IN SERIES
2. FINDING MINIMUM AND MAXIMUM OF N NUMBER

3. CALCULATE THE FACTORIAL OF GIVEN NUMBER

4. FINDING GCD OF TWO NUMBER

5. FINDING ALL THE ROOTS OF THE QUADRATIC EQUATION

6. GENERATE AND PRINT PRIME NUMBER UPTO N INTEGER

7. SORT GIVEN N NUMBER IN ASCENDING ORDER

8. CALCULATE THE VALUE OF SIN(X) & COS(X) USING THE SERIES.


ALSO PRINT SIN(X) & COS(X) USING THE LIBRARY FUNCTION .
(A). VALUE OF SIN(X) USING SERIES
(B). VALUE OF COS(X) USING SERIES
9. MATRICES PROGRAM

10. CALCULATE THE SUBJECT WISE & SUBJECT WISE TOTALS


& STORE THEM AS A PART OF THE STRUCTURE
11. (A). SINGLE LINKED LIST
(B). DOUBLE LINKED LIST
(C). CIRCULAR LINKED LIST
12. STACKS PROGRAM
(A). STACK IMPLEMENTTATION USING ARRAY
(B).STACK IMPLEMENTATION USING POINTERS

13. QUEUE PROGRAM


(A).QUEUE IMPLEMENTATION USING ARRAY
(B).QUEUE IMPLEMENTATION USING POINTER

14. (A). LINEAR SEARCH AND


(B).BINARY SEARCH

15. (A).SELECTION SORT


(B). INSERTION SORT

16. CREATE A BINARY TREE TO PERFORM TREE TRAVERSALS(PREORDER,


POSTORDER, INORDER) USING THE CONCEPT OF RECURSION .

AFRAZ KHAN
1616028
3

PROGRAM – 1
AIM- TO PRINT FIRST 10 ELEMENTS OF FIBONACCI SERIES
#include<stdio.h>
void main()
{
int x[10],i=1;
printf("ENTER THE FIRST TWO ELEMENTS OF SERIES : \nx[0]=");
scanf("%d",&x[0]);
printf("x[1]=");
scanf("%d",&x[1]);
printf("THE REST OF THE ELMENTS OF SERIES ARE : \n");
while (i<9)
{
x[i+1]=x[i]+x[i-1];
printf("x[%d]=%d\n",i+1,x[i+1]);
i++;

}
}

OUTPUT----
ENTER THE FIRST TWO ELEMENTS OF SERIES :
x[0]=0
x[1]=1
THE REST OF THE ELMENTS OF SERIES ARE :
x[2]=1
x[3]=2
x[4]=3
x[5]=5
x[6]=8
x[7]=13
x[8]=21
x[9]=34

PROGRAM-2
AIM - FINDING MINIMUM AND MAXIMUM OF n NUMBER
#include<stdio.h>
void main()
{
int a[10],i,max=0,min=10000;
printf("Enter the ten number:\n");
for(i=0;i<10;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
printf("\nThe max. number is =%d\n",max);
printf("The min. number is =%d\n",min);
}
OUTPUT:

AFRAZ KHAN
1616028
4

Enter the ten number:


a[0]=11
a[1]=12
a[2]=13
a[3]=14
a[4]=15
a[5]=16
a[6]=17
a[7]=18
a[8]=19
a[9]=20
The max. number is =20
The min. number is =11

PROGRAM – 3
AIM – CALCULATE FACTORIAL OF A GIVEN NUMBER
#include<stdio.h>
void main()
{
int a,i,n,fact=1;
printf("Enter the number a=");
scanf("%d",&a);
printf("\nEnter the number n=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nFactorial of given number is %d\n",fact);
}
OUTPUT:
Enter the number a=6
Enter the number n=6
Factorial of given number is 720

PROGRAM - 4
AIM - FINDING THE GCD OF TWO NUMBER
#include<stdio.h>
void main()
{
int n1,n2;
printf("Enter the number n1 & n2 respectively:\n");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
{
n1=n1-n2;
}
else
{
n2=n2-n1;
}
}
printf("\nGCD of two number is %d\n",n1);
}
OUTPUT:
Enter the number n1 & n2 respectively:
63
14
GCD of two number is 7

AFRAZ KHAN
1616028
5

PROGRAM - 5
AIM- FINDING ALL THE ROOTS OF A QUADRATIC EQUATION
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
printf("Given Quadratic Equation is ax^2+bx+c=0\n");
printf("Enter the value of a,b & c respectively\n");
scanf("%d %d %d",&a,&b,&c);

double r1,r2,D;
D=(b*b-4*a*c);
r1=((-b-sqrt(D))/(2*a));
r2=((-b+sqrt(D))/(2*a));

printf("\nr1=%f r2=%f\n",r1,r2);
if(D>0)
{
printf("Roots are real and unequal\n");
}
else if(D==0)
{
printf("Roots are real and equal\n");
}
else
{
printf("roots are real and imaginary\n");
}
}
OUTPUT:
(A).Given Quadratic Equation is ax^2+bx+c=0
Enter the value of a,b & c respectively
1 -4 4
r1=2.000000 r2=2.000000
Roots are real and equal

(B). Given Quadratic Equation is ax^2+bx+c=0


Enter the value of a,b & c respectively
1 -5 6
r1=2.000000 r2=3.000000
Roots are real and unequal

(C).Given Quadratic Equation is ax^2+bx+c=0


Enter the value of a,b & c respectively
1 2 3
r1=-1.#IND00 r2=-1.#IND00
roots are real and imaginary

PROGRAM - 6
AIM - GENERATE & PRINT PRIME NO. UPTO AN INTEGER N
#include<stdio.h>
void main()
{
int n,i,j,prime;
printf("n=");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
prime=0;

AFRAZ KHAN
1616028
6

for(j=2;j<i;j++)
{
if(i%j==0)
prime=1;
}
if(prime==0)
{
printf("%d\t",i);
}
}
printf("\n");
}
OUTPUT:
n=100
2 3 5 7 11 13 17 19 23
29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97

PROGRAM – 7
AIM – SORT GIVEN N NUMBERS IN ASCENDING ORDER
#include<stdio.h>
void main()
{
int a[5],i,j,t;
printf("Enter the array elements:\n");

for(i=0;i<5;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}

for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}

printf("\nThe sorted elements are:\n");


for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
}

OUTPUT:
Enter the array elements:
a[0]=90
a[1]=70
a[2]=20
a[3]=10
a[4]=30
The sorted elements are:
10
20

AFRAZ KHAN
1616028
7

30
70
90

PROGRAM-8
AIM – CALCULATE THE VALUE OF SIN(X) AND COS(X) USING
THE SERIES. ALSO PRINT SIN(X) AND COS(X) USING LIBRARY
FUNCTION

VALUE OF SIN(X) USING SERIES:-


#include<stdio.h>
#include<math.h>
int factorial(int y)
{
int fact=1,k;
for(k=1;k<=y;k++)
{
fact*=k;
}
return(fact);
}
void main()
{
int deg,z,n,i;
double rad,x,sum=0,m;
printf("Enter the degree=");
scanf("%d",&deg);

rad=(3.14/180)*deg;
x=rad;

printf("\nEnter the value of n=");


scanf("%d",&n);

for(i=0;i<n;i++)
{
z=factorial((2*i)+1);
sum+=(pow(-1,i)/z)*pow(x,(2*i)+1);
}
printf("\nValue of sin(%d) using sine series is %f\n",deg,sum);
m=sin(rad);
printf("\nValue of sin(%f) without using sine series is %f\n",rad,m);
}

OUTPUT:
Enter the degree=30
Enter the value of n=13

Value of sin(30) using sine series is 0.499770


Value of sin(0.523333) without using sine series is 0.499770

VALUE OF COS(X) USING SERIES


#include<stdio.h>
#include<math.h>

int factorial(int y)
{
int fact=1,k;

AFRAZ KHAN
1616028
8

for(k=1;k<=y;k++)
{
fact*=k;
}
return(fact);
}

void main()
{
int deg,z,n,i;
double rad,x,sum=0,m;
printf("Enter the degree=");
scanf("%d",&deg);

rad=(3.14/180)*deg;
x=rad;

printf("\nEnter the value of n=");


scanf("%d",&n);

for(i=0;i<n;i++)
{
z=factorial((2*i));
sum+=(pow(-1,i)/z)*pow(x,(2*i));
}
printf("\nValue of cos(%d) using sine series is %f\n",deg,sum);

m=cos(rad);
printf("\nValue of cos(%f) without using sine series is %f\n",rad,m);
}
OUTPUT:
Enter the degree=30
Enter the value of n=14

Value of cos(30) using sine series is 0.866158


Value of cos(0.523333) without using sine series is 0.866158

PROGRAM - 9
AIM- MATRICES PROGRAM
//MATRIX PROGRAM
#include<stdio.h>
void main()
{
int i,j,n,m,x,a,b,d,dgn=0;
int m1[100][100],m2[100][100],m3[100][100],m4[100][100],m5[100][100],tp[100][100];
printf("Enter the value of n=");
scanf("%d",&n);
printf("Enter the value of m=");
scanf("%d",&m);

printf("MATRIX 1:\n");
for(i=0;i<n;++i)
{
printf("Enter row number=%d",i);
for(j=0;j<m;++j)
{
scanf("%d",&m1[i][j]);
}
printf("\n");
}

AFRAZ KHAN
1616028
9

printf("\nDISPLAY THE MATRIX 1:\n");


for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
printf("%d\t",m1[i][j]);
}
printf("\n");
}
printf("\nMATRIX 2:\n");
printf("Enter value of a=");
scanf("%d",&a);
printf("Enter value of b=");
scanf("%d",&b);
for(i=0;i<a;++i)
{
printf("\nEnter row number=%d",i);
for(j=0;j<b;++j)
{
scanf("%d",&m2[i][j]);
}
printf("\n");
}
printf("\nDISPLAY THE MATRIX 2:\n");
for(i=0;i<a;++i)
{
for(j=0;j<b;++j)
{
printf("%d\t",m2[i][j]);
}
printf("\n");
}
do
{
printf("\n\n");
printf("Enter the Menu:\n");
printf("1:MATRIX ADDITION\n2:MATRIX SUBTRACTION\n3:TRANSPOSE OF
MATRIX\n4:MATRIX MULTIPLICATION\n5:SUM OF DIAGONAL ELEMENT");
printf("\nEnter the choice x=");
scanf("%d",&x);
switch(x)
{
case 1:printf("\nADDITION OF MATRIX\n");
if(n==a&&m==b)
{
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
{
m3[i][j]=m1[i][j]+m2[i][j];
printf("%d\t",m3[i][j]);
}
}
printf("\n");
}
}

else
{
printf("\n NO ADDTION WOULD TAKE PLACE:");
}
break;

AFRAZ KHAN
1616028
10

case 2:printf("\nSUBTRACTION OF MATRIX\n");


if(n==a&&m==b)
{
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
m4[i][j]=m1[i][j]-m2[i][j];
printf("%d\t",m4[i][j]);
}
printf("\n");
}
}

else
{
printf("\nNO SUBTRACTION WOULD TAKE PLACE:");
}
break;

case 3:printf("\nTRANSPOSE OF MATRIX:\n");


for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
tp[i][j]=m1[j][i];
}
printf("\n");
}

for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
printf("%d\t",tp[i][j]);
}
printf("\n");
}
break;

case 4:printf("\nMATRIX MULTIPLICATION:\n");


if(m==a)
{
d=0;
for(i=0;i<n;i++)
{
for(j=0;j<b;j++)
{
for(int c=0;c<a;++c)
{
d+=(m1[i][c]*m2[c][j]);
}
m5[i][j]=d;
d=0;
}

for(i=0;i<n;++i)
{
for(j=0;j<b;++j)
{

AFRAZ KHAN
1616028
11

printf("%d\t",m5[i][j]);
}
printf("\n");
}
}

else
{
printf("As column of 1st matrix is not equal to column of 2nd matrix,
so the multipliction would npt be exist.\n ");
}
break;

case 5:printf("\nADDITION OF DIAGONAL ELEMENTS");


if(n==m)
{
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
if(i==j)
{
dgn+=m1[i][j];
}
}
}
printf("\nsum of diagonal element is %d",dgn);
}
else
{
printf("\nAs the matrix are not square matrix so diagonal elements
will not be add:");
}
break;
default:printf("END\n");
}
}
while(x<10);
}
Enter the value of n=2
Enter the value of m=2
MATRIX 1:
Enter row number=0
1
2
Enter row number=1
3
4
DISPLAY THE MATRIX 1:
1 2
3 4

MATRIX 2:
Enter value of a=2
Enter value of b=2
Enter row number=0
2
2
Enter row number=1
3
3
DISPLAY THE MATRIX 2:

AFRAZ KHAN
1616028
12

2 2
3 3
Enter the Menu:
1:MATRIX ADDITION
2:MATRIX SUBTRACTION
3:TRANSPOSE OF MATRIX
4:MATRIX MULTIPLICATION
5:SUM OF DIAGONAL ELEMENT

Enter the choice x=1


ADDITION OF MATRIX
3 4
6 7
Enter the Menu:
1:MATRIX ADDITION
2:MATRIX SUBTRACTION
3:TRANSPOSE OF MATRIX
4:MATRIX MULTIPLICATION
5:SUM OF DIAGONAL ELEMENT

Enter the choice x=2


SUBTRACTION OF MATRIX
-1 0
0 1
Enter the Menu:
1:MATRIX ADDITION
2:MATRIX SUBTRACTION
3:TRANSPOSE OF MATRIX
4:MATRIX MULTIPLICATION
5:SUM OF DIAGONAL ELEMENT
Enter the choice x=3
TRANSPOSE OF MATRIX
1 3
2 4
Enter the Menu:
1:MATRIX ADDITION
2:MATRIX SUBTRACTION
3:TRANSPOSE OF MATRIX
4:MATRIX MULTIPLICATION
5:SUM OF DIAGONAL ELEMENT
Enter the choice x=4
MATRIX MULTIPLICATION:
8 8
18 18
Enter the Menu:
1:MATRIX ADDITION
2:MATRIX SUBTRACTION
3:TRANSPOSE OF MATRIX
4:MATRIX MULTIPLICATION
5:SUM OF DIAGONAL ELEMENT
Enter the choice x=5
ADDITION OF DIAGONAL ELEMENTS
5

PROGRAM – 10
AIM- CALCULATE THE SUBJECT WISE & STUDENT WISE
TOTALS & STORE THEM AS A PART OF THE STRUCTURE
#include<stdio.h>
struct college
{
char name[20];
int roll_no;

AFRAZ KHAN
1616028
13

int marks[5];
}student[5];
void main()
{
int i,j,sum;
float avg;
for(i=0;i<5;i++)
{
printf("Student name:");
scanf("%s",&student[i].name);
printf("Roll no: ");
scanf("%d",&student[i].roll_no);
printf("Marks obtained:\n");
sum=0;
for(j=0;j<5;j++)
{
scanf("%d",&student[j].marks[j]);
sum+=student[j].marks[j];
}
printf("\nSum=%d",sum);
avg=(sum/5);
printf("\nThe Average Marks is %f",avg);
if(avg>=90)
{
printf("\nGrade obtained is O");
}
else if(avg<=89&&avg>=70)
{
printf("\nGrade obtained is A");
}
else if(avg<=69&&avg>=50)
{
printf("\nGrade obtained is B");
}
else if(avg<=49&&avg>=33)
{
printf("\nGrade obtained is C");
}
else
{
printf("\nGrade obtained is E and student is fail");
}
printf("\n\n");
}
}
OUTPUT:
(A).Student name:Avaneesh_Awasthi
Roll no: 1616017
Marks obtained:
90
99
95
91
92
Sum=467
The Average Marks is 93.000000
Grade obtained is O

(B).Student name:pranav_pandey
Roll no: 1616018
Marks obtained:
90
65

AFRAZ KHAN
1616028
14

87
77
81
Sum=400
The Average Marks is 80.000000
Grade obtained is A

(C).Student name:Mayank_aggrawal
Roll no: 1616002
Marks obtained:
100
44
54
40
46
Sum=284
The Average Marks is 56.000000
Grade obtained is B

(D).Student name:kamal_nayan
Roll no: 1616020
Marks obtained:
45
44
43
41
42
Sum=215
The Average Marks is 43.000000
Grade obtained is C

(E). Student name:Sagar_rawat


Roll no: 1616027
Marks obtained:
99
99
95
91
93
Sum=477
The Average Marks is 95.000000
Grade obtained is O

PROGRAM-10(A)
TYPE OF LINK LISTS:-
(A):- single linked list
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int item;
struct node *next;
};
void create(node *);
void display(node *);
node *insert(node *);
node *remove(node *);
void main()
{
int choice;
node *start;

AFRAZ KHAN
1616028
15

do
{
printf("\nEnter the choice\n\nMenu:\n");
printf("\n1:Create\n2:Display\n3:Insert\n4:Delete\n5:End\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nTo create single linked list:\n");
start=(node *)malloc(sizeof(node));
create(start);
break;
case 2:printf("\nTo display single linked list:\n");
display(start);
break;
case 3: start=insert(start);
printf("The new list after inserting of data is:\n");
display(start);
break;
case 4:start=remove(start);
printf("The new list after deleting of data is\n");
display(start);
break;
}
}while(choice<=4);
printf("\nEND\n");
}
void create(node *record)
{
scanf("\n%d",&record->item);
if(record->item==0)
{
record->next=NULL;
return;
}
else
{
record->next=(node *)malloc(sizeof(node));
record=record->next;
create(record);
}
}
void display(node *record)
{
printf("\n%d",record->item);
if(record->next==NULL)
return;
else
record=record->next;
display(record);
}
node *insert(node *record)
{
node *first,*ptr;
ptr=(node*)malloc(sizeof(node));
int choice,x,y;
printf("Menu for insertion of data:\n");
printf("1:Insert at the beginning\n2:Insert anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Insert at the beginning:\n");
first=(node*)malloc(sizeof(node));

AFRAZ KHAN
1616028
16

printf("Enter the element to be inserted:\n");


scanf("%d",&first->item);
first->next=(node*)malloc(sizeof(node));
first->next=record;
break;
case 2: printf("Insert anywhere:\n");
first=record;
printf("Enter the element to be insert:\n");
scanf("%d",&y);
printf("After to be inserted\n");
scanf("%d",&x);
while(record->item!=x)
record=record->next;
ptr->item=y;
ptr->next=record->next;
record->next=ptr;
break;
}return(first);
}
node *remove(node *record)
{
node *first;
int choice,y;
printf("Menu for deletion of data:\n");
printf("1:Delete 1st node\n2:Delete anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("delete at the beginning:\n");
first=record;
record=record->next;
break;
case 2: printf("Delete anywhere:\n");
printf("after what to delete:\n");
scanf("%d",&y);
first=record;
while(record->item!=y)
record=record->next;
record->next=record->next->next;
return(first);
break;
}
return(record);
}
OUTPUT:
Enter the choice:
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
1
To create single linked list:
9
2
3
4
5
6
0

AFRAZ KHAN
1616028
17

Enter the choice


Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
2
To display single linked list:
9
2
3
4
5
6
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
3
Menu for insertion of data:
1:Insert at the beginning
2:Insert anywhere:
Enter the choice:
1
Insert at the beginning:
Enter the element to be inserted:
12
The new list after inserting of data is:
12
9
2
3
4
5
6
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
3
Menu for insertion of data:
1:Insert at the beginning
2:Insert anywhere:
Enter the choice:
2
Insert anywhere:
Enter the element to be insert:
7
After to be inserted
6
The new list after inserting of data is:

12

AFRAZ KHAN
1616028
18

9
2
3
4
5
6
7
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
4
Menu for deletion of data:
1:Delete 1st node
2:Delete anywhere:
Enter the choice:
1
delete at the beginning:
The new list after deleting of data is
9
2
3
4
5
6
7
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
4
Menu for deletion of data:
1:Delete 1st node
2:Delete anywhere:
Enter the choice:
2
Delete anywhere:
after what to delete:
6
The new list after deleting of data is
9
2
3
4
5
6
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End

AFRAZ KHAN
1616028
19

(B):-DOUBLE LINKED LIST


#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int item;
struct node *next,*prev;
};
void create(node *);
void displayforward(node *);
void displaybackward(node *);
node *insert(node *);
node *remove(node *);
void main()
{
int choice;
node *start;
do
{
printf("\nEnter the choice\n\nMenu:\n");
printf("\n1:Create\n2:Display\n3:Insert\n4:Delete\n5:End\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nTo create double linked list:\n");
start=(node *)malloc(sizeof(node));
start->prev=NULL;
create(start);
break;
case 2:printf("\nTo display double linked list:\n");
printf("Display link list from front:\n");
displayforward(start);
printf("\n\nDisplay link list from back:\n");
displaybackward(start);
break;

case 3:start=insert(start);
printf("The new list after inserting of data is:\n");
printf("\nDisplay link list from front:\n");
displayforward(start);
printf("\n\nDisplay link list from back:\n");
displaybackward(start);
break;
case 4:start=remove(start);
printf("The new list after deleting of data is\n");
printf("Display link list from front:\n");
displayforward(start);
printf("\n\nDisplay link list from back:\n");
displaybackward(start);
break;
}
}while(choice<=4);
printf("\nEND\n");
}
void create(node *record)
{
scanf("\n%d",&record->item);
if(record->item==0)
{
record->next=NULL;
return;

AFRAZ KHAN
1616028
20

}
else
{
record->next=(node *)malloc(sizeof(node));
record->next->prev=record;
record=record->next;
create(record);
}
}
void displayforward(node *record)
{
printf("\n%d",record->item);
if(record->next==NULL)
return;
else
record=record->next;
displayforward(record);
}
void displaybackward(node *record)
{
while(record->item!=0)
record=record->next;
while(record!=NULL)
{
printf("%d\n",record->item);
record=record->prev;
}
}
node *insert(node *record)
{
node *first,*ptr;
ptr=(node*)malloc(sizeof(node));
int choice,x,y;
printf("Menu for insertion of data:\n");
printf("1:Insert at the beginning\n2:Insert anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);

switch(choice)

{
case 1: printf("Insert at the beginning:\n");
first=(node*)malloc(sizeof(node));
printf("Enter the element to be inserted:\n");
scanf("%d",&first->item);
first->next=(node*)malloc(sizeof(node));
first->next=record;
first->next->prev=first;
first->prev=NULL;
break;
case 2: printf("Insert anywhere:\n");
first=record;
printf("Enter the element to be insert:\n");
scanf("%d",&y);
printf("After to be inserted\n");
scanf("%d",&x);
while(record->item!=x)
record=record->next;
ptr->item=y;
ptr->next=record->next;
record->next->prev=ptr;
record->next=ptr;

AFRAZ KHAN
1616028
21

ptr->prev=record;
break;
}return(first);
}
node *remove(node *record)
{
node *first;
int choice,y;
printf("Menu for deletion of data:\n");
printf("1:Delete 1st node\n2:Delete anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("delete at the beginning:\n");
first=record;
first->next->prev=NULL;
first=first->next;
free(record);
break;
case 2: printf("Delete anywhere:\n");
printf("after what to delete:\n");
scanf("%d",&y);
first=record;
while(record->item!=y)
record=record->next;
record->next->next->prev=record;
record->next=record->next->next;
break;
}
return(first);
}
OUTPUT:
Enter the choice
Menu:

1:Create
2:Display
3:Insert
4:Delete
5:End
1
To create double linked list:
1
3
5
7
9
0
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
2
To display double linked list:
Display link list from front:
1

AFRAZ KHAN
1616028
22

3
5
7
9
0
Display link list from back:
0
9
7
5
3
1
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
3
Menu for insertion of data:
1:Insert at the beginning
2:Insert anywhere:
Enter the choice:
1
Insert at the beginning:
Enter the element to be inserted:
12
The new list after inserting of data is:
Display link list from front:
12
1
3
5
7
9
0

Display link list from back:


0
9
7
5
3
1
12
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
3
Menu for insertion of data:
1:Insert at the beginning
2:Insert anywhere:
Enter the choice:
2
Insert anywhere:
Enter the element to be insert:
10
After to be inserted

AFRAZ KHAN
1616028
23

9
The new list after inserting of data is:
Display link list from front:
12
1
3
5
7
9
10
0

Display link list from back:


0
10
9
7
5
3
1
12
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
4
Menu for deletion of data:
1:Delete 1st node
2:Delete anywhere:
Enter the choice:
1
delete at the beginning:
The new list after deleting of data is
Display link list from front:

1
3
5
7
9
10
0
Display link list from back:
0
10
9
7
5
3
1
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
4
Menu for deletion of data:
1:Delete 1st node

AFRAZ KHAN
1616028
24

2:Delete anywhere:
Enter the choice: 2
Delete anywhere:
after what to delete:
9
The new list after deleting of data is
Display link list from front:
1
3
5
7
9
0

Display link list from back:


0
9
7
5
3
1
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End

(C):- SINGLE CIRCULAR LINKED LIST


#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int item;
struct node *next,*prev;
};
void create(node *);
void display(node *);
node *insert(node *);
node *remove(node *);
void display_anywhere(node*record);
void main()
{
int choice;
node *start;
do
{
printf("\nEnter the choice\n\nMenu:\n");
printf("\n1:Create\n2:Display\n3:Insert\n4:Delete\n5:End\n");
printf("\nEnter teh choice=");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nTo create circular linked list:\n");
start=(node *)malloc(sizeof(node));
create(start);
break;
case 2:printf("\nTo display single linked list:\n");
display(start);
break;

AFRAZ KHAN
1616028
25

case 3: start=insert(start);
printf("The new list after inserting of data is:\n");
display(start);
break;
case 4:start=remove(start);
printf("The new list after deleting of data is\n");
display(start);
break;
}
}while(choice<=4);

printf("\nEND\n");
}

void create(node *record)


{
int n;
node *first,*temp;
first=(node*)malloc(sizeof(node));
first=record;
printf("Enter the number of elements in a circular linked list:");
scanf("%d",&n);

while(n!=0)
{
record->next=(node*)malloc(sizeof(node));
scanf("%d",&record->item);
temp=record;
record=record->next;
n--;
}
temp->next=first;
}
void display(node *record)
{
node *first;
first=(node*)malloc(sizeof(node));
first=record;

do
{
printf("%d\n",record->item);
record=record->next;
}while(record!=first);

void display_anywhere(node*record)
{
int y;
printf("Display the list form the element entered");
scanf("%d",&y);

while(record->item!=y)
record=record->next;

do
{
printf("%d",record->item);
record=record->next;
}
while(record->item!=y);

AFRAZ KHAN
1616028
26

node *insert(node *record)


{
node *first,*ptr,*newnode;
newnode=(node*)malloc(sizeof(node));
ptr=(node*)malloc(sizeof(node));
int choice,x,y;
printf("Menu for insertion of data:\n");
printf("1:Insert at the beginning\n2:Insert anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Insert at the beginning:\n");
printf("Enter the element to be insert\n:");
scanf("%d",&newnode->item);
first=newnode;
while(record->next!=first)
record=record->next;
newnode->next=(node*)malloc(sizeof(node));
newnode->next=record->next;
record->next=newnode;
return(newnode);
break;
case 2: printf("Insert anywhere:\n");
first=record;
printf("Enter the element to be insert:\n");
scanf("%d",&y);
printf("After to be inserted\n");
scanf("%d",&x);
while(record->item!=x)
record=record->next;
ptr->item=y;
ptr->next=record->next;
record->next=ptr;
return(first);
break;
}
}
node *remove(node *record)
{
node *first;
first=(node*)malloc(sizeof(node));
int choice,y;

printf("Menu for deletion of data:\n");


printf("1:Delete 1st node\n2:Delete anywhere:\n");
printf("Enter the choice:\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("delete at the beginning:\n");
first=record;
while(record->next!=first)
record=record->next;
record->next=first->next;
free(first);
return(record->next);
break;
case 2: printf("Delete anywhere:\n");
printf("after what to delete:\n");
scanf("%d",&y);

AFRAZ KHAN
1616028
27

first=record;
while(record->item!=y)
record=record->next;
record->next=record->next->next;
return(first);
break;
case 3: printf("Delete at the end:\n");
first=record;
while(record->next->next!=first)
record=record->next;
record->next=first;
return(first);
break;
}
return(record);
}

OUTPUT:
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=1
To create circular linked list:
Enter the number of elements in a circular linked list:6
1
2
3
4
5
6
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=2
To display circular linked list:
1
2
3
4
5
6
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=3
Menu for insertion of data:
1:Insert at the beginning
2:Insert anywhere:
Enter the choice:
2

AFRAZ KHAN
1616028
28

Insert anywhere:
Enter the element to be insert:
10
After to be inserted
4
The new list after inserting of data is:
1
2
3
4
10
5
6
Enter the choice
Menu:
1:Create
2:Display
3:Insert
4:Delete
5:End
Enter the choice=
4
Menu for deletion of data:
1:Delete 1st node
2:Delete anywhere:
Enter the choice:
2
Delete anywhere:
after what to delete:
3
The new list after deleting of data is
1
2
3
10
5
6
PROGRAM-12

(A):- STACK IMPLMETATION USING ARRAY


#include<stdio.h>
#include<stdlib.h>
#define size 4
int stack[size],top=-1;
void push(int);
void display();
int pop();
void main()
{
int num,choice;
do
{
printf("\nMenu\n1:Push\n2:Pop\n3:Display\n\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be pushed:");
scanf("%d",&num);
push(num);
break;
case 2:num=pop();

AFRAZ KHAN
1616028
29

printf("\nElement popped is %d\n",num);


break;
case 3:printf("\nDispaly the Element of stacks\n");
display();
break;
default:printf("Invalid choice\n\n");
break;
}
}while(choice<=3);
}
void push(int x)
{
if(top==size-1)
printf("stack is full\n");
else

top++;

stack[top]=x;
}
int pop()
{
int m;
if(top==-1)
printf("stack is empty\n");
else
m=stack[top];
top--;
return m;
}
void display()
{
int n;
n=top;
while(n!=-1)
{
printf("%d\n",stack[n]);
n--;
};
}

OUTPUT:
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:21
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:22
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:23

AFRAZ KHAN
1616028
30

Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:24
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
24
23
22
21
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 24
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 23
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
22
21
Menu
1:Push
2:Pop
3:Display
Enter your choice:

(B):- STACK IMPLMETATION USING POINTER


#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int data;
struct node *link;
}*stack;
void push(int);
void display();
int pop();
void main()
{
int num,choice;
stack=NULL;

AFRAZ KHAN
1616028
31

do
{
printf("\nMenu\n1:Push\n2:Pop\n3:Display\n\n");
printf("Enter your choice:\n");
scanf("%d",&choice);

switch(choice)
{
case 1:printf("\nEnter the element to be pushed:");
scanf("%d",&num);
push(num);
break;
case 2:num=pop();
printf("\nElement popped is %d\n",num);
break;
case 3:printf("\nDispaly the Element of stacks\n");
display();
break;
default:printf("INVLAID CHOICE\n\n");
break;
}
}while(choice<=3);
}
void push(int x)
{
node *ptr;
ptr=(node *)malloc(sizeof(node));
ptr->data=x;
ptr->link=stack;
stack=ptr;
}
int pop()
{
node *temp=NULL;
if(stack==NULL)
printf("STACK IS EMPTY\n");
else
{
int i=stack->data;
temp=stack;
stack=stack->link;
free(temp);
return(i);
}
}
void display()
{
node *temp=stack;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
};
}
OUTPUT:
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:12

AFRAZ KHAN
1616028
32

Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:13
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:14
Menu
1:Push
2:Pop
3:Display
Enter your choice:
1
Enter the element to be pushed:15
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
15
14
13
12
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 15
Menu
1:Push
2:Pop
3:Display
Enter your choice:
2
Element popped is 14
Menu
1:Push
2:Pop
3:Display
Enter your choice:
3
Dispaly the Element of stacks
13
12
Menu
1:Push
2:Pop
3:Display
Enter your choice:

PROGRAM-13

AFRAZ KHAN
1616028
33

(A): QUEUE IMPLMETATION USING ARRAY


#include<stdio.h>
#include<stdlib.h>
#define size 4
int queue[size],rear =-1,front=-1;
void push(int);
void display();
int pop();
void main()
{
int num,choice;

do
{
printf("\nMenu\n1:INSERT AT REAR\n2:DELETE AT FRONT\n3:Display\n\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be pushed:");
scanf("%d",&num);
push(num);
break;
case 2:num=pop();
printf("\nElement popped is %d\n",num);
break;
case 3:printf("\nDispaly the Element of Queue\n");
display();
break;
default:printf("INVLAID CHOICE\n\n");
break;
}
}while(choice<=3);
}
void push(int x)
{
if(rear==size-1)
printf("QUEUE IS FULL\n");
else
{
rear++;
queue[rear]=x;
}
if(front==-1)
front=0;
}
int pop()
{
int m;
if(front==-1)
printf("queue is empty\n");
else
m=queue[front];
queue[front]=0;
if(front==rear)
front=rear=-1;
else
front++;
return m;
}
void display()

AFRAZ KHAN
1616028
34

{
int i;
if(front==rear==1)
printf("QUEUE IS EMPTY\n");
else
{
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
}
OUTPUT:
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
1
Enter the element to be pushed:10
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
1
Enter the element to be pushed:20
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
1
Enter the element to be pushed:30
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display

Enter your choice:


1
Enter the element to be pushed:40
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
3
Dispaly the Element of Queue
10
10
30
40
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
2
Element popped is 10
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display

AFRAZ KHAN
1616028
35

Enter your choice:


2
Element popped is 20
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:
3
Dispaly the Element of Queue
30
40
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:Display
Enter your choice:

(B): QUEUE IMPLMETATION USING POINTER


#include<stdio.h>
#include<stdlib.h>
#define size 4
#define NULL 0
struct node
{
int data;
node *next;
}*rear,*front;
void push(int);
void display();
int pop();
void main()
{
int num,choice;
rear=(node *)malloc(sizeof(node));
rear=NULL;
front=(node *)malloc(sizeof(node));
front=NULL;
do
{
printf("\nMenu\n1:INSERT AT REAR\n2:DELETE AT FRONT\n3:DISPLAY\n\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be pushed:");
scanf("%d",&num);
push(num);
break;
case 2:num=pop();
printf("\nElement popped is %d\n",num);
break;
case 3:printf("\nDispaly the Element of Queue:\n");
display();
break;
default:printf("INVLAID CHOICE\n\n");
break;
}
}while(choice<=3);
}
void push(int x)
{
node *temp;

AFRAZ KHAN
1616028
36

temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->next=NULL;
if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=rear->next;
}
}
int pop()
{
int m;
node *temp;
temp=front;
if(rear==NULL)
printf("QUEUE IS EMPTY\n");
else
{
m=temp->data;
temp=temp->next;
free(front);
front=temp;
}
return m;
}
void display()
{
node *temp;
temp=front;
if(rear==NULL)
printf("\nQueue is empty\n");
else
{
while(temp!=NULL)

{
printf("\n%d\n",temp->data);
temp=temp->next;
};
}
}
OUTPUT:
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
1
Enter the element to be pushed:10
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
1
Enter the element to be pushed:20
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY

AFRAZ KHAN
1616028
37

Enter your choice:


1
Enter the element to be pushed:30
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY

Enter your choice:


1
Enter the element to be pushed:40
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
3
Dispaly the Element of Queue:
10
20
30
40
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
2
Element popped is 10
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
2
Element popped is 20
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
3
Dispaly the Element of Queue:
30
40
Menu
1:INSERT AT REAR
2:DELETE AT FRONT
3:DISPLAY
Enter your choice:
PROGRAM_14

(A):- LINEAR SEARCH


#include<stdio.h>
void main()
{
int a[10],i,n,num;
int ch;
printf("Enter the no. of elements in an array:");
scanf("%d",&n);
printf("\nenter the elements:\n");
for(i=0;i<n;i++)

AFRAZ KHAN
1616028
38

{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
do
{
printf("\nEnter the element to the searched:");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
printf("The element is found at position %d",i+1);
break;
}
}
if(i==n)
printf("\nThe element not found");
printf("\n\nType 1 to continue and 0 to exit:");
scanf("%d",&ch);
}while(ch==1);
}
OUTPUT:
Enter the no. of elements in an array:5
enter the elements:
a[0]=51
a[1]=61
a[2]=71
a[3]=81
a[4]=91
Enter the element to the searched:61
The element is found at position 2
Type 1 to continue and 0 to exit:1
Enter the element to the searched:91
The element is found at position 5

Type 1 to continue and 0 to exit:1


Enter the element to the searched:71
The element is found at position 3
Type 1 to continue and 0 to exit:1
Enter the element to the searched:11
The element not found
Type 1 to continue and 0 to exit:

(B):- BINARY SEARCH


#include<stdio.h>
void main()
{
int a[10],start,end,mid,i,n,num,ch;
printf("Enter the no. of elements in an array:”);
scanf("%d",&n);
printf("\nenter the elements:\n");

for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
do
{
printf("\nEnter the element to the searched:");
scanf("%d",&num);

AFRAZ KHAN
1616028
39

start=0;
end=n-1;
mid=(start+end)/2;
while(num!=a[mid]&&start<=end)
{
if(num>a[mid])
start=mid+1;
else
end=mid-1;
mid=(start+end)/2;
}
if(num==a[mid])
printf("\nThe element %d is found at position %d",num,mid+1);
if(start>end)
printf("\nThe element %d is not found",num);
printf("\n\nType 1 to continue and 0 to exit:");
scanf("%d",&ch);
}while(ch==1);
}
OUTPUT:
Enter the no. of elements in an array:5
enter the elements:
a[0]=11
a[1]=12
a[2]=13
a[3]=14
a[4]=15
Enter the element to the searched:11
The element 11 is found at position 1
Type 1 to continue and 0 to exit:1
Enter the element to the searched:12
The element 12 is found at position 2
Type 1 to continue and 0 to exit:1
Enter the element to the searched:15
The element 15 is found at position 5
Type 1 to continue and 0 to exit

PROGRAM- 15
AIM- TO SORT NUMBERS IN ASCENDING ORDER

(A).TO SORT NUMBERS USING SELECTION SORT METHOD


#include<stdio.h>
void main()
{
int a[10],i,j,n,minpos,temp;
printf("Enter the elements to be sorted:");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{ minpos=i;
for(j=i+1;j<n;j++)
{

AFRAZ KHAN
1616028
40

if(a[j]<a[minpos])
minpos=j;
}
if(minpos!=i)
{
temp=a[i];
a[i]=a[minpos];
a[minpos]=temp;
}
}
printf("array after %d iteration",i);
for(int k=0;k<n;k++)
printf("%d\t",a[k]);
printf("\n");

}
printf("The elements of sorted array are:\n");
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
}
OUTPUT:
Enter the elements to be sorted:5
Enter the elements:
a[0]=50
a[1]=40
a[2]=10
a[3]=90
a[4]=30
array after 0 iteration10 40 50 90 30
array after 1 iteration10 30 50 90 40
array after 2 iteration10 30 40 90 50
array after 3 iteration10 30 40 50 90
The elements of sorted array are:
a[0]=10
a[1]=30
a[2]=40
a[3]=50
a[4]=90

(B). TO SORT NUMBER USING INSERTION SORTING


METHOD
#include<stdio.h>
void main()
{
int a[10],i,j,n,temp;
printf("Enter the elements to be sorted:");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)

AFRAZ KHAN
1616028
41

{
if(temp<a[j])
a[j+1]=a[j];
else
break;
}
a[j+1]=temp;
printf("array after %d iteration",i);
for(int k=0;k<n;k++)
printf("%d\t",a[k]);
printf("\n");
}
printf("The elements of sorted array are:\n");
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
}
OUTPUT:
Enter the elements to be sorted:5
Enter the elements:
a[0]=25
a[1]=14
a[2]=26
a[3]=23
a[4]=24
array after 0 iteration25 14 26 23 24
array after 1 iteration14 25 26 23 24
array after 2 iteration14 25 26 23 24
array after 3 iteration14 23 25 26 24
array after 4 iteration14 23 24 25 26
The elements of sorted array are:
a[0]=14
a[1]=23
a[2]=24
a[3]=25
a[4]=26

PROGRAM- 16
AIM- TO CREATE A BINARY SEARCH TREE
#include<stdio.h>
#include<stdlib.h>
typedef struct BST {
int data;
struct BST *Ichild , *rchild;
}
node;
void insert(node *,node *);
void inorder(node *) ;
void preorder(node *) ;
void postorder(node *) ;
node *search(node *,int, node **);
void main()
{
int choice , ans , key;
node *new_node,*root,*tmp,*parent;
node *get_node();
root = NULL;
printf("\n Program For Binary Search Tree");

AFRAZ KHAN
1616028
42

do{
printf("\n1.CREATE \n2.SEARCH \n3.RECURSIVE \n4.EXIT ");
printf("\n Enter your choice : " );
scanf("%d",&choice);
switch(choice)
{
case 1:
do{
new_node=get_node();
printf("\nEnter the Element ");
scanf("%d",&new_node->data);
if(root==NULL) /* tree is not Created */
root=new_node*
else
insert(root,new_node);
printf("\n Want to enter More Elements? press 1 for yes and 0 for no");
scanf("%d",&ans);
}while(ans==1);
break;
case 2 :
printf("\n Enter element to be searched : ");
scanf("%d",&key);
tmp=search(root,key,&parent);
printf("\n Parent of node %d is %d",tmp->data,parent->data);
break;

case 3:
if(root==NULL)
printf("Tree is not created");
else
{
printf("\n The inorder display : ");
inorder(root);
printf("\n The preorder display : ");
preorder(root);
printf("\n The postorder display : ");
postorder(root);
}
break;
}
}while(choice!=4);
}
//GET NEW NODE

node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->Ichild=NULL;
temp->rchild=NULL;
return temp;
}
//THIS FUNCTION IS FOR CREATING A BINARY SEACH TREE

void insert(node *rootx,node *new_nodex)


{
if(new_nodex->data<rootx->data)
{

AFRAZ KHAN
1616028
43

if(rootx->Ichild==NULL)
rootx->Ichild=new_nodex;
else
insert(rootx->Ichild,new_nodex);
}
if(new_nodex->data>rootx->data)
{
if(rootx->rchild==NULL)
rootx->rchild=new_nodex;
else
insert(rootx->rchild,new_nodex);
}
}
node *search(node *rootx,int key,node **parentx)
{
node *temp;
temp=rootx;
while(temp!=NULL)
{
if(temp->data==key)

{
printf("\nThe %d Element is Present",temp->data);
return temp;
}
*parentx=temp;
if(temp->data>key)
temp=temp->Ichild;
else
temp=temp->rchild;
}
return NULL;
}
//THIS FUNCTION DISPLAYS THE TREE IN INORDER FASHION

void inorder(node *temp)


{
if(temp!=NULL){
inorder(temp->Ichild);
printf("%d",temp->data);
inorder(temp->rchild);
}
}
//THIS FUNCTION DISPLAYS THE TREE IN PREORDER FASHION

void preorder(node *temp)


{
if(temp!=NULL){
printf("%d",temp->data);
preorder(temp->Ichild);
preorder(temp->rchild);
}
}

//THIS FUNCTION DISPLAYS THE TREE IN POSTORDER FASHION

void postorder(node *temp)


{

AFRAZ KHAN
1616028
44

if(temp!=NULL){

postorder(temp->Ichild);
postorder(temp->rchild);
printf("%d",temp->data);
}
}

output :
Program For Binary Search Tree
1.CREATE
2.SEARCH
3.RECURSIVE
4.EXIT
Enter your choice : 1

Enter the Element 8

Want to enter More Elements? press 1 for yes and 0 for no1

Enter the Element 9

Want to enter More Elements? press 1 for yes and 0 for no1

Enter the Element 7

Want to enter More Elements? press 1 for yes and 0 for no1

Enter the Element 32

Want to enter More Elements? press 1 for yes and 0 for no0

1.CREATE
2.SEARCH
3.RECURSIVE
4.EXIT
Enter your choice : 2

Enter element to be searched : 7


The 7 Element is Present
Parent of node 7 is 8
1.CREATE
2.SEARCH
3.RECURSIVE
4.EXIT

Enter your choice : 3


The inorder display : 78932
The preorder display : 87932
The postorder display : 73298
1.CREATE
2.SEARCH
3.RECURSIVE
4.EXIT
Enter your choice :
4

AFRAZ KHAN
1616028

You might also like