Ds File
Ds File
Program Code:
//To insert in the array
#include<stdio.h>
#include<conio.h>
int a[100],n;
void insertbeg()
{
int i,key;
for(i=0;i<n;i++)
a[i+1]=a[i];
printf("\nEnter the new element: ");
scanf("%d",&key);
a[0]=key;
n+=1;
}
void insert()
{
int i,key,loc;
printf("\nEnter the new element and the location: ");
scanf("%d%d",&key,&loc);
for(i=loc;i<n;i++)
a[i+1]=a[i];
a[loc]=key;
n+=1;
}
void insertend()
{
int key;
printf("\nEnter the new element: ");
scanf("%d",&key);
a[n]=key;
n+=1;
}
void main()
{
int i,ch;
char c;
clrscr();
printf("\nEntre the size of the array: ");
scanf("%d",&n);
printf("\nEnter the elements in the array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
c='y';
while(c=='Y'||c=='y')
{
printf("\nMain Menu\n");
printf("\n1.Insertion in the Beginning of the list\n2.Insertion at a specific
place\n3.Insertion in the end of the list\n4.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertbeg();
break;
case 2:
insert();
break;
case 3:
insertend();
break;
case 4:
exit(0);
break;
default: printf("\nYou entered the wrong choice: ");
}
printf("\nCurrent list status: ");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\nDo you want to enter more: ");
scanf("%c",c);
}
getch();
}
Output
Entre the size of the array: 4
Main Menu
Program Code:
//To Delete from the array
#include<stdio.h>
#include<conio.h>
int a[100],n;
void deletebeg()
{
int i;
for(i=0;i<n;i++)
a[i]=a[i+1];
a[n]=NULL;
n-=1;
}
void deletem()
{
int i,loc;
printf("\nEnter the location: ");
scanf("%d",&loc);
for(i=loc;i<n;i++)
a[i]=a[i+1];
a[n]=NULL;
n-=1;
}
void deleteend()
{
a[n]=NULL;
n-=1;
}
void main()
{
int i,ch;
char c;
clrscr();
printf("\nEntre the size of the array: ");
scanf("%d",&n);
printf("\nEnter the elements in the array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
c='y';
while(c=='Y'||c=='y')
{
printf("\nMain Menu\n");
printf("\n1.deletion in the Beginning of the list\n2.deletion at a specific
place\n3.deletion in the end of the list\n4.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
deletebeg();
break;
case 2:
deletem();
break;
case 3:
deleteend();
break;
case 4:
exit(0);
break;
default:
printf("\nYou entered the wrong choice: ");
}
printf("\nCurrent list status: ");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\nDo you want to delete more: ");
scanf("%c",c);
}
getch();
}
Output
Main Menu
1.deletion in the Beginning of the list
2.deletion at a specific place
3.deletion in the end of the list
4.Exit1
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int array[5]; // An array of integers.
int length = 5; // Lenght of the array.
int i, j;
int firstelement, temp;
clrscr();
//Some input
for (i = 0; i < length; i++)
{
printf("Enter a number: ");
scanf("%d",& array[i])
}
//Algorithm
for (i= length - 1; i > 0; i--)
{
firstelement = 0;
for (j=1; j<=i; j++)
{
if (array[j] < array[firstelement])
firstelement = j;
}
temp = array[firstelement];
array[firstelement] = array[i];
array[i] = temp;
}
//Some output
for (i = 0; i < 5; i++)
{
printf("%d ",array[i]);
}
getch();
}
Output
Enter a number: 5
Enter a number: 4
Enter a number: 7
Enter a number: 10
Enter a number: 2
10 7 5 4 2
Program-4
Program Code:
//RADIX SORT
#include<stdio.h>
#include<conio.h>
int a[20];
void radix(int n,int time)
{
int b[10][20],q,i,j,m,r,c;
for(i=0;i<10;i++)
for(j=0;j<20;j++)
b[i][j]=0;
j=0;
for(m=0;m<n;m++)
{
switch(time)
{
case 1:
r=a[m]%10;
break;
case 2:
printf("\na[m] %d",a[m]);
q=a[m]/10;
r=q%10;
printf("\nr %d",r);
break;
case 3:
q=a[m]/100;
r=q%10;
break;
case 4:
r=a[m]/1000;
break;
}
c=1;
j=0;
switch(r)
{
case 0:
while(c)
{
if(b[0][j]==0)
{
b[0][j]=a[m];
c=0;
}
else
j++;
}
break;
case 1:
while(c)
{
if(b[1][j]==0)
{
b[1][j]=a[m];
c=0;
}
else
j++;
}
break;
case 2:
while(c)
{
if(b[2][j]==0)
{
b[2][j]=a[m];
c=0;
}
else
j++;
}
break;
case 3:
while(c)
{
if(b[3][j]==0)
{
b[3][j]=a[m];
c=0;
}
else
j++;
}
break;
case 4:
while(c)
{
if(b[4][j]==0)
{
b[4][j]=a[m];
c=0;
}
else
j++;
}
break;
case 5:
while(c)
{
if(b[5][j]==0)
{
b[5][j]=a[m];
c=0;
}
else
j++;
}
break;
case 6:
while(c)
{
if(b[6][j]==0)
{
b[6][j]=a[m];
c=0;
}
else
j++;
}
break;
case 7:
while(c)
{
if(b[7][j]==0)
{
b[7][j]=a[m];
c=0;
}
else
j++;
}
break;
case 8:
while(c)
{
if(b[8][j]==0)
{
b[8][j]=a[m];
c=0;
}
else
j++;
}
break;
case 9:
while(c)
{
if(b[9][j]==0)
{
b[9][j]=a[m];
c=0;
}
else
j++;
}
break;
}
}
m=0;
for(i=0;i<10;i++)
{
for(j=0;j<n;j++)
{
if(b[i][j])
{
a[m]=b[i][j];
m++;
}
}
}
/*printf("\nThe sorted array: ");
for(i=0;i<n;i++)
printf("%d \t",a[i]);*/
}
void main()
{
int i,n,c,max,times,j;
clrscr();
printf("\nEnter the size of the array: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<n;i++)
if(max<a[i])
max=a[i];
times=0;
c=max;
while(c)
{
if(c%10)
{
times++;
c=c/10;
}
}
for(j=1;j<=times;j++)
radix(n,j);
Output
a[m] 53
r5
a[m] 23
r2
a[m] 46
r4
a[m] 46
r4
a[m] 89
r8
a[m] 99
r9
The sorted array: 23 46 46 53 89 99
Program-5
Bubble Sort
Program code:
//program to sort n number using bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp,k;
clrscr();
printf("Enter 10 numbers");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
{
for(j=0;j<9-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nPrinting the sorted array");
for(k=0;k<10;k++)
printf("\n%d",a[k]);
getch();
}
Output
Enter 10 numbers56
46
13
28
79
49
53
82
59
76
Merge Sort
Program Code:
//Merge Sort
#include<stdio.h>
#include<conio.h>
mergesort(int a[], int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return(0);
}
Output
Enter 10 numbers12
23
34
45
56
67
78
89
90
26
Quick Sort
Program code:
//Quick Sort
#include<stdio.h>
#include<conio.h>
int partition(int a[],int p,int r)
{ int x,i,j,temp;
x=a[r];
i=p-1;
for(j=p;j<r-1;j++)
{
if(a[j]<=x)
{
i++;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return (i+1);
}
void quick(int a[],int p,int r)
{
int q;
if(p<r)
{
q=partition(a,p,r);
quick(a,p,q-1);
quick(a,q+1,r);
}
}
void main()
{
int a[100],n,p,r,i;
clrscr();
printf("\nEnter the size of the array: ");
scanf("%d",&n);
printf("\nEnter the elements in the array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=0;
r=n-1;
quick(a,p,r);
printf(“\nThe sorted array is: ”);
for(i=0;i<n;i++)
printf(“%d ”,a[i]);
getch();
}
Output
void main( )
{
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i, num ;
clrscr( ) ;
if ( i == 10 )
printf ( "Number is not present in the array." ) ;
else
printf ( "The number is at position %d in the array.", i ) ;
getch( ) ;
}
OUTPUT:
Enter number to search:25
void main( )
{
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i, num ;
clrscr( ) ;
getch( ) ;
}
OUTPUT:
Enter number to search:25
The number is at position 5 in the array._
Program 10
1. Add at beginning
2. Append
3. Add at position
4. Delete from beginning
5. Delete from end
6. Delete from position
7. display
8. exit
enter your choice>> 7
12 34
1. Add at beginning
2. Append
3. Add at position
4. Delete from beginning
5. Delete from end
6. Delete from position
7. display
8. exit
enter your choice>> _
Program 12
Program to implement all operations on a circular linked list
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
Void main( )
{
struct node *front, *rear ;
clrscr( ) ;
*r = q ;
( *r ) -> link = *f ;
}
/* if queue is empty */
if ( *f == null )
printf ( "queue is empty" ) ;
else
{
if ( *f == *r )
{
item = ( *f ) -> data ;
free ( *f ) ;
*f = null ;
*r = null ;
}
else
{
/* delete the node */
q = *f ;
item = q -> data ;
*f = ( *f ) -> link ;
( *r ) -> link = *f ;
free ( q ) ;
}
return ( item ) ;
}
return null ;
}
q = q -> link ;
p=f;
}
}
Output:
Before deletion :
10 17 18 530 15
After deletion:
5 30 15
Program 13
Program to implement all operations on a doubly linked list
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
Void main( )
{
struct dnode *p ;
d_append ( &p , 11 ) ;
d_append ( &p , 2 ) ;
d_append ( &p , 14 ) ;
d_append ( &p , 17 ) ;
d_append ( &p , 99 ) ;
clrscr( ) ;
d_display ( p ) ;
printf ( "\nno. Of elements in the dll = %d\n", d_count ( p ) ) ;
d_addatbeg ( &p, 33 ) ;
d_addatbeg ( &p, 55 ) ;
d_display ( p ) ;
printf ( "\nno. Of elements in the dll = %d\n", d_count ( p ) ) ;
d_addafter ( p, 4, 66 ) ;
d_addafter ( p, 2, 96 ) ;
d_display ( p ) ;
printf ( "\nno. Of elements in the dll = %d\n", d_count ( p ) ) ;
d_delete ( &p, 55 ) ;
d_delete ( &p, 2 ) ;
d_delete ( &p, 99 ) ;
d_display ( p ) ;
printf ( "\nno. Of elements in the dll = %d\n", d_count ( p ) ) ;
}
return c ;
}
Output:
11 2 14 17 99
No. Of elements in the dll = 5
55 33 11 2 14 17 99
No. Of elements in the dll =7
55 33 96 11 2 66 14 17 99
No. Of elements in the dll =9
66 96 11 66 14 17
No. Of elements in the dll =6
_
Program 14
Program to implement all operations on stack
1. Push 2. Pop
Program code:
#define max 10
#include<stdio.h>
#include<conio.h>
Int tos=-1;
Void main()
{
void push(int *,int);
void pop(int *);
void display(int *);
int arr[max];
clrscr();
push(arr,10);
push(arr,40);
push(arr,100);
push(arr,50);
pop(arr);
display(arr);
getch();
}
Void push(int *arr,int t)
{
tos++;
if(tos==max)
{
printf("stack full !!!!");
return;
}
else
*(arr+tos)=t;
}
Void pop(int *arr)
{
Printf("popped item : %d\n\n",arr[tos]);
Tos--;
If(tos==-1)
{
printf("stack empty !!!");
return;
}
}
Void display(int *arr)
{
int i;
printf("stack after operation :\n");
for(i=tos;i>=0;i--)
printf("%d\t",*(arr+i));
}
Output:
Popped item :50
Stack after operation :
100 40 10
Program 15
Program to implement infix to prefix conversion
Program code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define max 50
Struct infix
{
char target[max] ;
char stack[max] ;
char *s, *t ;
int top, l ;
};
Void main( )
{
struct infix q ;
char expr[max] ;
clrscr( ) ;
initinfix ( &q ) ;
getch( ) ;
}
while ( *( pq -> s ) )
{
if ( *( pq -> s ) == ' ' || *( pq -> s ) == '\t' )
{
pq -> s++ ;
continue ;
}
if ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
{
while ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
{
*( pq -> t ) = *( pq -> s ) ;
pq -> s++ ;
pq -> t-- ;
}
}
if ( *( pq -> s ) == ')' )
{
push ( pq, *( pq -> s ) ) ;
pq -> s++ ;
}
if ( *( pq -> s ) == '*' || *( pq -> s ) == '+' || *( pq -> s ) == '/' ||
*( pq -> s ) == '%' || *( pq -> s ) == '-' || *( pq -> s ) == '$' )
{
if ( pq -> top != -1 )
{
opr = pop ( pq ) ;
if ( *( pq -> s ) == '(' )
{
opr = pop ( pq ) ;
while ( opr != ')' )
{
*( pq -> t ) = opr ;
pq -> t-- ;
opr = pop ( pq ) ;
}
pq -> s++ ;
}
}
while ( pq -> top != -1 )
{
opr = pop ( pq ) ;
*( pq -> t ) = opr ;
pq -> t-- ;
}
pq -> t++ ;
}
/* returns the priotity of the operator */
Int priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
/* displays the prefix form of given expr. */
Void show ( struct infix pq )
{
while ( *( pq.t ) )
{
printf ( " %c", *( pq.t ) ) ;
pq.t++ ;
}
}
Output:
Stack is empty
#define max 50
Struct prefix
{
int stack[max] ;
int top, nn ;
char *s ;
};
Void main( )
{
struct prefix q ;
char expr[max] ;
clrscr( ) ;
initprefix ( &q ) ;
getch( ) ;
}
if ( p -> top == -1 )
{
printf ( "\nstack is empty." ) ;
return null ;
}
return data ;
}
/* if digit is encountered */
if ( isdigit ( *( p -> s ) ) )
{
p -> nn = *( p -> s ) - '0' ;
push ( p, p -> nn ) ;
}
else
{
/* if operator is encountered */
n1 = pop ( p ) ;
n2 = pop ( p ) ;
switch ( *( p -> s ) )
{
case '+' :
n3 = n1 + n2 ;
break ;
case '-' :
n3 = n1 - n2 ;
break ;
case '/' :
n3 = n1 / n2 ;
break ;
case '*' :
n3 = n1 * n2 ;
break ;
case '%' :
n3 = n1 % n2 ;
break ;
case '$' :
n3 = pow ( n1 , n2 ) ;
break ;
default :
printf ( "unknown operator" ) ;
exit ( 1 ) ;
}
push ( p, n3 ) ;
}
p -> s++ ;
}
}
Output:
Enter prefix expression to be evaluated :+2*3+45
Result is:29
Program 17
#define max 50
Struct infix
{
char target[max] ;
char stack[max] ;
char *s, *t ;
int top ;
};
Void main( )
{
struct infix p ;
char expr[max] ;
initinfix ( &p ) ;
clrscr( ) ;
getch( ) ;
}
while ( *( p -> s ) )
{
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
{
p -> s++ ;
continue ;
}
if ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
while ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
*( p -> t ) = *( p -> s ) ;
p -> s++ ;
p -> t++ ;
}
}
if ( *( p -> s ) == '(' )
{
push ( p, *( p -> s ) ) ;
p -> s++ ;
}
if ( *( p -> s ) == ')' )
{
opr = pop ( p ) ;
while ( ( opr ) != '(' )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
p -> s++ ;
}
}
*( p -> t ) = '\0' ;
}
Stack is empty .
The postfix expression is: ab+cdef*/$-
Program 18
Program to implement postfix evaluation
Program code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define max 50
Struct postfix
{
int stack[max] ;
int top, nn ;
char *s ;
};
Void main( )
{
struct postfix q ;
char expr[max] ;
clrscr( ) ;
initpostfix ( &q ) ;
getch( ) ;
}
if ( p -> top == -1 )
{
printf ( "\nstack is empty." ) ;
return null ;
}
return data ;
}
/* if digit is encountered */
if ( isdigit ( *( p -> s ) ) )
{
p -> nn = *( p -> s ) - '0' ;
push ( p, p -> nn ) ;
}
else
{
/* if operator is encountered */
n1 = pop ( p ) ;
n2 = pop ( p ) ;
switch ( *( p -> s ) )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '%' :
n3 = n2 % n1 ;
break ;
case '$' :
n3 = pow ( n2 , n1 ) ;
break ;
default :
printf ( "unknown operator" ) ;
exit ( 1 ) ;
}
push ( p, n3 ) ;
}
p -> s++ ;
}
}
Output:
Stack is empty.
Stack is empty .result is: 5
Program 19
Program to implement all operations on a queue
Program code:
#include <stdio.h>
#include <conio.h>
#define max 10
Void main( )
{
int arr[max] ;
int front = -1, rear = -1, i ;
clrscr( ) ;
getch( ) ;
}
( *prear )++ ;
arr[*prear] = item ;
if ( *pfront == -1 )
*pfront = 0 ;
}
if ( *pfront == -1 )
{
printf ( "\nqueue is empty." ) ;
return null ;
}
data = arr[*pfront] ;
arr[*pfront] = 0 ;
if ( *pfront == *prear )
*pfront = *prear = -1 ;
else
( *pfront )++ ;
return data ;
}
Output:
Queue is full.
Item deleted:23
Item deleted:9
Item deleted:11_
Program 20
Program to implement all operations on a circular queue
Program code:
#include <stdio.h>
#include <conio.h>
#define max 10
Void main( )
{
int arr[max] ;
int i, front, rear ;
clrscr( ) ;
getch( ) ;
}
if ( *prear == max - 1 )
*prear = 0 ;
else
( *prear )++ ;
arr[*prear] = item ;
if ( *pfront == -1 )
*pfront = 0 ;
}
if ( *pfront == -1 )
{
printf ( "\nqueue is empty." ) ;
return null ;
}
data = arr[*pfront] ;
arr[*pfront] = 0 ;
if ( *pfront == *prear )
{
*pfront = -1 ;
*prear = -1 ;
}
else
{
if ( *pfront == max - 1 )
*pfront = 0 ;
else
( *pfront )++ ;
}
return data ;
}
Output:
Elements in the circular queue:
14 22 13 -6 25 0 0 0 0 0
Item deleted: 14
Item deleted:22
#define max 10
Void main( )
{
int arr[max] ;
int front, rear, i, n ;
clrscr( ) ;
n = count ( arr ) ;
printf ( "\ntotal number of elements in deque: %d", n ) ;
n = count ( arr ) ;
printf ( "\ntotal number of elements in deque: %d", n ) ;
getch( ) ;
}
if ( *pfront == -1 )
{
*pfront = *prear = 0 ;
arr[*pfront] = item ;
return ;
}
if ( *prear != max - 1 )
{
c = count ( arr ) ;
k = *prear + 1 ;
for ( i = 1 ; i <= c ; i++ )
{
arr[k] = arr[k - 1] ;
k-- ;
}
arr[k] = item ;
*pfront = k ;
( *prear )++ ;
}
else
{
( *pfront )-- ;
arr[*pfront] = item ;
}
}
if ( *pfront == -1 )
{
*prear = *pfront = 0 ;
arr[*prear] = item ;
return ;
}
if ( *prear == max - 1 )
{
k = *pfront - 1 ;
for ( i = *pfront - 1 ; i < *prear ; i++ )
{
k=i;
if ( k == max - 1 )
arr[k] = 0 ;
else
arr[k] = arr[i + 1] ;
}
( *prear )-- ;
( *pfront )-- ;
}
( *prear )++ ;
arr[*prear] = item ;
}
if ( *pfront == -1 )
{
printf ( "\ndeque is empty.\n" ) ;
return 0 ;
}
item = arr[*pfront] ;
arr[*pfront] = 0 ;
if ( *pfront == *prear )
*pfront = *prear = -1 ;
else
( *pfront )++ ;
return item ;
}
if ( *pfront == -1 )
{
printf ( "\ndeque is empty.\n" ) ;
return 0 ;
}
item = arr[*prear] ;
arr[*prear] = 0 ;
( *prear )-- ;
if ( *prear == -1 )
*pfront = -1 ;
return item ;
}
Elements in a deque:
Front->6 5 28 -9 10 17 8 13 14 25<-rear
Total elements in a deque:
10
Item extracted: 6
Item extracted:5
Item extracted:28
Item extracted:-9
Elements in a deque after deletion :
Front->0 0 0 0 10 17 8 13 14 25<-rear
Elements in a deque after addition:
Front-> 0 0 10 17 8 13 14 25 16 7<-rear
Item extracted:7
Item extracted:16
Elements in a deque after deletion :
Front-> 0 0 10 17 8 13 14 25 0 0<-rear
Total elements in a deque:6
Program 22
Program to implement depth first search (dfs)
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#define true 1
#define false 0
#define max 8
Struct node
{
int data ;
struct node *next ;
};
Int visited[max] ;
Void main( )
{
struct node *arr[max] ;
struct node *v1, *v2, *v3, *v4 ;
int i ;
clrscr( ) ;
v1 = getnode_write ( 2 ) ;
arr[0] = v1 ;
v1 -> next = v2 = getnode_write ( 3 ) ;
v2 -> next = null ;
v1 = getnode_write ( 1 ) ;
arr[1] = v1 ;
v1 -> next = v2 = getnode_write ( 4 ) ;
v2 -> next = v3 = getnode_write ( 5 ) ;
v3 -> next = null ;
v1 = getnode_write ( 1 ) ;
arr[2] = v1 ;
v1 -> next = v2 = getnode_write ( 6 ) ;
v2 -> next = v3 = getnode_write ( 7 ) ;
v3 -> next = null ;
v1 = getnode_write ( 2 ) ;
arr[3] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 2 ) ;
arr[4] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 3 ) ;
arr[5] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 3 ) ;
arr[6] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 4 ) ;
arr[7] = v1 ;
v1 -> next = v2 = getnode_write ( 5 ) ;
v2 -> next = v3 = getnode_write ( 6 ) ;
v3 -> next = v4 = getnode_write ( 7 ) ;
v4 -> next = null ;
dfs ( 1, arr ) ;
getch( ) ;
}
printf ( "%d\t", v ) ;
q=*(p+v-1);
while ( q != null )
{
if ( visited[q -> data - 1] == false )
dfs ( q -> data, p ) ;
else
q = q -> next ;
}
}
while ( n != null )
{
temp = n -> next ;
free ( n ) ;
n = temp ;
}
}
Output:
1 2 4 8 5 6 3 7 _
Program 24
Program to implement breadth first search (bfs)
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#define true 1
#define false 0
#define max 8
Struct node
{
int data ;
struct node *next ;
};
Int visited[max] ;
Int q[8] ;
Int front, rear ;
Void main( )
{
struct node *arr[max] ;
struct node *v1, *v2, *v3, *v4 ;
int i ;
clrscr( ) ;
v1 = getnode_write ( 2 ) ;
arr[0] = v1 ;
v1 -> next = v2 = getnode_write ( 3 ) ;
v2 -> next = null ;
v1 = getnode_write ( 1 ) ;
arr[1] = v1 ;
v1 -> next = v2 = getnode_write ( 4 ) ;
v2 -> next = v3 = getnode_write ( 5 ) ;
v3 -> next = null ;
v1 = getnode_write ( 1 ) ;
arr[2] = v1 ;
v1 -> next = v2 = getnode_write ( 6 ) ;
v2 -> next = v3 = getnode_write ( 7 ) ;
v3 -> next = null ;
v1 = getnode_write ( 2 ) ;
arr[3] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 2 ) ;
arr[4] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 3 ) ;
arr[5] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 3 ) ;
arr[6] = v1 ;
v1 -> next = v2 = getnode_write ( 8 ) ;
v2 -> next = null ;
v1 = getnode_write ( 4 ) ;
arr[7] = v1 ;
v1 -> next = v2 = getnode_write ( 5 ) ;
v2 -> next = v3 = getnode_write ( 6 ) ;
v3 -> next = v4 = getnode_write ( 7 ) ;
v4 -> next = null ;
front = rear = -1 ;
bfs ( 1, arr ) ;
getch( ) ;
}
visited[v - 1] = true ;
printf ( "%d\t", v ) ;
addqueue ( v ) ;
while ( u != null )
{
if ( visited [ u -> data - 1 ] == false )
{
addqueue ( u -> data ) ;
visited [ u -> data - 1 ] = true ;
printf ( "%d\t", u -> data ) ;
}
u = u -> next ;
}
}
}
rear++ ;
q[rear] = vertex ;
if ( front == -1 )
front = 0 ;
}
Int deletequeue( )
{
int data ;
if ( front == -1 )
{
printf ( "\nqueue underflow." ) ;
exit( ) ;
}
data = q[front] ;
if ( front == rear )
front = rear = -1 ;
else
front++ ;
return data ;
}
Int isempty( )
{
if ( front == -1 )
return true ;
return false ;
}
Output:
1 2 3 4 5 6 7 8 _
Program 24
Program to multiply two matrices.
Program code:
#include<stdio.h>
#include<conio.h>
Void main()
{ int n,m,i,j,k;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("enter the number of rows and columns ¯ ");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d%d",&a[i][j],&b[i][j]);
}
}
For(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
printf("%d ",c[i][j]);
printf("\n");
}
getch();
}
Output :
Enter the number of rows and columns>>2 2
1 2
2 4
5 6
7 8
20 28
52 76
_
Program 24
Program to implement sorting of a linked list.
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
Void getdata( ) ;
Void append ( struct node **, int ) ;
Void displaylist( ) ;
Int count ( struct node * ) ;
Void selection_sort ( int ) ;
Void bubble_sort ( int ) ;
Void main( )
{
int n ;
getdata( ) ;
clrscr( ) ;
printf ( "linked list before sorting: " ) ;
displaylist( ) ;
n = count ( start ) ;
selection_sort ( n ) ;
printf ( "\nlinked list after selection sorting: " ) ;
displaylist( ) ;
getch( ) ;
getdata( ) ;
clrscr( ) ;
printf ( "linked list before sorting: " ) ;
displaylist( ) ;
n = count ( start ) ;
bubble_sort ( n ) ;
printf ( "\nlinked list after bubble sorting: " ) ;
displaylist( ) ;
getch( ) ;
}
Void getdata( )
{
int val, n ;
char ch ;
struct node *new ;
clrscr( ) ;
new = null ;
do
{
printf ( "\nenter a value: " ) ;
scanf ( "%d", &val ) ;
start = new ;
}
return c ;
}
p = start ;
for ( i = 0 ; i < n - 1 ; i++ )
{
q = p -> link ;
k=n;
for ( i = 0 ; i < n - 1 ; i++, k-- )
{
p = start ;
q = p -> link ;
Void main( )
{
struct node *p ;
p = null ; /* empty linked list */
addatbeg ( &p, 7 ) ;
addatbeg ( &p, 43 ) ;
addatbeg ( &p, 17 ) ;
addatbeg ( &p, 3 ) ;
addatbeg ( &p, 23 ) ;
addatbeg ( &p, 5 ) ;
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 ) ) ;
}
q = *x ;
r = null ;
*x = r ;
}
Output :
5 23 3 17 43 7
No. Of element in the linked list = 6
7 43 17 3 23 5
No. Of element in the linked list = 6
Program 26
Program to implement prim’s algorithm .
Program code:
#include <stdio.h>
Int n; /* the number of nodes in the graph */
Int weight[100][100];
Char intree[100];
Int d[100];
Int whoto[100];
Main() {
file *f = fopen("dist.txt", "r");
int i,j,total,treesize;
fscanf(f, "%d", &n);
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
fscanf(f, "%d", &weight[i][j]);
fclose(f);
Min = i;
/* and add it */
printf("adding edge %c-%c\n", whoto[min] + 'a', min + 'a');
intree[min] = 1;
total += d[min];
updatedistances(min);
}
Output:
C:\tc\bin>prism
Adding node a
Adding edge a-b
Adding edge a-c
Adding edge a-d
Adding edge a-e
Adding edge a-f
Adding edge a-g
Adding edge a-h
Adding edge a-i
Total distance i:135678
C:\tc\bin>
Program 27
Program to implement preorder, postorder and inorder traversal of a
binary tree .
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
Struct btreenode
{
struct btreenode *leftchild ;
int data ;
struct btreenode *rightchild ;
};
Void main( )
{
struct btreenode *bt ;
int req, i = 1, num ;
clrscr( ) ;
printf ( "specify the number of items to be inserted: " ) ;
scanf ( "%d", &req ) ;
/* print the data of the node whose leftchild is null or the path
has already been traversed */
printf ( "\t%d", sr -> data ) ;
Output:
Specify the number of items to be inserted:5
Enter the data:23
Enter the data:12
Enter the data:9
Enter the data:45
Enter the data:34
In-order traversal: 9 12 23 34 45
Pre-order traversal: 23 12 9 45 34
Post-order traversal: 9 12 34 45 23
Program 28
Program to implement kruskal’s algorithm.
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
Struct lledge
{
int v1, v2 ;
float cost ;
struct lledge *next ;
};
Int stree[5] ;
Int count[5] ;
Int mincost ;
Void main( )
{
struct lledge *temp, *root ;
int i ;
clrscr( ) ;
root -> v1 = 4 ;
root -> v2 = 3 ;
root -> cost = 1 ;
temp = root -> next = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ;
temp -> v1 = 4 ;
temp -> v2 = 2 ;
temp -> cost = 2 ;
temp -> next = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ;
getch( ) ;
}
Struct lledge * kminstree ( struct lledge *root, int n )
{
struct lledge *temp = null ;
struct lledge *p, *q ;
int noofedges = 0 ;
int i, p1, p2 ;
p1 = getrval ( p -> v1 ) ;
p2 = getrval ( p -> v2 ) ;
if ( p1 != p2 )
{
combine ( p -> v1, p -> v2 ) ;
noofedges++ ;
mincost += p -> cost ;
if ( temp == null )
{
temp = p ;
q = temp ;
}
else
{
q -> next = p ;
q = q -> next ;
}
q -> next = null ;
}
}
return temp ;
}
Output:
Stree[1] -> 4
Stree[2] -> 4
Stree[3] -> 4
Stree[4] -> 4
Nthe minimum cost of spanning tree is 7