Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
int i ;
printf ( "Enter the names and salaries of employees\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
scanf ( "%s%f", e[i].n, &e[i].s ) ;
}
Ans.
No Compile time error.
On Execution,scanf : floating point formats not linked
Abnormal program termination
This error occurs because the floating point emulator is not initialized and
generally happens when we access the float member of the structure before
any float variable.
To avoid this error initialize the floating point emulator by the following
code fragment :
extern void _floatconvert( );
#pragma extref _floatconvert
-------------------------------------------------------------------------------4.What would be the output of the following:
main( )
{
int a[3][4] = { 1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0 } ;
printf ( "\n %u %u", a + 1, &a + 1 ) ;
}
Ans.
If the base address of array a is 65000 then the output will be
65008 65024
The Address Calculation of a two dimensional array is as follows :
base + index1 * dim2 * sizeof ( type ) + index2 * sizeof ( type )
a + 1 is same as &( a + 1 ) and interpreted as
65000 + 1 * 4 * 2 + 0 i.e 65008 in this case.
&a + 1 is same as (&a) + 1 and interpreted as
base address of the two dimensional array + 1 * sizeof ( a ) , which is
65000 + 1 * 24 ( rows * cols * sizeof ( type ) ) which evaluates to be 65024
-------------------------------------------------------------------------------5.Consider the following program segment.
main( )
{
int a = 5, b = 10 ;
P(a);
P(b);
}
Define the macro P such that the output of the program comes out to be
a=5
b = 10
Ans.
#define P( x ) printf( #x " = %d\n", x ) ;
-------------------------------------------------------------------------------6.Suppose you have written two functions f( ) and g( ). How would you add them
to the standard library?
Ans.
Suppose the two functions are written in a file 'myfunc.c'. Compile this
file to create its object file (.OBJ). On the command line execute the
following commands to add the function f( ) and g( ) into a library file
'mylib.lib':
tlib mylib.lib + myfunc.obj, mylib.lst
-------------------------------------------------------------------------------7. Suppose a function f( ) has been defined in file 'file1.h'. If we #include
this file twice a redefinition of function would occur. How would you avoid it?
Ans.
To avoid the redefiniation of the function, write the following code
at the beginning of the file.
#if(!(defined __file_h))
#define __file_h
/* function definitions */
#endif
-------------------------------------------------------------------------------8. Suppose your executable (.EXE ) files have been deleted accidently and if you
want to copy all executable files to the specified directory, you have to
recompile all the files. This process would be cumbersome one. How would you
compile all the files in a single pass.
Ans.
The Solution is to use the command-line compiler (TCC) to compile to .obj and
generate the .exe file. This file is generally found in C:\TC\BIN directory. Run the
TCC from the command prompt, a list of various options will be displayed.
Among the various options available use the following command to convert .c
files
to create its .obj and .exe files.
c:\tc\bin\tcc -ms -Lc:\tc\lib -Ic:\tc\include file.c
where
-ms
- memory model
-Lc:\tc\lib - Option to search library files
-Ic:\tc\include - Option to search include files
file.c
- a C file
To compile all the C files we have to rerun this command with different filnames
'file.c' . To avoid this write the filenames along with the path (if any) in a data file
and read this data file for the filenames and run the Turbo Command Compiler
(TCC) command as above repeatedly to convert into .exe files.
The following program accomplishes the above said procedure. Compile the
program and execute it from the command prompt:
#include <stdlib.h>
#include <stdio.h>
struct file
{
char fname[20] ;
};
int main ( void )
{
char fname[20], command[127], command1[] =
{ "c:\\tc\\bin\\tcc -ms -Lc:\\tc\\lib -Ic:\\tc\\include " } ;
FILE *fp ;
struct file s ;
fp = fopen ( "filelist.dat", "wb+" ) ;
if ( fp == NULL )
{
puts ( "\nUnable to Open the file" ) ;
exit( 1 ) ;
}
dump_files( fp ) ;
strcpy ( command, command1 ) ;
f( int c )
{
int i, num ;
va_list p ;
va_start ( p, c ) ;
printf ( "\n %d", c ) ;
for ( i = 0 ; i < c ; i++ )
{
num = va_arg ( p, int ) ;
printf ( " %d", num ) ;
}
}
-------------------------------------------------------------------------------10. Write a program to reverse a given linked list.
Ans.
#include "alloc.h"
struct node
{
int data ;
struct node *link ;
};
reverse ( struct node ** ) ;
main( )
{
struct node *p ;
p = NULL ; /* empty linked list */
addatbeg ( &p, 1 ) ;
addatbeg ( &p, 2 ) ;
addatbeg ( &p, 3 ) ;
addatbeg ( &p, 4 ) ;
addatbeg ( &p, 5 ) ;
addatbeg ( &p, 6 ) ;
clrscr( ) ;
display ( p ) ;
printf ( "\nNo. of elements in the linked list = %d", count ( p ) ) ;
reverse ( &p ) ;
display ( p ) ;
printf ( "\nNo. of elements in the linked list = %d", count ( p ) ) ;
}
addatbeg ( struct node **q, int num )
{
struct node *temp ;
/* add new node */
temp = malloc ( sizeof ( struct node ) ) ;
temp -> data = num ;
temp -> link = *q ;
*q = temp ;
}
reverse ( struct node **x )
{
struct node *q, *r, *s ;
q = *x ;
r = NULL ;
/* traverse the enetire linked list */
while ( q != NULL )
{
s=r;
r=q;
q = q -> link ;
r -> link = s ;
}
*x = r ;
}
/* displays the contents of the linked list */
display ( struct node *q )
{
printf ( "\n" ) ;
/* traverse the entire linked list */
while ( q != NULL )
{
main( )
{
int i ;
struct emp e ;
FILE * fp ;
fp = fopen ( "emp.dat", "wb+" ) ;
if ( fp == NULL )
{
printf ( "Unable to open" ) ;
exit( ) ;
}
for ( i = 0 ; i < 10 ; i++ )
{
scanf ( "%s%d%d%d%f", e.name, &e.jd.dd, &e.jd.mm,
&e.jd.yy, &e.salary ) ;
fwrite ( &e, sizeof ( e ), 1, fp ) ;
}
fclose ( fp ) ;
}
/* This program sorts and displays data file */
#include "stdio.h"
struct date
{
int dd, mm, yy ;
};
struct emp
{
char name[10] ;
struct date jd ;
float salary ;
};
main( )
{
FILE *fp ;
struct emp e1, e2 ;
int i, j ,n = 10 ;
long pos ;
int fcmp_dates ( struct date *d1, struct date *d2 ) ;
clrscr( ) ;
fp = fopen ( "c:\\tc\\bin\\emp.dat", "rb+" ) ;
if ( fp == NULL )
{
printf ( "Unable to open" ) ;
exit( ) ;
}
for ( i = 0 ; i < 9 ; i++,n-- )
{
fread ( &e1, sizeof(e1), 1, fp ) ;
for ( j = 1 ; j < n ; j++ )
{
fseek ( fp, 0L, SEEK_CUR ) ;
fread ( &e2, sizeof(e2), 1, fp ) ;
if( fcmp_dates ( &e1.jd, &e2.jd ) > 0 )
{
fseek ( fp, -2L * sizeof( struct emp ), SEEK_CUR ) ;
fwrite ( &e2, sizeof ( e2 ), 1, fp ) ;
fwrite ( &e1, sizeof ( e1 ), 1, fp ) ;
}
else
e1 = e2 ;
}
rewind ( fp ) ;
}
rewind ( fp ) ;
printf ( "\nThe Sorted List is : \n" ) ;
while ( fread ( &e1, sizeof( e1 ), 1, fp ) )
printf ( "\n%s\t %d/%d/%d \t%7.2f", e1.name, e1.jd.dd, e1.jd.mm, e1.jd.yy,
e1.salary ) ;
fclose ( fp ) ;
}
fcmp_dates ( struct date *d1, struct date *d2 )
{
int x, y ;
x = ( d1->yy - 1980 ) * 512 + d1->mm * 32 + d1->dd ;
y = ( d2->yy - 1980 ) * 512 + d2->mm * 32 + d2->dd ;
return ( x - y ) ;
}
-------------------------------------------------------------------------------12. All possible combinations of 1, 2, 3 are 1 2 3, 1 3 2, ... These turn out to be 3!
combinations.
If we take 4 numbers, there would be 4! combinations. If we take n numbers, there would
be n combinations. If n is received from keyboard, write a program to generate and
print these combinations.
Ans.
#include "stdio.h"
#define MAX 10
main( )
{
int arr[MAX], begin[MAX], end[MAX], lastsub ;
unsigned long int n, i ;
printf ( "Enter any number" ) ;
scanf ( "%lu", &n ) ;
lastsub = n - 1 ;
for ( i = 0 ; i <= lastsub ; i++ )
{
begin[i] = 1 ;
end[i] = n ;
}
clrscr( ) ;
/* Explicitly nested for statements. The number of for's
would change depending upon the value of n */
for ( arr[0] = begin[0] ; arr[0] <= end [0] ; arr[0]++ )
{
for ( arr[1] = begin[1] ; arr[1] <= end[1] ; arr[1]++ )
{
for ( arr[2] = begin[2] ; arr[2] <= end [2] ; arr[2]++ )
work ( arr, lastsub ) ;
}
}
getch( ) ;
printf ( "\n\n" ) ;
/* Recursively nested for statements */
recfor ( arr, begin, end, 0, lastsub ) ;
}
recfor ( int *arr, int *bval, int *eval, int p, int lastsub )
{
arr[p] = bval[p] ;
while ( arr[p] <= eval[p] )
{
if ( p == lastsub )
work ( arr, lastsub ) ;
else
recfor ( arr, bval, eval, ( p + 1 ), lastsub ) ;
arr[p]++ ;
}
}
work ( int *arr, int lastsub )
{
int p, j, num ;
for ( p = 0 ; p <= lastsub ; p++ )
{
num = arr[p] ;
for ( j = p + 1 ; j <= lastsub ; j++ )
{
if ( num == arr[j] )
return ;
}
}
for ( p = 0 ; p <= lastsub ; p++ )
printf ( "arr[%d] = %d ", p, arr[p] ) ;
printf ( "\n" ) ;
getch( ) ;
}
-------------------------------------------------------------------------------13. Write a program to add two matrices of dimensions 3 x 4. Each element of the
matrices are complex numbers.
Ans.
#define ROW 3
#define COL 4
struct complex
{
double re ;
double im ;
};
main( )
{
struct complex a[ROW][COL] = {
10.9, 2.6, 5.3, 29.0, 3.8,4.8,
5.4, 6.5, 7.4, 8.6, 7.33, 2.9,
11.9, 12.6, 15.3, 129.0, 13.8,14.8,
51.4, 61.5, 17.4, 18.6, 71.33, 1
};
struct complex b[ROW][COL] = {
10.9, 29.0, 3.8,4.8, 4.5, 23.36,
5.4, 6.5,7.4, 8.6, 4.5, 7,98,
11.9, 21.6, 15.3, 12.0, 31.8,14.8,
15.4, 61.5, 71.4, 18.6, 71.33, 21.9
};
struct complex c[ROW][COL] ;
int i, j ;
clrscr( ) ;
for ( i = 0 ; i < ROW ;i++ )
{
for ( j = 0 ; j < COL ; j++ )
{
c[i][j].re = a[i][j].re + b[i][j].re ;
c[i][j].im = a[i][j].im + b[i][j].im ;
}
}
for ( i = 0 ; i < ROW ;i++ )
{
for ( j = 0 ; j < COL ; j++ )
{
printf (" %lf ", c[i][j].re ) ;
printf (" %lf ", c[i][j].im ) ;
}
printf ("\n" ) ;
}
getch( ) ;
}
r = r1 = 0 ;
break ;
case '-' :
r += opn[--top1] ;
if ( opr[top2-1] == '*' )
{
top2-- ;
r1 += opn[--top1] ;
r1 *= opn[--top1] ;
}
else if ( opr[top2-1] == '/' )
{
top2-- ;
r1 += opn[--top1] ;
r1 *= opn[--top1] ;
}
r -= r1 ;
result += r ;
r = r1 = 0 ;
break ;
case '*' :
r += opn[--top1] ;
r *= opn[--top1] ;
result += r ;
r=0;
break ;
case '/' :
r += opn[--top1] ;
r /= opn[--top1] ;
result += r ;
r=0;
break ;
}
}
printf ( "%d", result ) ;
}
-------------------------------------------------------------------------------17. A magic square is an n x n matrix, where each of the integers 1, 2, 3, ..., n2,
appear exactly once and the sums of every row, columnn and diagonal are equal.
It has been proved that for even values of n, no magic square exits. Here is a
magic square for n = 3:
|8|1|6|
|3|5|7|
|4|9|2|
The following is a procedure for constructing an n x n magic square for any
odd integer n.
Place initial number in the middle of the top row. Then afterr integer k has
been placed, move up one row and one column to the right to place the next
integer k + 1, unless one of the following occurs :
a)
If a move takes you above the top row in the jth column, move to the
bottom of the jth column and place the integer there.
b)
If a move takes you outside to the right of the square in the ith row,
place the integer in the ith row at the left side.
c)
If a move takes you to an already filled square or if you move out of the
square at the upper-right-hand corner, place k + 1 immediately below k.
Write a program to construct a magic square for any odd integer n.
Ans.
#include "conio.h"
int a[81] ;
main( )
{
int sr = 5, sc = 30, er, ec ;
int n, num, row, col, i, j, l, k, sum = 0 ;
printf ( "Enter the size of odd square matrix" ) ;
scanf ( "%d", &n ) ;
if ( !(n % 2) )
{
puts ( "Not an Odd number\n" ) ;
exit( ) ;
}
printf ( "Enter initial number of the series" ) ;
scanf ( "%d", &num ) ;
clrscr ( ) ;
gotoxy ( 35, 1 ) ;
er = sr + n * 2 ; /* ending row */
ec = sc + n * 3 ; /* ending column */
gotoxy ( sc, sr ) ;
printf ( "%c", 218 ) ;
gotoxy ( ec, er ) ;
printf ( "%c", 217 ) ;
gotoxy ( ec, sr ) ;
printf ( "%c", 191 ) ;
gotoxy ( sc, er ) ;
printf ( "%c", 192 ) ;
}
-------------------------------------------------------------------------------19. Accept two dates and the day of the first date from the keyboard. Write a
program to determine and print the day of the other date.
Ans.
main( )
{
struct date
{
int dd, mm, yy ;
} d1, d2, d3, d4 ;
int i, d, flag = 0, l = 0, diff ;
char ch, day[4], *days[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" } ;
printf ( "\nEnter today's date as dd-mm-yyyy : " ) ;
scanf ( "%d%*[-]%d%*[-]%d", &d1.dd, &d1.mm, &d1.yy ) ;
printf ( "\nEnter the day of the date ( first three letters ) : " ) ;
scanf ( "%s", day ) ;
d3 = d1 ;
for ( i = 0 ; i < 7 ; i++ )
{
if ( !strcmp ( day , days[i] ) )
{
d=i+1;
flag = 1 ;
break ;
}
}
if ( !flag )
printf ( "Invalid Day" ), exit( ) ;
do
{
printf ( "\nEnter the date, whose day is unknown as dd-mm-yyyy : " ) ;
scanf ( "%d%*[-]%d%*[-]%d", &d2.dd, &d2.mm, &d2.yy ) ;
printf ( "\nThe day is " ) ;
d=i+1;
diff = l = 0 ;
d1 = d3 ;
d4 = d2 ;
if ( d2.yy > d1.yy )
{
if ( d2.dd > d1.dd )
{
while ( d1.dd != d2.dd )
{
d1.dd++ ;
d++ ;
if ( d > 7 )
d -= 7 ;
}
}
else
{
while ( d1.dd != d2.dd )
{
d1.dd-- ;
d-- ;
if ( d <= 0 )
d += 7 ;
}
}
if ( d2.mm > d1.mm )
{
while ( d1.mm != --d2.mm )
{
switch ( d1.mm )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
d += 3 ;
if ( d > 7 )
d -= 7 ;
break ;
case 4 :
case 6 :
case 9 :
case 11 :
d += 2 ;
if ( d > 7 )
d -= 7 ;
break ;
}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d++ ;
if ( d > 7 )
d -= 7 ;
d1.mm++ ;
}
}
}
else
{
while ( d1.mm != d2.mm )
{
switch ( --d1.mm )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
d -= 3 ;
if ( d <= 0 )
d += 7 ;
break ;
case 4 :
case 6 :
case 9 :
case 11 :
d -= 2 ;
if ( d <= 0 )
d += 7 ;
break ;
}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d-- ;
if ( d <= 0 )
d += 7 ;
}
}
}
diff = d2.yy - d1.yy ;
if ( diff > 0 && d2.mm > 2 && leap ( d1.yy ) )
l--, d1.yy += 4 ;
else
{
if ( leap ( d1.yy ) && d4.mm < 2 )
l++ ;
while ( !leap ( d1.yy++ ) ) ;
d1.yy-- ;
}
if ( diff )
while ( d1.yy <= d2.yy )
l++ , d1.yy += 4 ;
if ( d4.mm < 2 && leap ( d4.yy ) )
l-- ;
diff += l ;
d += diff ;
while ( d >= 7 )
d -= 7 ;
if ( d <= 0 )
d += 7 ;
}
else
{
}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d++ ;
if ( d > 7 )
d -= 7 ;
}
d1.mm++ ;
}
}
else
{
while ( d1.mm != d2.mm )
{
switch ( --d1.mm )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
d -= 3 ;
if ( d <= 0 )
d += 7 ;
break ;
case 4 :
case 6 :
case 9 :
case 11 :
d -= 2 ;
if ( d <= 0 )
d += 7 ;
break ;
}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d-- ;
if ( d <= 0 )
d += 7 ;
}
}
}
diff = d1.yy - d2.yy ;
if ( diff > 0 && d4.mm > 2 && leap ( d1.yy ) )
l++, d1.yy -= 4 ;
else
{
if ( leap ( d1.yy ) && d4.mm < 2 )
l-- ;
if ( diff )
while ( !leap ( d1.yy++ ) ) ;
d1.yy-- ;
}
if ( diff )
while ( d2.yy <= d1.yy )
l++ , d1.yy -= 4 ;
if ( diff && d4.mm > 2 && leap ( d4.yy ) )
l-- ;
if ( diff )
{
diff += l ;
d -= diff ;
}
while ( d <= 0 )
d += 7 ;
}
printf ( "%sday", days[d-1] ) ;
printf ( "\nAny More Queries ( Y/N ) : " ) ;
ch = getche( ) ;
}while ( ch == 'y' || ch == 'Y' ) ;
}
leap ( int y )
{
return ( y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ) ;
}
20. Write a program which converts the numeric figures into the words.
e.g. 123 should be printed as , One Hundred Twenty Three.
Ans.
# include <ctype.h>
# include <stdio.h>
char *ot[3][9] = {
{ " One", " Two", " Three", " Four", " Five",
" Six", " Seven", " Eight", " Nine" },
{ " Ten", " Twenty", " Thirty", " Forty", " Fifty",
" Sixty", " Seventy", " Eighty", " Ninety" },
{ " Eleven", " Twelve", " Thirteen", " Fourteen",
" Fifteen"," Sixteen"," Seventeen"," Eighteen", "
Nineteen"}
};
char *a[5] = { " Hundred", " Thousand", " Lakhs", " Crore", " Arab" } ;
char result[250] = "" ;
char *t[50] ;
main( )
{
int i, j, ind = 0, c, r, pr = -1, e = 0 ;
unsigned long n ;
unsigned long q ;
clrscr( ) ;
printf ( "\nEnter a long unsigned number : " ) ;
scanf ( "%ld", &n ) ;
printf ( "%ld\n", n ) ;
q=n;
if ( n == 0 )
strcpy ( result, "Zero" ) ;
else
{
for( i = 0 ; q > 0 ; i++ )
{
if( ( i % 2 ) && i > 2 )
e++ ;
r = q % 10 ;
q /= 10 ;
if ( r != 0 )
{
if ( i == 0 )
t[++ind] = ot[i][r-1];
else if ( i == 1 )
{
if ( r == 1 && pr == 0 )
t[++ind] = ot[i][r-1] ;
else if ( r == 1 && pr > 0 )
t[--ind] = ot[2][r-1] ;
else
t[++ind] = ot[i][r-1] ;
}
else if ( i >= 2 )
{
if ( i == 2 )
{
t[++ind] = a[e] ;
t[++ind] = ot[0][r-1] ;
}
else
{
if ( i % 2 )
{
t[++ind] = a[e] ;
t[++ind] = ot[0][r-1] ;
}
else
{
if ( pr == 0 )
{
t[++ind] = a[e] ;
t[++ind] = ot[1][r-1] ;
}
else if ( r == 1)
t[ind] = ot[2][pr-1] ;
else
t[++ind] = ot[1][r-1] ;
}
}
}
}
pr = r ;
}
}/* End for */
for ( i = ind ; i >= 0 ; i-- )
strcat ( result, t[i] ) ;
printf( "\n%s", result ) ;
} /* End main */
-------------------------------------------------------------------------------21. Pascal triangle's elements are the Binomial coefficients and can be calculated
from the formula :
n!/((n-r)!! * r! ).
Write a program to print the pascal's triangle without using the formula or
arrays.
Ans.
main( )
{
int n, l, m = 1, f = 1, v, i, k, w, j ;
printf ( "Enter the number of rows required for pascal triangle " ) ;
scanf ( "%d", &n ) ;
printf( "%d\n", n ) ;
for( i = 1 ; i <= n ; i++ )
{
w = 5 + ( ( n-1 ) * 3 ) - ( ( i - 1 ) * 3 ) , m = 1 , f = 1 ;
for ( j = 1 ; j <= ( ( i / 2 ) + ( i % 2 ) ) ; j++ )
{
if ( i == 1 )
{
printf ( "%*d", w, j ) ;
break ;
}
else if( j == 1)
printf ( "%*d", w, j ), w = 6 ;
else
{
m = 1, f = 1 ;
for ( k = 1 ; k <= j-1 ; k++ )
{
m *= ( i - k ) ;
f *= k ;
}
printf ( "%*d", w, ( m / f ) ) ;
}
}
for( j = i / 2 ; j >= 1 ; j-- )
{
if ( i == 1)
break ;
else if ( j == 1 )
{
printf ( "%*d", w, j ) ;
break ;
}
else
{
m = 1, f = 1 ;
for ( k = j - 1 ; k >= 1 ; k-- )
m *= ( i - k ), f *= k ;
printf ( "%*d", w, ( m / f ) ) ;
}
} /*End of for loop*/
printf ( "\n" ) ;
} /*End of for loop*/
getch( ) ;
} /*End of main*/
-------------------------------------------------------------------------------22. Write a program to print the calendar, which is constraint to only 5 rows as :
January 2000
Sun Mon Tue Wed Thu Fri Sat
30 31
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
Ans.
static char *months[] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November",
"December"
};
main( )
{
static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;
long int ndays, ldays, tydays, tdays ;
int d, i, m, fday, y ;
char ch ;
clrscr() ;
#define MAX 10
struct btreenode
{
struct btreenode *leftchild ;
int data ;
struct btreenode *rightchild ;
};
main( )
{
struct btreenode *bt1, *bt2 ;
int req, i = 1, num ;
bt1 = NULL ; /* empty tree */
bt2 = NULL ; /* empty tree */
clrscr( ) ;
printf ( "Specify the number of data to be inserted : " ) ;
scanf ( "%d", &req ) ;
while ( i++ <= req )
{
printf ( "Enter the data : " ) ;
scanf ( "%d", &num ) ;
insert ( &bt1, num ) ;
}
i=1;
while ( i++ <= req )
{
printf ( "Enter the data : " ) ;
scanf ( "%d", &num ) ;
insert ( &bt2, num ) ;
}
clrscr( ) ;
if ( inorder ( bt1, bt2 ) )
printf ( "\nBoth the Binary Search trees are Equal" ) ;
}
/* inserts a new node in a binary search tree */
insert ( struct btreenode **sr, int num )
{
if ( *sr == NULL )
{
*sr = malloc ( sizeof ( struct btreenode ) ) ;
append ( &first, 1 ) ;
append ( &first, 2 ) ;
append ( &first, 3 ) ;
append ( &second, 1 ) ;
append ( &second, 2 ) ;
append ( &second, 3 ) ;
clrscr( ) ;
if ( compare ( first, second ) )
printf ( "Both linked lists are EQUAL" ) ;
else
printf ( " Linked lists are DIFFERENT" ) ;
}
/* adds a node at the end of a linked list */
append ( struct node **q, int num )
{
struct node * temp ;
temp = *q ;
if ( *q == NULL ) /* if the list is empty, create first node */
{
*q = malloc ( sizeof ( struct node ) ) ;
temp = *q ;
}
else
{
/* go to last node */
while ( temp -> link != NULL )
temp = temp -> link ;
/* add node at the end */
temp -> link = malloc ( sizeof ( struct node ) ) ;
temp = temp -> link ;
}
/* assign data to the last node */
temp ->data = num ;
temp -> link = NULL ;
}
/* compares 2 linked lists and returns 1 if linked lists are equal and 0 if
unequal */
compare ( struct node *q, struct node *r )
{
static int flag ;
if ( ( q == NULL ) && ( r == NULL ) )
flag = 1 ;
else
{
if ( q == NULL || r == NULL )
flag = 0 ;
if ( q -> data != r-> data )
flag = 0 ;
else
compare ( q -> link, r -> link ) ;
}
return flag ;
}