Record Ra3 PDF
Record Ra3 PDF
Program No:1
LINEAR SEARCH
AIM
Write a program to search an element using Linear Search.
ALGORITHM
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
01: START
02: ACCEPT NUMBER AS N, INITIALIZE C TO 0
03: ASSIGN I=0
04: REPEAT STEP 5 TO STEP 6 UNTIL I<N
05: ACCEPT ELEMENTS A[I] //A REPRESENTS INTEGER ARRAY
06: I=I+1
07: ACCEPT ELEMENT TO SEARCH S
08: I=0
09: REPEAT STEP 10 TO STEP 11 UNTIL I<N
10: IF( S==A[I])
INCREMENT C BY 1
STEP 11: INCREMENT I BY 1
STEP 12: IF (C==0)
PRINT ITEM NOT FOUND
ELSE
PRINT ITEM FOUND
STEP 13: STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],s,i,n,c,ch;
clrscr();
while(1)
{
c=0;
printf("\n\nLINEAR SEARCH IN SINGLE DIMENSIONAL ARRAY\n");
printf("******************************\n\n");
printf("Enter a Limit:\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to search:\t");
scanf("%d",&s);
for(i=0;i<n;i++)
if(a[i]==s)
{
c++;
}
if(c==0)
printf("BAD LUCK!!! Item not found\n");
else
{
printf("\n%d is found!!!\n",s);
printf("Positions\n");
for(i=0;i<n;i++)
{
if(a[i]==s)
printf("%d\t",i+1);
}
}
printf("\n\npress 1 to continue.....press 5 to exit\n");
scanf("%d",&ch);
if(ch==5)
exit(0);
}
}
2
OUTPUT
Program No:2
LINEAR SEARCH IN 2 D
AIM
Write a program to search an element in the 2 dimensional array using Linear Search.
ALGORITHM
STEP 01: START
STEP 02: INITIALIZE FLAG=0
STEP 03: ACCEPT ORDER OF MATRIX: ROW AND COLUMN AS R AND C
STEP 04: ACCEPT ELEMENTS A[I][J] OF THE MATRIX
STEP 05: ACCEPT ELEMENT TO SEARCH AS ITEM
STEP 06: ASSIGN I=0
STEP 07: REPEAT STEP 12 UNTIL I<R
STEP 08: ASSIGN J=0
STEP 09: REPEAT STEP 10 UNTIL J<C
STEP 10: IF (A[I][J]==ITEM)
SET FLAG=1 AND GOTO STEP 13
STEP 11: J=J+1
STEP 12: IF(FLAG==1)
EXIT FROM LOOP
ELSE GOTO STEP 12
STEP 13: I=I+1
STEP 14: IF(FLAG==1)
PRINT ITEM FOUND
ELSE
PRINT NOT FOUND
STEP 15: STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],s,i,j,r,c,f,x;
clrscr();
while(1)
{
f=0;
printf("\n\nLINEAR SEARCH IN TWO DIMENSIONAL ARRAY\n");
printf("******************************\n\n");
printf("Enter the Order of Matrix : Row & Column:\n");
scanf("%d%d",&r,&c);
printf("Enter %d elements:\n",r*c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("\nGiven Matrix\n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
}
printf("\nEnter the element to search:\t");
scanf("%d",&s);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
if(a[i][j]==s)
{
f=1;
break;
}
if(f==1)
break;
}
if(f==1)
printf("\n\n%d is found!!!\trow- %d\tcolumn- %d\n",s,i,j);
else
5
OUTPUT
LINEAR SEARCH IN TWO DIMENSIONAL ARRAY
*************************************************
Enter the Order of Matrix : Row & Column:
2
2
Enter 4 elements:
1
2
5
6
Given Matrix
1
2
5
6
Enter the element to search: 6
6 is found!!!
row- 1 colomn- 1
Given Matrix
3
5
Enter the element to search: 4
BAD LUCK!!! Item not found
Press 1 to continue5 to exit
5
7
Program No:3
MATRIX OPERATIONS
AIM
Perform the following operations in a Matrix:
1.Additon
2.Subtraction
3.Multiplication
4.Transpose
ALGORITHM:
STEP 01: START.
STEP 02: READ CHOICE.
IF CHOICE 1GO TO STEP 3 FOR ADDITION.
IF CHOICE 2 GO TO STEP 4 FOR SUBTRACTION.
IF CHOICE 3 GO TO STEP 5 FOR MULTIPLICATION.
IF CHOICE 4 GO TON STEP 6 FOR TRANSPOSE.
IF CHOICE 5 EXIT.
STEP 03: FOR ADDITION BY PASSING LIKE.
READ A[10][10],B[10][10],
SIZE OF ROW AND COLUMN.DO THE
FOLLOWING.
PROGRAM
#include<stdio.h>
#include<conio.h>
void read(int);
void mult(int,int,int);
void transpose(int,int);
int a[20][20],b[20][20],c[20][20],r1,c1,r2,c2,i,j;
void main()
{
int c;
clrscr();
while(1)
{
printf("\n\tMATRIX OPERATIONS\n");
printf("*\t--------------*\n");
printf("1.ADDITION\n2.SUBTRACTION\n3.MULTIPLICATION\n4.TRANSPOSE\n5.E
XIT");
printf("\nEnter your choice:\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nMATRIX ADDITION\n");
read(1);
break;
case 2:
printf("\nMATRIX SUBTRACTION\n");
read(2);
break;
case 3:
printf("\nMATRIX MULTIPLICATION\n");
printf("Enter order of 1st matrix:\n");
scanf("%d%d",&r1,&c1) ;
printf("Enter order of 2nd matrix:\n");
scanf("%d%d",&r2,&c2);
if(c1==r2)
mult(r1,c1,c2);
else
printf("\nMultiplication Impossible\n");
break;
case 4:
9
printf("\nMATRIX TRANSPOSE\n");
printf("Enter order of a matrix:\n");
scanf("%d%d",&r1,&c1) ;
transpose(r1,c1);
break;
case 5:
exit();
default:
printf("\nINVALID CHOICE\n");
break;
}
}
}
void read(int x)
{
printf("Enter Order of Matrix:\n");
scanf("%d%d",&r1,&c1);
printf("Enter Matrix1..with %d elements\n",r1*c1);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter Matrix2..with %d elements\n",r1*c1);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
if(x==1)
{
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
}
else
{
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]-b[i][j];
}
printf("\n\nMATRIX1\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",a[i][j]);
10
}
printf("\n\nMATRIX2\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",b[i][j]);
}
printf("\n\nRESULTANT MATRIX\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
}
}
void mult(int m,int n,int q)
{
int k;
printf("Enter Matrix1..with %d elements\n",m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter Matrix2..with %d elements\n",n*q);
for(i=0;i<n;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("\n\nMATRIX1\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
printf("\n\nMATRIX2\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
}
printf("\n\nProdut Matrix\n");
11
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d\t",c[i][j]) ;
}
}
void transpose(int p,int q)
{
printf("Enter Matrix..with %d elements\n",p*q);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&a[i][j]);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
b[j][i]=a[i][j];
printf("\n\nGiven MATRIX\n");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d\t",a[i][j]);
}
printf("\nTranspose Matrix\n");
for(i=0;i<q;i++)
{
printf("\n");
for(j=0;j<p;j++)
printf("%d\t",b[i][j]) ;
}
}
12
OUTPUT
MATRIX OPERATIONS
********************
1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.TRANSPOSE
5.EXIT
Enter your choice:
1
MATRIX ADDITION
Enter Order of Matrix:
2
2
Enter Matrix1with 4 elements
1
2
3
4
Enter Matrix2with 4 elements
5
6
7
8
MATRIX1
1
2
3
4
MATRIX2
5
6
7
8
RESULTANT MATRIX
6
8
10
12
MATRIX OPERATIONS
********************
1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.TRANSPOSE
5.EXIT
Enter your choice:
2
MARIX SUBTRACTION
Enter Order of Matrix:
13
2
2
Enter Matrix1with 4 elements
3
6
5
9
Enter Matrix2with 4 elements
1
4
3
2
MATRIX1
3
6
5
9
MATRIX2
1
4
3
2
RESULTANT MATRIX
2
2
2
7
MATRIX OPERATIONS
********************
1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.TRANSPOSE
5.EXIT
Enter your choice:
3
MATRIX MULTIPLICATION
Enter order of 1st matrix:
2
2
Enter order of 2nd matrix:
2
2
Enter Matrix1with 4 elements
2
2
2
2
Enter Matrix2with 4 elements
3
3
3
3
MATRIX1
2
2
2
2
MATRIX2
3
3
3
3
Product Matrix
12
12
12
12
14
MATRIX OPERATIONS
********************
1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.TRANSPOSE
5.EXIT
Enter your choice:
4
MATRTIX TRANSPOSE
Enter order of a matrix:
2
3
Enter Matrix..with 6 elements
2
6
8
4
5
Given Matrix
2
6
8
4
5
6
Transpose Matrix
2
4
6
5
8
6
MATRIX OPERATIONS
********************
1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.TRANSPOSE
5.EXIT
Enter your choice:
5
15
Program No:4
16
PROGRAM
#include<stdio.h>
#include<conio.h>
void swap_call(int,int);
void swap_ref(int *,int *);
void main()
{
int a,b;
clrscr();
printf("Swapping\n*********\n");
printf("Enter value for A:\t");
scanf("%d",&a);
printf("Enter value for B:\t");
scanf("%d",&b);
printf("\nSwapping by Call by Value\n");
swap_call(a,b);
printf("\nSwapping by Call by Reference\n");
printf("A = %d\tB =%d\n",a,b);
swap_ref(&a,&b);
printf("After Swapping \n");
printf("A = %d\tB =%d\n",a,b);
getch();
}
void swap_call(int x,int y)
{
int t;
printf("Before Swapping\n");
printf("A = %d\t B = %d\n",x,y);
t=y;
y=x;
x=t;
printf("After Swapping \n");
printf("A = %d\t B = %d\n",x,y);
}
void swap_ref(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
}
17
OUTPUT
Swapping
********
Enter value for A:
Enter value for B:
12
10
18
Program No:5
STRING OPERATIONS
AIM
Write a program to perform following string operations.
1. Addition
2. Copy
3. Reverse
4. Length
ALGORITHM
STEP 01 : START
STEP 02 : READ A STRING,NAME
STEP 03 : CALL K=STRLEN(NAME)
DISPLAY LENGTH
STEP 04 : READ TWO STRINGS A,B
STEP 05 : CALL FUNCTION STRCAT (A,B)
DISPLAY A AND B
STEP 06 : READ A STRING, C
STEP 07 : CALL THE FUNCTION STRREV(C)
DISPLAY C
STEP 08 : READ A STRING, D
STEP 09 : CALL THE FUNCTION STRCPY(E,D)
DISPLAY E
STEP 10 : STOP.
19
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[20];
int c,l;
while(1)
{
printf("String Operations\n**************\n\n");
printf("1.Addition\n2.Copy\n3.Reverse\n4.Length\n5.Exit\n");
printf("Enter Choice:\t");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter 1st String:\t");
scanf("%s",a);
printf("Enter 2nd String:\t");
scanf("%s",b);
printf("After Concatenation\n");
strcat(a,b);
printf("String 1:\t%s\nString 2:\t%s\n",a,b);
break;
case 2:
printf("Enter a String:\t");
scanf("%s",a);
printf("After Copying\n");
strcpy(b,a);
printf("String 1:\t%s\nString 2:\t%s\n",a,b);
break;
case 3:
printf("Enter a String:\t");
scanf("%s",a);
strcpy(b,strrev(a));
printf("Reverse:\t%s\n",b);
break;
case 4:
printf("Enter a String:\t");
scanf("%s",a);
20
l=strlen(a);
printf("Length:\t%d\n",l);
break;
case 5: exit();
default:
printf("Enter valid choice\n");
break;
}
}
}
21
OUTPUT
String Operations
**************
1. Addition
2. Copy
3. Reverse
4. Length
Enter Choice:
1
st
Enter 1 String: data
Enter 2nd String: structure
After Concatenation
String 1: datastructure
String 2: structure
String Operations
**************
1. Addition
2. Copy
3. Reverse
4. Length
Enter Choice:
Enter a String:
After Copying
String 1:
String 2:
2
stack
stack
String Operations
**************
1. Addition
2. Copy
3. Reverse
4. Length
Enter Choice:
3
Enter a String:
queue
Reverse: eueuq
22
String Operations
**************
1. Addition
2. Copy
3. Reverse
4. Length
Enter Choice:
Enter a String:
Length: 7
4
postfix
String Operations
**************
1. Addition
2. Copy
3. Reverse
4. Length
Enter Choice:
23
Program No:6.a
BINARY SEARCH
AIM
a)Write a program to search an element from an array using iterative binary search.
ALGORITHM
BINARYSEARCH(A,N,ITEM)
HERE A IS AN ARRAY OF N NUMBER OF ELEMENTS AND ITEM REPRESENTS THE
ITEM TO BE SEARCHED IN ARRAY A
STEP 01: START
STEP 02: SET FLAG=0
STEP 03: SET LOW=0 AND HIGH=N-1
STEP 04: REPEAT THROUGH STEP 6 WHILE(LOW<=HIGH)
STEP 05: CALCULATE MID AS: MID=(LOW+HIGH)/2
STEP 06: COMPARE A[MID] AND ITEM
IF(A[MID]==ITEM) THEN SET FLAG+1 AND GO TO STEP 7
ELSE IF(A[MID]>ITEM) THEN UPDATE AS HIGH=MID-1
ELSE UPDATE LOW AS LOW=MID+1
STEP 07: CHECK FLAG. IF FLAG=1 THEN PRINT A MESSAGE ITEM FOUND
OTHERWISE THEN PRINT A MESSAGE ITEM NOT FOUND
STEP 08: STOP
24
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,mid,fst,lst,f=0,s;
clrscr();
printf("\n\n\tBINARY SEARCHING\n");
printf("\t*----------*\n");
printf("\n\nEnter Limit:\n");
scanf("%d",&n);
printf("Enter %d Elements in Ascending Order:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter Element to be search:\t");
scanf("%d",&s);
fst=0;
lst=n-1;
while(fst<=lst)
{
mid=(fst+lst)/2;
if(s==a[mid])
{
f=1;
break;
}
else if(s<a[mid])
lst=mid-1;
else
fst=mid+1;
}
if(f==1)
printf("\n%d is found at position - %d\n\n",s,mid+1);
else
printf("Not Found!!\n");
getch();
}
25
OUTPUT
BINARY SEARCHING
*--------------------------*
Enter Limit:
5
Enter 5 Elements in Ascending Order:
5
6
8
10
12
Enter Element to be search: 8
8 is found at position - 3
26
Program No:6.b
BINARY SEARCH
AIM
b) Write a program to search an element from an array using recursive binary search.
ALGORITHM:
STEP 01: START.
STEP 02: DECLARE ARRAY A[10],I,N,M,C,L,U.
STEP 03: READ AN ARRAY AND ENTER THE ELEMENT TO BE SEARCHED.
STEP 04: INTIALIZE L=0 AND U=N-1.
STEP 05: CALL THE FUNCTION BINARY WITH RETURN AND ARGUMENT.
STEP 06: IN THE DEFINITION PART INITIALIZE C=0 AND PERFORM THE STEPS 7,8,9.
STEP 07: TO CHECK IF(L<=U) THEN SET MID=L+U/2.
STEP 08: THEN TO CHECK IF(M==A[MID]) THEN SET C=1.
ELSEIF(M<A[MID]) THEN RETURN BINARY(A,N,M,MID+1,U).
STEP 09: OTHERWISE RETURN C.
STEP 10: IN THE MAIN PROGRAM TO CHECK IF(C++0) THEN PRINT NUMBER
IS NOT FOUND
OTHERWISE PRESENT.
STEP 11: STOP.
27
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,m,c,l,u;
clrscr();
printf("RECURSIVE BINARY SEARCHING\n\n");
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: " );
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("%d is is not found.",m);
else
printf("Number is found.");
getch();
}
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else if(m<a[mid])
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
28
OUTPUT
RECURSIVE BINARY SEARCHING
Enter the size of an array:
5
Enter the elements of the array:
5
6
12
18
22
Enter the Number to be search: 18
18 is found.
29
Program No:7
BUBBLE SORT
AIM
Write a program to sort numbers in ascending order using Bubble Sorting.
ALGORITHM
STEP 01: START
STEP 02: INITIALIZE I, J, TEMP, N, ARRAY A[SIZE].
STEP 03: READ N ARRAY ELEMENT.
STEP 04: REPEAT STEP 05, WHILE I<N AND J<N-I.
STEP 05: COMPARE A[J] AND A[J+1]
IF A[J]>A[J+1] THEN
TEMP=A[J]
A[J]=A[J+1]
A[J+1]=TEMP
STEP 06: PRINT SORTED ARRAY A .
STEP 07: STOP.
30
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,n,t,k;
clrscr();
printf("\n\nBUBBLE SORTING\n");
printf("*------------------*\n\n");
printf("Enter a Limit:\n");
scanf("%d",&n);
printf("Enter %d Elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nBEFORE SORTING\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
printf("\n\nAfter %d pass: \t",i+1);
for(k=0;k<n;k++)
printf("%d\t",a[k]);
}
printf("\n\nAFTER SORTING\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
31
OUTPUT
BUBBLE SORTING
*----------------------*
Enter a Limit:
5
Enter 5 Elements:
9
6
12
BEFORE SORTING
9
6
12
7
After 1 pass:
After 2 pass:
After 3 pass:
After 4 pass:
After 5 pass:
9
7
3
6
6
7
3
7
7
7
12
6
6
6
3
3
AFTER SORTING
3
6
7
3
9
9
9
9
12
12
12
12
12
32
Program No: 8
SELECTION SORT
AIM
Write a program to implement Selection Sort.
ALGORITHM
STEP 01 : START
STEP 02 : REPEAT STEPS 3AND 4 FOR K=1,2,..,N-1.
STEP 03 : CALL MIN (A,K,N,LOC)
STEP 04 : [INTERCHANGE A[K] AND A[LOC]]
SET TEMP : = A[K], A[K] : =A[LOC] AND A[LOC] : =TEMP
[END OF STEP 2 LOOP]
STEP 05 : EXIT
33
PROGRAM
#include<stdio.h>
#include<conio.h>
void sel_sort(int[],int);
void main()
{
int x[20],n,i;
clrscr();
printf("\nSelection Sort\n**************\n");
printf("Enter Limit:\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
sel_sort(x,n);
printf("Sorted Elements\n");
for(i=0;i<n;i++)
printf("%d\n",x[i]);
getch();
}
void sel_sort(int a[],int n)
{
int i,pos,j,large,k;
for(i=n-1;i>0;i--)
{
large=a[0];
pos=0;
for(j=1;j<=i;j++)
{
if(a[j]>large)
{
large=a[j];
pos=j;
}
}
a[pos]=a[i];
a[i]=large;
printf("\n\nAfter %d pass: \t",i);
for(k=0;k<n;k++)
printf("%d\t",a[k]);
}
}
34
OUTPUT
Selection Sort
************
Enter Limit: 4
Enter 4 elements:
3
9
1
6
After 3 pass: 3
After 2 pass: 3
After 1 pass: 1
Sorted Elements
1
3
6
9
6
1
3
1
6
6
9
9
9
35
Program No:9
INSERTION SORT
AIM
Write a program to implement Insertion Sort.
ALGORITHM
STEP 01: START
STEP 02: DECLARE INT S[100] //TO HOLD INTEGER VALUES
STEP 03: ASSIGN S[0]=X[0] //WHEREX IS AN ARRAY HAVING INPUT VALUES
STEP 04: ASSIGN I=1
STEP 05: REPEAT STEP 6 TO STEP 8 UNTIL I<ITEMS
STEP 06: ASSIGN TEMP=I-1
STEP 07: WHILE((S[J]>TEMP) AND (J>=0))
S[J+1]=S[J]
DECREMENT J BY 1
STEP 08: INCREMENT I BY 1
STEP 09: ASSIGN I =0
STEP 10: REPEAT STEP 11 TO STEP 12 UNTIL I<ITEMS
STEP 11: ASSIGN X[I]=S[I] //TRANSFER TEMPORARY ARRAY TO ACTUAL ARRAY
STEP 12: INCREMENT I BY 1
STEP 13: STOP
36
PROGRAM
#include<stdio.h>
#include<conio.h>
void insert(int[],int);
void main()
{
int n,a[20],i;
clrscr();
printf("INSERTION SORTING\n*******************\n");
printf("Enter Limit\n");
scanf("%d",&n);
printf("Enter %d Elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nBefore Sorting Elements\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
insert(a,n);
printf("\n\nAfter Sorting Elements\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void insert(int a[],int n)
{
int j,k,temp,i;
for(k=1;k<=n-1;k++)
{
temp=a[k];
j=k-1;
while(temp<a[j]&&j>=0)
{
if(temp<a[j])
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
printf("\n\nAfter %d pass: \t",k);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
}
37
OUTPUT
INSERTION SORTING
*******************
Enter Limit
5
Enter 5 Elements
2
8
0
1
12
2
0
0
0
8
2
1
1
0
8
2
2
1
1
8
8
12
12
12
12
38
Program No:10
QUICK SORT
AIM
Write a program to implement Quick Sort.
ALGORITHM
QUICKSORT(A,FIRST,LAST)
HERE A IS AN ARRAY OF ELEMENTS AND FIRST AND LAST REPRESENT THE FIRST
INDEX AND LAST INDEX OF THE ARRAYA
PERFORM THE FOLLOWING STEPS ONLY IF(I<J)
STEP 01: START
STEP 02: SET I=FIRST+1
STEP 03: SET J=LAST
STEP 04: REPEAT THROUGH STEP 9 WHILE(I<J)
STEP 05: REPEAT THROUGH STEP 6 WHILE(A[I]<A[FIRST])
STEP 06: INCREMENT THE VALUE OF I AS I=I+1
STEP 07: REPEAT THROUGH STEP 8 WHILE(A[J]>A[FIRST])
STEP 08: DECREMENT THE VALUE OG J AS J=J-1
STEP 09: IF(I<J) THEN EXCHANGE THE VALUE OF A[I] AND A[J]
STEP 10: EXCHANGE THE VALUE OF A[FIRST] AND A[J]
STEP 11: CALL QUICKSORT(A<FIRST<J-1)
STEP 12: CALL QUICKSORT(A,J+1,LAST)
STEP 13: STOP
39
PROGRAM
#include<stdio.h>
#include<conio.h>
void quick(int [],int,int);
void main()
{
int x[20],size,i;
clrscr();
printf("\tQuick Sort\n**********\n");
printf("Enter Limit:\t");
scanf("%d",&size);
printf("Enter %d Elements\n",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quick(x,0,size-1);
printf("Sorted Elements:\n");
for(i=0;i<size;i++)
printf("%d\n",x[i]);
getch();
}
void quick(int x[10],int f,int l)
{
int p,i,j,temp,k;
if(f<l)
{
p=f;
i=f;
j=l;
//printf("\nPass: %d",i);
//printf("\nPivot:%d\n",x[p]) ;
while(i<j)
{
while(x[i]<=x[p] && i<l)
i++;
while(x[j]>x[p])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
40
x[j]=temp;
}
}
temp=x[p];
x[p]=x[j];
x[j]=temp;
quick(x,f,j-1);
quick(x,j+1,l);
}
}
41
OUTPUT
Quick Sort
********
Enter Limit: 5
Enter 5 Elements
2
9
6
7
4
Sorted Elements:
2
4
6
7
9
42
Program No:11
MERGE SORT
AIM
Write a program to implement Merge Sort.
ALGORITHM
STEP 01: START
STEP 02: READ LIMIT N
STEP 03: READ N ELEMENTS
STEP 04: CALL THE FUNCTION MERGE_SORT(ARR,LOW,HIGH)
STEP 05: IF LOW<HIGH THEN CALCULATE MID AS:
MID=(LOW+HIGH)/2
STEP 06: CALL THE FUNCTION MERGE_SORT(ARR,LOW,MID)
STEP 07: CALL THE FUNCTION MERGE_SORT(ARR,MID+1,HIGH)
STEP 08: CALL THE FUNCTION MERGE(ARR,LOW,MID,HIGH)
STEP 09: ASSIGN N1=(MID+LOW-1)
N2=(HIGH-MID)
STEP 10: FOR J=0 TO N2-1
ASSIGN ARR2[J]=ARR[MID+J+1]
STEP 11: ASSIGN ARR1[J]=9999 AND ARR2[J]=9999.
ALSO SET I=0,J=0
STEP 12: FOR K=0 TO HIGH REPEAT STEP 13
STEP 13: CHECK IF ARR1[I]<=ARR2[J] THEN
ASSIGN ARR[K]=ARR1[I++] OTHERWISE ASSIGN ARR[K]=ARR1[J++]
INCREMENT K
STEP 14: DISPLAY SORTED ELEMENTS
STEP 15: STOP
43
PROGRAM
#include<stdio.h>
#include<conio.h>
int arr[10];
void main()
{
int i,n;
clrscr();
printf(Merge Sort Using Divide & Conquer Technique\n);
printf(---------------------------------------------------------------\n\n);
printf("Enter Limit:\n");
scanf("%d",&n);
printf("Enter %d Elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
merge_sort(arr, 0, n-1);
printf("\nSorted array:\n");
for(i = 0; i < n; i++)
printf("%d\n", arr[i]);
getch();
}
int merge_sort(int arr[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
// Divide and Conquer
merge_sort(arr, low, mid);
merge_sort(arr, mid + 1, high);
// Combine
merge(arr, low, mid, high);
}
return 0;
}
int merge(int arr[], int l, int m, int h)
{
int arr1[10], arr2[10];
int n1, n2, i, j, k;
n1 = m - l + 1;
44
n2 = h - m;
for(i = 0; i < n1; i++)
arr1[i] = arr[l + i];
for(j = 0; j < n2; j++)
arr2[j] = arr[m + j + 1];
arr1[i] = 9999;
arr2[j] = 9999;
i = 0;
j = 0;
for(k = l; k <= h; k++)
{
if(arr1[i] <= arr2[j])
arr[k] = arr1[i++];
else
arr[k] = arr2[j++];
}
return 0;
}
45
OUTPUT
Merge Sort Using Divide & Conquer Technique
-------------------------------------------------------Enter Limit:
7
Enter 7 elements:
5 2 6 4 12 3 1
Sorted array:
1
2
3
4
5
6
12
46
Program No:12
HEAP SORT
AIM
Write a program to implement Heap Sort.
ALGORITHM:
STEP 01 : START
STEP 02 : DECLARE A[10] AND READ THE LIMIT ,N
STEP 03 : READ THE ELEMENTS
STEP 04 : CALL HEAPSORT(A,N) GOTO STEP 05
STEP 05 : CONSTRUCT HEAP TREE, CALL HEAPIFY(A,N)
DECLARE K,I,J AND ITEM
FOR K=0 UPTO K<N
ITEM = A[K]
I=K
J = (I-1)/2
WHILE I>0 AND ITEM>A[J]
A[I] = A[J]
I=J
J= (I-1)/2
A[I] = ITEM
STEP 06 : SORT ELEMENTS ,REPLACE ROOT NODE WITH CHILDREN
FOR I=N-1 TO I>0
T = A[0]
A[0] = A[I]
A[I] = T
DECREMENT I
STEP 07 : ADJUST HEAP TREE
STEP 08 : DISPLAY SORTED ARRAY ELEMENTS
FOR I=0 UPTO I<N
PRINT
A[I]
INCREMENT I
47
STEP 09:STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
void main()
{
int n,i,a[20];
clrscr();
printf("\n\tHEAP SORT\n\t*********");
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe Elements Are:\n");
for(i=0;i<n;i++)
{
printf("\t%d ",a[i]);
}
heapsort(a,n);
printf("\nThe Sorted Elements Are:\n");
for(i=0;i<n;i++)
{
printf("\t%d ",a[i]);
}
getch();
}
void heapsort(int a[],int n)
{
int i,t;
heapify(a,n);
printf("\nThe Heap Tree Elements Are:\n");
for(i=0;i<n;i++)
{
printf("\t%d ",a[i]);
}
for(i=n-1;i>0;i--)
{
t = a[0];
a[0] = a[i];
48
a[i] = t;
adjust(a,i);
}
}
void heapify(int a[],int n)
{
int k,i,j,item;
for(k=0;k<n;k++)
{
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j]))
{
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}
}
void adjust(int a[],int n)
{
int i,item,j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1)
{
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i])
{
a[j] = a[i];
j = i;
i = 2*j+1;
}
else
break;
a[j] = item;
}
}
49
OUTPUT:
HEAP SORT
***********
Enter the limit:
8
Enter the elements:
6 5 3 1 8 7 2 4
The Elements Are:
6 5 3 1 8 7 2 4
The Heap Tree Elements Are:
8 6 7 4 5 3 2 1
The Sorted Elements Are:
1 2 3 4 5 6
50
Program No:13
51
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 20
void push();
void pop();
void display();
int top=-1,stack[MAX];
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n\n\tARRAY IMPLEMENTATION OF STACK\n\n);
printf(MENU\n);
printf(---------)
1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit();
default:
printf("Enter a valid choice!!!\n");
break;
}
}
}
52
void push()
{
int item;
if(top==MAX-1)
printf("STACK OVERFLOW!!!!\n");
else
{
top++;
printf("Enter an Item:\t");
scanf("%d",&item);
stack[top]=item;
printf("%d Saved!!\n",stack[top]);
}
}
void pop()
{
int item;
if(top==-1)
printf("STACK UNDERFLOW!!\n");
else
{
item=stack[top];
printf("%d deleted..\n",item);
top--;
}
}
void display()
{
int i;
if(top==-1)
printf("EMPTY STACK!!!\n");
else
{
printf("\nSTACK ELEMENTS\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}
53
OUTPUT
ARRAY IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
1
Enter an Item: 10
10 Saved!!
ARRAY IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
1
Enter an Item: 20
20 Saved!!
ARRAY IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
1
Enter an Item: 30
30 Saved!!
54
20
10
ARRAY IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
2
20 deleted..
ARRAY IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
2
10 deleted..
ARRAY IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
3
EMPTY STACK!!
56
Program No:14
ALGORITHM
STEP 01: START
STEP 02: SET TOP=NULL
STEP 03: [PUSH OPERATION]
CREATE TEMP VARIABLE USING MALLOC() FUNCTION
STEP 04: READ DATA ITEM
STEP 05: ASSIGN TEMP->DATA=ITEM
TEMP->LINK=TOP
TOP=TEMP
STEP 06: [POP OPERATION]
CHECK IF TOP=NULL THEN DISPLAY STACK UNDERFLOW
ELSE ASSIGN TEMP=TOP
STEP 07: FREE THE VARIABLE TEMP
STEP 08: ASSIGN TOP=TOP->LINK
STEP 09: [DISPLAY OPERATION]
CHECK IF TOP=NULL THEN DISPLAY STACK EMPTY
STEP 10: DO THE FOLLOWING STEPS 11 & 12 IF TEMP!=NULL
STEP 11: PRINT TEMP->DATA TEMP=TEMP->LINK
STEP 12: STOP
57
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void push();
void pop();
void display();
typedef struct node
{
int data;
struct node *link;
}n;
n *top=NULL;
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n\n\tLINKEDLIST IMPLEMENTATION OF STACK\n\n");
printf("MENU\n");
printf("-----\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
58
OUTPUT
LINKEDLIST IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
1
Enter an Item: 100
100 saved..
LINKEDLIST IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
1
Enter an Item: 200
200 saved..
LINKEDLIST IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
Enter your choice
1
60
STACK ELEMENTS
200
100
LINKEDLIST IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
2
200 deleted..
LINKEDLIST IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
2
100 deleted..
LINKEDLIST IMPLEMENTATION OF STACK
MENU
--------1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice
3
STACK EMPTY!!!
62
63
Program No: 15
STEP 5. ELSE
STEP 6. SET REAR = REAR + 1
[END OF STEP 4 IF]
STEP 9. EXIT
FOR DENQUEUE
STEP 1. IF (FRONT == 0) THEN [CHECK FOR UNDERFLOW]
STEP 2. PRINT: UNDERFLOW
STEP 3. ELSE
STEP 4. ITEM = QUEUE[FRONT]
STEP 5. IF (FRONT == REAR) THEN [CHECK IF ONLY ONE ELEMENT IS LEFT]
(A) SET FRONT = 0
(B) SET REAR = 0
STEP 6. ELSE
STEP 7. SET FRONT = FRONT + 1 [INCREMENT FRONT BY 1]
[END OF STEP 5 IF]
64
STEP 9. EXIT
PROGRAM
/*queue using array*/
#include<stdio.h>
#include<conio.h>
#define qsize 10
void displaymenu();
int isempty();
int isfull();
int enqueue(int value);
int dequeue(int *value);
int peek();
void view();
int queue[qsize],front=-1,rear=-1;
void main(){
int status,choice,data;
displaymenu();
while(1){
printf("\n? ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("enter the element: ");
fflush(stdin);
scanf("%d",&data);
status=enqueue(data);
if(status==-1)
printf("queue overflow on enqueuwe ");
break;
case 2:
status=dequeue(&data);
if(status==-1)
printf("queue underflow on dequque ");
else
printf("the dequeued value is %d ",data);
break;
case 3:
65
view();
break;
default: printf("\n end of program");
exit(0);
}}}
void displaymenu(){
printf("\n\t\tqueue using array " );
printf("\n\t1.enqueue");
printf("\n\t2.dequeue");
printf("\n\t3.view");
printf("\n\t4.exit");
}
int isempty(){
if(front==-1&&rear==-1)
return -1;
else
return 0;
}
int isfull(){
if(rear==(qsize-1))
return -1;
else
return 0;
}
int enqueue(int value){
if(isfull())
return -1;
else if(isempty())
front=rear=0;
else
rear=rear+1;
queue[rear]=value;
return 0;
}
int dequeue(int *value){
if(isempty())
return -1;
*value=queue[front];
if(front==rear)
66
front=rear=-1;
else
front=front+1;
return 0;
}
void view(){
int f;
if(isempty()){
printf("queue is empty..");
return;
}
printf("content of queue is " );
for(f=front;f!=rear+1;f++)
printf(" %d",queue[f]);
}
67
OUTPUT
QUEUE USING ARRAY
1.ENQUEUE
2.DEQUEUE
3.VIEW
4.EXIT
?2
Queue underflow on DEQUEUE
?3
Queue Empty..
?1
Enter the element :1001
?1
Enter the element :1002
?1
Enter the element :1003
?1
Enter the element :1004
?1
Enter the element : 1005
?2
Dequeued value 1001
?3
CONTENT OF QUEUE IS
1002 1003 1004 1005
END OF PROGRAM
68
Program No: 16
QUEUE USING LINKED LIST
AIM
Write a program to implement queue using linked list.
ALGORITHM
FOR ADDING ELEMENT INTO A QUEUE
STEP 1: START
STEP 2: ASSIGN TEMP=NEWNODE
STEP 3: ASSIGN FRONTPTR=REARPTR=NULL
STEP 4: ACCEPT NO
STEP 5: ASSIGN TEMP->NEXT=NULL
STEP 6: IF(FRONTPTR=NULL)
6.1: ASSIGN FRONTPTR=TEMP
6.2: ASSIGN REARPTR=TEMP
ELSE
6.1: ASSIGN REARPTR->NEXT=TEMP
6.2: ASSIGN REARPTR=REARPTR->NEXT
STEP 7: STOP
FOR DELETING AN ELEMENT FROM A QUEUE
STEP1: START
STEP 2: IF (FRONTPTR=NULL)
2.1: DISPLAY QUEUE IS EMPTY
2.2: RETURN
STEP 3: ASSIGN DELEPTR=FRONTPTR
STEP 4: IF(DELETEPTR!=NULL)
4.1: DISPLAY DELETEPTR->NO
4.2: ASSIGN TEMP=FRONTPTR
4.3: ASSIGN FRONTPTR=FRONTPTR->NEXT
4.4: DELETE DELETEPTR
STEP 5: STOP
69
70
PROGRAM
#include<stdio.h>
#include<conio.h>
#define null 0
struct node
{
int no;
struct node *next;
};
struct node *frontptr,*rearptr;
void addqueue();
void delqueue();
void display();
void main()
{
int choice;
while(1)
{
printf(\n1.Insertion);
printf(\n2.Deletion);
printf(\n3.Display);
printf(\n4.Quit);
printf(\n enter your choice);
scanf(%d,&choice);
switch(choice)
{
case 1:addqueue();
break;
case 2:delqueue();
break;
case 3:
display();
break;
case 4:exit(0);
default:printf(\ninvalid choice);
break;
getch();
}
}
}
void addqueue()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
71
printf(\nenter no);
scanf(%d,&temp->no);
temp->next=null;
if(frontptr==null)
{
frontptr=temp;
rearptr=temp;
}
else
{
rearptr->next=temp;
rearptr=rearptr->next;
}}
void delqueue()
{
struct node *deletptr;
if(frontptr==null)
{
printf(\n queue is empty);
return;
}
deleteptr=frontptr;
if(deleteptr!=null)
{
printf(\ndeleted element is %d,deleteptr->no);
frontptr=frontptr->next;
free(deleteptr);
}}
void display()
{
struct node *p;
p=frontptr;
if(p==null)
{
printf(\nqueue is empty);
return;
}
printf(\n the elements present in the queue:\n);
while(p!=null)
{
printf(%5d,p->no);
p=p->next;
}}
72
OUTPUT
1.Insertion
2.Deletion
3.Display
4.Quit
Enter your choice : 1
Enter value 20
1.Insertion
2.Deletion
3.Display
4.Quit
Enter your choice : 1
Enter value :30
1.Insertion
2.Deletion
3.Display
4.Quit
Enter your choice : 2
Deleted element is 20
1.Insertion
2.Deletion
3.Display
4.Quit
Enter your choice : 3
Element present in the Queue
30
1.Insertion
2.Deletion
3.Display
4.Quit
Enter your choice : 4
73
Program No: 17
INSERTION AT END
1.PTR=HEADER.LINK
2.ENDNODE=INSERTEND()
3.WHILE(PTR.RLINK!=NULL)
1.PTR=PTR.RLINK
4.ENDWHILE
5.PTR.RLINK=ENDNODE
6.ENDNODE.LLINK=PTR
7.ENDNODE.DATA=DATA
8.ENDNODE.RLINK=NULL
74
3.ENDWHILE
4.PTR2.RLINK=NULL
5.RETURNNODE(PTR)
DELETION AT ANY POSITION
1.PTR=HEADER
2.WHILE(PTR.RLINK!=NULL)AND(PTR.DATA!=KEY)
1.PTR3=PTR
2.PTR=PTR.RLINK
3.ENDWHILE
4.IF(PTR.RLINK==NULL)
1.PRINT ELEMENT NOT FOUND
2.EXIT
5.ELSE
1.PTR4=PTR.RLINK
2.PTR3.RLINK=PTR4
3.PTR.LLINK=PTR3
6.ENDIF
7.STOP
76
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
}*header,*ptr;
void main()
{
int ch;
void create();
void insertfront();
void insertend();
void insertany();
void deletefront();
void deleteend();
void deleteany();
void traverse();
clrscr();
create();
traverse();
do
{
printf("\nEnter your choice");
printf("\n1:Insertion at front");
printf("\n2:Insertion at end");
printf("\n3:Insertion at any position");
printf("\n4:Deletion at front");
printf("\n5:Deletion at end");
printf("\n6:Deletion at any position");
printf("\n7:Quit");
scanf("\n%d",&ch);
switch(ch)
{
case 1:
insertfront();
break;
case 2:
insertend();
77
break;
case 3:
insertany();
break;
case 4:
deletefront();
break;
case 5:
deleteend();
break;
case 6:
deleteany();
break;
case 7:
break;
default:
printf("\nInvalid choice");
break;
}
traverse();
}while(ch!=7);
getch();
}
void create()
{
if(header==NULL)
{
struct node *firstnode;
firstnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data\n");
scanf("\n%d",&firstnode->data);
firstnode->rlink=NULL;
header=firstnode;
firstnode->llink=header;
ptr=firstnode;
}
while(1)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data\n");
scanf("\n%d",&newnode->data);
if(newnode->data==0)
break;
78
newnode->rlink=NULL;
ptr->rlink=newnode;
newnode->llink=ptr;
ptr=newnode;
}
}
void insertfront()
{
struct node *frontnode;
frontnode=(struct node*)malloc(sizeof(struct node));
ptr=header;
printf("\nEnter the data\n");
scanf("\n%d",&frontnode->data);
header=frontnode;
frontnode->rlink=ptr;
}
void insertend()
{
struct node *endnode;
endnode=(struct node*)malloc(sizeof(struct node));
ptr=header;
printf("\nEnter the data");
scanf("\n%d",&endnode->data);
while(ptr->rlink!=NULL)
{
ptr=ptr->rlink;
}
ptr->rlink=endnode;
endnode->llink=ptr;
endnode->rlink=NULL;
}
void insertany()
{
int key;
struct node *anynode;
struct node *ptr1;
anynode=(struct node*)malloc(sizeof(struct node));
ptr=header;
printf("\nEnter the key");
scanf("\n%d",&key);
printf("\nEnter the data");
scanf("\n%d",&anynode->data);
while((ptr->rlink!=NULL)&&(ptr->data!=key))
{
79
ptr=ptr->rlink;
}
if(ptr->rlink==NULL)
{
printf("\nKey not found");
}
else
{
ptr1=ptr->rlink;
ptr->rlink=anynode;
anynode->llink=ptr;
anynode->rlink=ptr1;
ptr1->llink=anynode;
}
}
void deletefront()
{
struct node *ptr1;
ptr=header;
ptr1=ptr->rlink;
header=ptr1;
ptr1->llink=header;
free(ptr);
}
void deleteend()
{
struct node *ptr2;
ptr=header;
while(ptr->rlink!=NULL)
{
ptr2=ptr;
ptr=ptr->rlink;
}
ptr2->rlink=NULL;
free(ptr);
}
void deleteany()
{
int key;
struct node *ptr3;
struct node *ptr4;
ptr=header;
printf("\nEnter the key");
scanf("\n%d",&key);
80
while((ptr->rlink!=NULL)&&(ptr->data!=key))
{
ptr3=ptr;
ptr=ptr->rlink;
}
if(ptr->rlink==NULL)
{
printf("\nKey not found");
}
else
{
ptr4=ptr->rlink;
ptr3->rlink=ptr4;
ptr4->llink=ptr3;
free(ptr);
}
}
void traverse()
{
ptr=header;
printf("\n\nDATA\tADDRESS\tLLINK\tRLINK\n\n");
while(ptr!=NULL)
{
printf("\n%d\t%u\t%d\t%d\n",ptr->data,&ptr->data,ptr->llink,ptr->rlink);
ptr=ptr->rlink;
}
}
81
OUTPUT
Enter the data
5
Enter the data
4
Enter the data
3
Enter the data
2
Enter the data
0
DATA ADDRESS LLINK RLINK
5
2240 2240 2250
4
2250 2240 2260
3
2260 2250 2270
2
2270 2260 0
Enter your choice
1:Insertion at front
2:Insertion at end
3:Insertion at any position
4:Deletion at front
5:Deletion at end
6:Deletion at any position
7:Quit
1
Enter the data
6
1:Insertion at front
2:Insertion at end
3:Insertion at any position
4:Deletion at front
5:Deletion at end
6:Deletion at any position
7:Quit
4
DATA ADDRESS LLINK RLINK
5
2240 2240 2250
4
2250 2250 2310
9
2310 2250 2260
3
2260 2310 2270
2
2270 2260 2300
1
2300 2270 0
Enter your choice
1:Insertion at front
2:Insertion at end
3:Insertion at any position
4:Deletion at front
5:Deletion at end
6:Deletion at any position
7:Quit
5
DATA ADDRESS LLINK RLINK
5
2240 2240 2250
4
2250 2250 2310
9
2310 2250 2260
3
2260 2310 2270
2
2270 2260 0
Enter your choice
1:Insertion at front
2:Insertion at end
3:Insertion at any position
4:Deletion at front
5:Deletion at end
6:Deletion at any position
7:Quit
84
6
Enter the key
9
DATA ADDRESS LLINK RLINK
5
2240 2240 2250
4
2250 2250 2260
3
2260 2250 2270
2
2270 2260 0
Enter your choice
1:Insertion at front
2:Insertion at end
3:Insertion at any position
4:Deletion at front
5:Deletion at end
6:Deletion at any position
7:Quit
7
DATA ADDRESS LLINK RLINK
5
2240 2240 2250
4
2250 2250 2260
3
2260 2250 2270
2
2270 2260 0
85
Program No: 18
86
PROGRAM
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int info;
struct node *next;
}n;
n *start=NULL;
void create();
void display();
void main()
{
int x;
clrscr();
create();
display();
getch();
}
void create()
{
n *temp,*p;
int x;
printf("Enter Elements.... Enter 0 to quit\n");
scanf("%d",&x);
if(x!=0)
{
temp=(n*)malloc(sizeof(n));
temp->info=x;
temp->next=NULL;
start=temp;
p=temp;
scanf("%d",&x);
while(x!=0)
{
temp=(n*)malloc(sizeof(n));
temp->info=x;
temp->next=NULL;
p->next= temp;
87
p=temp;
scanf("%d",&x);
}
}
}
void display()
{
n *temp;
int c=0;
if(start==NULL)
printf("Empty List\n");
else
{
temp=start;
printf("Elements are:\n");
while(temp!=NULL)
{
printf("%d\n",temp->info);
c++;
temp=temp->next;
}
printf("\nNo: of Nodes = %d\n",c);
}
}
88
OUTPUT
Enter Elements.... Enter 0 to quit
1
2
4
6
8
9
0
Elements are:
1
2
4
6
8
9
No: of Nodes = 6
89
Program No:19
ALGORITHM
STEP 01 : START.
STEP 02 : DECLARE A STRUCTURE WITH VARIABLE 'DATA' AND A POINTER TO THE NODE
'NEXT'.
STEP 03 : CREATE A NODE 'HEAD'.
STEP 04 : INITIALIZE INT I , VALUE.
DECLARE CHAR='Y'.
STEP 05: ENTER THE OPERATION TO DO.
STEP 06: SCAN THE CHOICE.
STEP 07: FOR CHOICE 1: INSERT A DATA.
SCAN THE DATA.
CALL THE FUNCTION INSERT_DATA ().
CALL THE FUNCTION DISPLAY ().
STEP 08: FOR CHOICE 2: DISPLAY THE DATA.
CALL THE FUNTION DISPLAY ().
STEP 09: FOR CHOICE 3: REVERSE THE LIST.
CALL THE FUNCTION REVERSE_LIST ().
CALL THE FUNCTION DISPLAY ().
STEP 10: FOR CHOICE 4: EXIT ().
STEP 11: STOP.
FUNCTION INSERT_DATA ()
1: CREATE NODE 'VAR' , 'TEMP'.
2: TEMP= HEAD.
3: SPECIFY MEMORY ALLOCATION FOR VAR.
4: VAR-> DATA= VALUE.
5:IF (HEAD==NULL)
90
HEAD= VAR.
HEAD-> NEXT= NULL.
ELSE WHILE ( TEMP-> NEXT!=NULL)
TEMP= TEMP-> NEXT.
6:VAR-> NEXT=NULL.
7.TEMP-> NEXT=VAR.
8.EXIT.
FUNCTION DISPLAY ()
1: CREATE NODE VAR.
2:VAR=HEAD.
3:WHILE (VAR!=NULL)
PRINT THE NUMBERS.
VAR=VAR-> NEXT.
4: EXIT.
FUNCTION REVERSE_LIST ()
1: CREATE NODE 'TEMP' , 'TEMP1'.
2: TEMP=HEAD.
3:VAR=NULL.
4:WHILE (TEMP!=NULL)
TEMP1=VAR.
VAR=TEMP.
TEMP=TEMP-> NEXT.
VAR-> NEXT=TEMP1.
5: HEAD= VAR
6: EXIT.
91
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void insert_data(int value)
{
struct node *var,*temp;
temp=head;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
var->next=NULL;
temp->next=var;
}
}
void reverse_list()
{
struct node *temp,*temp1,*var;
temp=head;
var=NULL;
while(temp!=NULL)
{
temp1=var;
var=temp;
temp=temp->next;
92
var->next=temp1;
}
head=var;
}
void display()
{
struct node *var;
var=head;
printf("\nlist of elments are \n");
while(var!=NULL)
{
printf("-> %d ",var->data);
var=var->next;
}
}
void main()
{
int i,value;
char ch='y';
head=NULL;
printf(" 1.) Insert node");
printf("\n 2.) display the list");
printf("\n 3.) reverse the nodes");
printf("\n 4.) exit");
while(ch=='y')
{
printf("\nChoose to do operation :");
scanf("%d",&i);
switch(i)
{
case 1 :
{
printf("\nEnter the data to be inserted in node ");
scanf("%d",&value);
insert_data(value);
display();
break;
}
case 2 :
{
display();
break;
93
}
case 3 :
{
reverse_list();
display();
break;
}
case 4 :
{
exit(0);
break;
}
}
}
getch();
}
94
OUTPUT
1) Insert node.
2) Display the list.
3) Reverse the node.
4) Exit.
Choose to do operation: 1
Enter the data to be inserted in node: 10
List of elements are: -> 10
Choose to do operation: 1
Enter the data to be inserted in node: 20
List of elements are:-> 10
-> 20
Choose to do operation: 1
Enter the data to be inserted in node: 30
List of elements are: -> 10
-> 20
-> 30
Choose to do operation: 2
List of elements are: -> 10
-> 20
-> 30
Choose to do operation: 3
List of elements are: -> 30
-> 20
-> 10
95
Program No: 20
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct node
{
int exp;
int coff;
struct node *link;
};
struct nod *st1=NULL;*st2=NULL;*st3=NULL;
void createpol(struct node * *);
void display(struct node *);
void add(struct node*,struct node *, struct node **);
void main();
{
struct node *temp,*p,*t,*p1,*p2,*p3;
char ch;
clrscr();
printf(\n create first polynomial);
createpol(&st1);
printf(\n create second polynomial);
createpol(&st2);
printf(\n first polynomial is..);
display(st1);
printf(\n second polynomial is..);
display(st2);
//addition
p1=st1;
p2=st2;
p3=st3;
while(p1!=NULL&&p2!=NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->link=NULL;
if(p1->exp>p2->xp)
{
temp->coeff=p1->coff;
temp->exp=p1->exp;
p1=p1->link;
}
else if(p2->exp>p1->exp)
{
temp->coeff=p2->coeff;
97
temp->exp=p2->exp;
p2=p2->link;
}
else
{
temp->coeff=p1->coeff+p2->coeff;
temp->exp=p1->exp;
p1=p1->link;
p2=p2->link;
}
if(st3==NULL)
{
st3=temp;
p3=temp;
}
else
{
p3->link=temp;
p3=temp;
}
}
if(p1==NULL&&p2!=NULL)
{
while(p2!=NULL)
{
temp=(struct node*)mlloc (sizeof(struct node));
temp->link=NULL;
temp->coeff=p2->coeff;
temp->exp=p2->exp;
p2=p2->link;
if(st3==NULL)
{
st3=temp;
p3=temp;
}
else
{
p3->link=temp;
p3=temp;
}
}
if(p2==NULL&&p1!=NULL)
{
while(p1!=NULL)
98
printf(%s,st);
do
{
temp=(struct node *)malloc(size of(struct node));
printf(\n enter coeff);
scanf(%d,temp->coeff);
printf(\n enter exp);
scanf(%d,temp->exp);
temp->link=NULL;
if(*st==NULL)
{
*st=temp;
p=temp;
}
else
{
if(temp->exp>(*st)->exp)
{
temp->link=*st;
*st=temp;
}
else
{
t=*st;
while(temp->exp<=t->exp&&t!=NULL)
{
p=t;
t=t->link;
}
temp->link=p->link;
p->link=temp;
}
}
printf(\n do u wants more terms y/n);
ch=getch();
}
while(ch==Y||ch==Y);
}
100
OUTPUT
Input 1:
65
74
82
Input 2:
75
34
53
Polynomial expression 1:
6x^5+7x^4+8x^2
Polynomial expression 2:
7x^5+3x^4+5x^3
Output
13x^5+10x^4+5x^3+8x^2
101
Program No:21
102
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
char stack[50];
int top=-1;
void convrsn(char infix[]);
void push(char);
char pop();
void main()
{
char infix[40];
clrscr();
printf("\n Enter the infix expression:");
gets(infix);
convrsn(infix);
getch();
}
void push(char symb)
{
if(top>=49)
{
printf("\n stack overflow");
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("\n stack underflow");
getch();
return 0;
}
else
{
item=stack[top];
103
top--;
}
return item;
}
int preced(char ch)
{
if(ch==47)
return(5);
else if(ch==42)
return(4);
else if(ch==43)
return(3);
else
return(2);
}
void convrsn(char infix[])
{
static int index=0,pos=0;
int length;
char symbol,temp,postfix[40];
length=strlen(infix);
push('#');
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case '(':push(symbol);
break;
case ')':temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(preced(stack[top])>=preced(symbol))
{
104
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default :postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}
OUTPUT
Enter the infix expression:a+b*c/d
abcd/*+
105
Program No:22
T=CQ[FRONT]
IF(FRONT==REAR)
FRONT=REAR=-1
ELSE
FRONT=(FRONT+1)%MAX
PRINT DELETED ELEMENTS
IF(FRONT!=-1)
PRINT NOW FRONT ELEMENT IS ,CQ[FRONT]
DISPLAY()
STEP 05:STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 5
void insert();`
void del();
void display();
int cq[MAX],front=-1,rear=-1;
void main()
{
int c;
clrscr();
printf("\n\n\t\t---Array Implementation of Circular Queue---\n\n");
while(1)
{
printf("\n\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your Choice:\t") ;
scanf("%d",&c) ;
switch(c)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
107
break;
case 4:
exit();
default:
printf("Invalid Choice\n");
break;
}
}
}
void insert()
{
int n;
if(front==(rear+1)%MAX)
printf("\nCQ OVERFLOW\n");
else
{
printf("\nEnter an Item:");
scanf("%d",&n);
if(front==-1)
front++;
rear=(rear+1)%MAX;
cq[rear]=n;
printf("\n%d is stored...Now REAR element is: %d\n",n,cq[rear]);
display();
}
}
void display()
{
int i;
if(front==-1)
printf("\nCQ Empty...No elements to Display\n");
else
{
printf("\nCQ Elements\n-----------\n");
for(i=front;i<=rear;i++)
printf("%d\n",cq[i]);
if(front>rear)
{
for(i=front;i<MAX;i++)
printf("%d\n",cq[i]);
for(i=0;i<=rear;i++)
printf("%d\n",cq[i]);
}
108
}
}
void del()
{
int t;
if(front==-1)
printf("\nCQ Empty....No elements to delete\n");
else
{
t=cq[front];
if(front==rear)
front=rear=-1;
else
+front=(front+1)%MAX;
printf("\n%d is deleted...",t);
if(front!=-1)
printf("Now FRONT element is: %d\n",cq[front]);
display();
}
}
109
OUTPUT
---Array Implementation of Circular Queue--1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:
Enter an Item:12
12 is stored...Now REAR element is: 12
CQ Elements
----------12
1.Insert
2.Delete
3.Display
4.Exit
Enter an Item:14
14 is stored...Now REAR element is: 14
CQ Elements
----------12
14
1.Insert
2.Delete
3.Display
4.Exit
Enter an Item:56
56 is stored...Now REAR element is: 56
CQ Elements
----------12
14
110
56
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:
1.Insert
2.Delete
3.Display
4.Exit
CQ Elements
----------14
56
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:
111
Program No:23
112
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 10
typedef struct dequeue
{int front,rear;
int arr[MAX];
}dq;
/*If flag is zero, insertion is done at beginning
else if flag is one, insertion is done at end.
*/
void enqueue(dq *q,int x,int flag)
{
int i;
if(q->rear==MAX-1)
{printf("Queue overflow\n");
exit(1);
}
if(flag==0)
{
for(i=q->rear;i>=q->front;i--)
q->arr[i+1]=q->arr[i];
q->arr[q->front]=x;
q->rear++;
}
else if(flag==1)
{q->arr[++q->rear]=x;
}
else
{printf("Invalid flag value\n");
return;
}
}
void de_queue(dq *q,int flag)
{int i;
/*front is initialized with zero, then rear=-1
indicates underflow*/
if(q->rear<q->front)
{printf("\nQueue Underflow");
113
exit(1);
}
if(flag==0)/*deletion at beginning*/
{for(i=q->front;i<=q->rear;i++)
q->arr[i]=q->arr[i+1];
q->arr[q->rear]=0;
q->rear;
}
else if(flag==1)
{q->arr[q->rear--]=0;
}
else
{printf("\nInvalid flag value\n");
return;
}
}
void display(dq *q)
{
int i;
for(i=q->front;i<=q->rear;i++)
printf("%d\t",q->arr[i]);
}
void main()
{
dq q;
int ch,num;
q.front=0;
q.rear=-1;
clrscr();
while(1)
{
printf("\nMenu-Double Ended Queue\n------------------------------------------");
printf("\n\n1. Insert Begin");
printf("\n2. Insert End");
printf("\n3. Delete Begin");
printf("\n4. Delete End");
printf("\n5. Display");
printf("\n6. Exit");
printf("\n\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
114
case 1:
printf("\nEnter the number");
scanf("%d",&num);
enqueue(&q,num,0);
break;
case 2:
printf("\nEnter the number");
scanf("%d",&num);
enqueue(&q,num,1);
break;
case 3:
printf("\nDeleting element from beginning");
de_queue(&q,0);
break;
case 4:
printf("\nDeleting element from end");
de_queue(&q,1);
break;
case 5:
printf(DE QUEUE ELEMENTS\n);
display(&q);
break;
case 6:
exit(0);
default:
printf("\nInvalid Choice");
}
getch();
}
}
115
OUTPUT
Menu-Double Ended Queue
--------------------------------------1. Insert Begin
2. Insert End
3. Delete Begin
4. Delete End
5. Display
6. Exit
Enter your choice
1
Enter the number
10
Menu-Double Ended Queue
-------------------------------------1. Insert Begin
2. Insert End
3. Delete Begin
4. Delete End
5. Display
6. Exit
Enter your choice
1
Enter the number
20
Menu-Double Ended Queue
--------------------------------------1. Insert Begin
2. Insert End
3. Delete Begin
4. Delete End
5. Display
6. Exit
Enter your choice
5
DE QUEUE ELEMENTS
20
10
Menu-Double Ended Queue
-----------------------------------1. Insert Begin
2. Insert End
3. Delete Begin
4. Delete End
116
5. Display
6. Exit
Enter your choice
Enter the number
2
25
4. Delete End
5. Display
6. Exit
Enter your choice
5
DE QUEUE ELEMENTS
10
118
Program No:24
119
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *ptr,i,n;
clrscr();
printf("\nREVERSE ORDER OF QUEUE
ELEMENTS\n);
printf("Enter no. of elements\n");
scanf("%d",&n);
ptr=(int *)malloc(sizeof(int)*n);
if(ptr==NULL)
{
printf("Not enough memory\n");
exit(1);
}
for(i=0;i<n;i++)
{
printf("Enter %d element\n",i+1);
scanf("%d",&ptr[i]);
}
printf("Array in original order\n");
for(i=0;i<n;i++)
{
printf("%d\n",ptr[i]);
}
printf("Array in reverse order\n");
for(i=n-1;i>=0;i--)
{
printf("%d\n",ptr[i]);
}
getch();
}
120
OUTPUT
REVERSE ORDER OF QUEUE ELEMENTS
Enter no. of elements
5
Enter 1 element
1
Enter 2 element
2
Enter 3 element
3
Enter 4 element
4
Enter 5 element
5
Array in original order
1
2
3
4
5
Array in reverse order
5
4
3
2
1
121
Program No:25
BINARY TREE
AIM
Write a program to implement binary tree.
ALGORITHM
CREATE_TREE(INFO,NODE)
WHERE INFO -> INFORMATION FOR WHICH WE HAVE TO CREATE NODE
NODE -> STRUCTURE TYPE VARIABLES TO POINT BOTH LEFT AND RIGHT CHILD
STEP 1 : [ CHECK WHETHER THE TREE IS EMPTY ]
IF NODE == NULL
NODE = CREATE A NODE
LEFT_CHILD[NODE]=NULL
RIGHT_CHILD[NODE]=NULL
STEP 2 : [TEST FOR THE LEFT CHILD ]
IF INFO[NODE]>=INFO
LEFT_CHILD[NODE]=CALL CREATE_TREE
(INFO,LEFT_CHILD[NODE])
ELSE
RIGHT_CHILD[NODE]= CALL CREATE_TREE
(INFO,RIGHT_CHILD[NODE])
STEP 3 : [INORDER TRAVERSAL]
IF TREE != NULL
INORDER(TREE->LEFT)
PRINT TREE->INFO
INORDER(TREE->RIGHT)
STEP 4 : [PREORDER TRAVERSAL]
IF TREE != NULL
PRINT TREE->INFO
PREORDER(TREE->LEFT)
PREORDER(TREE->RIGHT)
122
123
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node * right, * left;
};
void insert(node **tree, int val)
{
node *temp = NULL;
if(*tree==NULL)
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
}
else if(val<(*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node *tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node *tree)
{
if (tree)
{
print_inorder(tree->left);
124
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node *tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void search(node **tree, int val)
{
if(!(*tree))
printf("Searched element do not exist\n");
else if(val < (*tree)->data)
search(&((*tree)->left), val);
else if(val > (*tree)->data)
search(&((*tree)->right), val);
else if(val == (*tree)->data)
printf("Searched element %d exist\n",(*tree)->data);
}
void main()
{
clrscr();
node *root,*t;
char ans = 'N';
int s,val,n,i,ch;
root = NULL;
do
{
printf("\t\t\t BINARY TREE \n");
printf("1.INSERTION \n 2.RECURSIVE TRAVERSALS\n 3.SEARCH \n4. EXIT\n");
printf("ENTER THE CHOICE\t\t\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
do
{
printf ("\nINSERTION \n");
125
126
OUTPUT
BINARY TREE
1.INSERTION
2.RECURSIVE TRAVERSALS
3.SEARCH
4.EXIT
Enter the choice 1
INSERTION
Enter the nodes
5
Want to enter More Elements?(y/n)
INSERTION
Enter the nodes
7
Want to enter More Elements?(y/n)
BINARY TREE
1.INSERTION
2.RECURSIVE TRAVERSALS
3.SEARCH
4.EXIT
Enter the choice 2
PRE ORDER DISPLAY
127
5
7
IN ORDER DISPLAY
5
7
POST ORDER DISPLAY
7
5
BINARY TREE
1.INSERTION
2.RECURSIVE TRAVERSALS
3.SEARCH
4.EXIT
Enter the choice 3
Enter the node to be searched 0
Searched element does not exist
BINARY TREE
1.INSERTION
2.RECURSIVE TRAVERSALS
3.SEARCH
4.EXIT
Enter the choice 4
EXIT
128
Program No:26
129
130
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
voidinit();
int empty();
int insert(int);
voidpreorder(struct node *);
voidpostorder(struct node *);
voidinorder(struct node *);
voidprinttree(int);
voidinit()
{
root=(struct node *)malloc(sizeof(struct node));
root=NULL;
}
int empty()
{
return(root==NULL);
}
int insert(int x)
{
struct node *p,*prev,*cur;
p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{
printf("\nOut of memory");
return 0;
}
p->data=x;
p->left=NULL;
p->right=NULL;
if(root==NULL)
{
root=p;
return 1;
131
}
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(p->data<cur->data)
cur=cur->left;
else
cur=cur->right;
}
if(p->data<prev->data)
prev->left=p;
else
prev->right=p;
return 1;
}
voidprinttree(int x)
{
if(x==1)
preorder(root);
else if(x==2)
inorder(root);
else
postorder(root);
}
voidinorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->data);
inorder(p->right);
}
}
voidpreorder(struct node *p)
{
if(p!=NULL)
{
printf("%d\t",p->data);
preorder(p->left);
preorder(p->right);
}
}
132
134
OUTPUT
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
1
Enter the element to insert
30
Inserted successfully
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
1
Enter the element to insert
10
Inserted successfully
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
1
Enter the element to insert
40
Inserted successfully
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
1
Enter the element to insert
6
Inserted successfully
1:Insertion
2:Preorder
3:Inorder
135
4:Postorder
5:Exit
Enter your choice
1
Enter the element to insert
80
Inserted successfully
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
2
The preorder elements are
30 10 6 40 80
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
3
The inorder elements are
6 10 30 40 80
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
4
The postorder elements are
6 1 80 40 30
1:Insertion
2:Preorder
3:Inorder
4:Postorder
5:Exit
Enter your choice
5
136
Program No: 27
ASSIGN()
Input : Address of Top node, vale to be stored.
Output: Returns address of current Top node.
Steps:
1: Get TOP address and value to be stored
2: Create NODE pointer TEMP and allocate memory of NODE size
3: IF TEMP is NULL PRINT no memory ,RETURN TOP
ELSE SET TEMP->data to value, TEMP->link to TOP.
RETURN TEMP
End ASSIGN()
DISP()
Input : Address of top node
Output: Void
Steps
1: Get TOP addresss
2: IF TOP is NULL PRINT List Empty,RETURN
3: Create NODE pointer TEMP
3: Set TEMP=TOP;
4: While TEMP not NULL do step 5,6
5: PRINT TEPM->Data
6: Set TEMP to TEMP->Link
End DISP()
137
ODDEVEN()
Input : Address of TOP node
Output : void
Steps
1: Create NODE varables odd,even,temp and
set odd=NULL,even=NULL,temp=Top
2: While temp not NULL do Steps 3,4
3: If temp-> is even then
set even= assin(even,temp->data)
Else
set odd= assign(odd,temp->data)
4: Set temp= temp->Link
5: call disp(odd)
6: call disp(even)
End ODDEVEN()
CREATE()
Input :Gets null pointer
Output: Returns TOP
Steps
1: Create integer variable val
2: PRINT enter value
3: Store value in val;
4: Set TOP=assign(pointer,val);
5: RETURN TOP
End CREATE()
138
PROGRAM
#include<stdio.h>
#include<conio.h>
typedef struct nod{
int data;
struct nod *link;
}node;
node *front=NULL,*rear=NULL;
void assign(){
int val;
node *temp=(node*) malloc(sizeof(node));
if(temp==NULL){
printf("NO memory");
}else{
printf("\nEnter value");
scanf("%d",&val);
temp->data=val;
temp->link=NULL;
if(front==NULL&&rear==NULL){
front=temp;
rear=temp;
}else{
rear->link=temp;
rear=temp;
}
}
return;
}
void del(){
if(front==NULL)
return;
if(front==rear)
front=rear=NULL;
else
front=front->link;
}
void ser(){
int val,c=0;
node *temp=front;
if(temp==NULL){
139
printf("\n Q EMPTY");
getch();
return;
}
printf("\nEnter value:");
scanf("%d",&val);
while(temp!=NULL){
if(temp->data==val)
c++;
temp=temp->link;
}
if(c==0)
printf("\n %d not found",val);
else
printf("\n %d found %d times",val,c);
getch();
return;
}
void disp(){
node *temp=front;
if(front==NULL&&rear==NULL) {
printf("\nQ EMPTY\n");
getch();
return;
}
while(temp!=NULL){
printf(" %d",temp->data);
temp=temp->link;
}
getch();
}
void menu(){
clrscr();
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t1.insert\n\t2.Display\n\t3.delete\n\t4.Search\n\t5.Exit");
printf("\nEnter choice:");
}
main(){
char j;
140
do{
menu();
scanf("%c",&j);
switch(j){
case '1':assign();
break;
case '2':disp();
break;
case '3':del();
break;
case '4':ser();
break;
}
}while(j!='5');
return 0;
}
141
OUTPUT
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.insert
2.Display
3.delete
4.Search
5.Exit
Enter choice:1
Enter value12
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.insert
2.Display
3.delete
4.Search
5.Exit
Enter choice:1
Enter value23
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.insert
2.Display
3.delete
4.Search
5.Exit
Enter choice:1
Enter value17
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.insert
2.Display
3.delete
4.Search
5.Exit
Enter choice:1
Enter value64
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.insert
2.Display
3.delete
4.Search
5.Exit
Enter choice: 4
142
Enter value:17
17 found 1 times
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.insert
2.Display
3.delete
4.Search
5.Exit
Enter choice: 5
143