0% found this document useful (0 votes)
123 views

Ds File

Program 1 Insertion in the beginning, at selected location, in the end. Program Code: //To insert in the array #include #include int a[100],n; void insertbeg() { int i,key; for(i=0;iEnter
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

Ds File

Program 1 Insertion in the beginning, at selected location, in the end. Program Code: //To insert in the array #include #include int a[100],n; void insertbeg() { int i,key; for(i=0;iEnter
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 85

Program 1

Insertion in the beginning, at selected location, in the end.

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

Enter the elements in the array: 24


25
26
27

Main Menu

1.Insertion in the Beginning of the list


2.Insertion at a specific place
3.Insertion in the end of the list
4.Exit1

Enter the new element: 98


Current list status: 98 24 24 24 24
Do you want to enter more:
Main Menu

1.Insertion in the Beginning of the list


2.Insertion at a specific place
3.Insertion in the end of the list
4.Exit2
Program 2

Deletion in the beginning, at selected location, in the end.

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

Entre the size of the array: 5

Enter the elements in the array: 46


78
59
46
23

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

Current list status: 78 59 46 23


Do you want to delete more:
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.Exit 4
Program 3

Selection Sort – in descending order

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

Radix Sort(Bucket Sort)

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);

printf("\nThe sorted array: ");


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

Output

Enter the size of the array: 6

Enter the elements: 46


53
23
89
99
46

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

Printing the sorted array


13 28 46 49 53 56 59 76 79 82
Program – 6

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);
}

merge(int a[], int low, int high, int mid)


{
int i, j, k, c[50];
i=low;
j=mid+1;
k=low;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=a[j];
k++;
j++;
}
}
while(i<=mid)
{
c[k]=a[i];
k++;
i++;
}
while(j<=high)
{
c[k]=a[j];
k++;
j++;
}
for(i=low;i<k;i++)
{
a[i]=c[i];
}
}
void main()
{
int a[100],n,p,r;
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;
merge_sort(a,p,r);
getch();
}

Output
Enter 10 numbers12
23
34
45
56
67
78
89
90
26

Printing the sorted array


12 23 26 34 45 56 67 78 89 90
Program 7

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

Enter the size of the array: 4

Enter the elements in the array: 56


89
47
79

The Sorted array:


47 56 79 89
Program 8
Program to implement linear search in an unsorted array
Program code:
#include <stdio.h>
#include <conio.h>

void main( )
{
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i, num ;

clrscr( ) ;

printf ( "Enter number to search: " ) ;


scanf ( "%d", &num ) ;

for ( i = 0 ; i <= 9 ; i++ )


{
if ( arr[i] == num )
break ;
}

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

The number is at position 5 in the array._


Program 9

Program to implement linear search in sorted array


Program code:
#include <stdio.h>
#include <conio.h>

void main( )
{
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
int i, num ;

clrscr( ) ;

printf ( "Enter number to search: " ) ;


scanf ( "%d", &num ) ;

for ( i = 0 ; i <= 9 ; i++ )


{
if ( arr[9] < num || arr[i] >= num )
{
if ( arr[i] == num )
printf ( "The number is at position %d in the array.", i ) ;
else
printf ( "Number is not present in the array." ) ;
break ;
}
}

getch( ) ;
}

OUTPUT:
Enter number to search:25
The number is at position 5 in the array._
Program 10

Program to implement shell sort


Program code:
#include<stdio.h>
#include<conio.h>

Void shell_sort(int array[],int size)


{ int temp,gap,i,ex_oc;
gap=size/2;
do{
do{
ex_oc=0;
for(i=0;i<size;i++)
{ if(array[i]>array[i+gap])
{
temp=array[i];
array[i]=array[i+gap];
array[i+gap]=temp;
ex_oc=1;
}
}
}while(ex_oc);
gap=gap/2;
}while(gap);
}
void main()
{int a[5]={2,10,9,6,11};
int i;
clrscr();
shell_sort(a,5);
printf("\n\nsorted list :\n");
for(i=0;i<5;i++)
printf("%d\t",a[i]);
getch();
}
Output:
Sorted list:
0 2 6 9 10 _
Program 11
Program to implement all operations on a linked list
Program code:
#include<stdio.h>
#include<alloc.h>
#include<process.h>
#include<conio.h>
Struct node{
Int num;
Struct node *link;
};
/* function prototype */
Void addbeg(struct node **,int);
Void append(struct node **,int);
Void addpos(struct node **,int);
Void delbeg(struct node **);
Void delend(struct node **);
Void delpos(struct node **);
Void display(struct node *);
/* end of function prototype */
Void main()
{
struct node *b;
int num,choice;
clrscr();
b=null;
while(1)
{ printf("1.add at begining");
printf("\n2. Append ");
printf("\n3.add at position");
printf("\n4.delete from begining");
printf("\n5.delete from end");
printf("\n6.delete from position");
printf("\n7. Display ");
printf("\n8. Exit ");

printf("\nenter your choice ¯ ");


scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the number ¯ ");
scanf("%d",&num);
addbeg(&b,num);
break;
case 2:printf("enter the number ¯ ");
scanf("%d",&num);
append(&b,num);
break;
case 3:printf("enter the number ¯ ");
scanf("%d",&num);
addpos(&b,num);
break;
case 4:printf("enter the number ¯ ");
scanf("%d",&num);
delbeg(&b);
break;
case 5:delend(&b);
break;
case 6:delpos(&b);
break;
case 7:display(b);
break;
case 8:exit(0);
}
}
getch();
}

Void addbeg(struct node **q,int num)


{
struct node *temp;
temp=*q;
temp=malloc(sizeof(struct node));
if(*q==null)
{
temp->num=num;
temp->link=null;
*q=temp;
}
else
{
temp->num=num;
temp->link=*q;
*q=temp;
}
clrscr();
}

Void append(struct node **q,int num)


{
struct node *temp,*r;
temp=*q;
if(*q==null)
{
temp=malloc(sizeof(struct node));
temp->num=num;
temp->link=null;
*q=temp;
}
else
{
while(temp->link!=null)
temp=temp->link;
r=malloc(sizeof(struct node));
r->num=num;
r->link=null;
temp->link=r;
}
clrscr();
}
Void addpos(struct node **q,int num)
{
struct node *temp,*r;
int loc,i;
temp=*q;
printf("\nenter the location ¯ ");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp=temp->link;
if(temp==null)
{
printf("not enough elements !!!");
return;
}
}
r=malloc(sizeof(struct node));
r->num=num;
r->link=temp->link;
temp->link=r;
clrscr();
}

Void delbeg(struct node **q)


{
struct node *temp;
temp=*q;
if(*q==null)
{
printf("node cannot be deleted !!!!");
return;
}
else
{
*q=temp->link;
free(temp);
}
clrscr();
}
Void delend(struct node **q)
{
struct node *temp,*old;
temp=*q;
while(temp!=null)
{old=temp;
temp=temp->link;
}
old->link=null;
free(temp);
clrscr();
}
Void delpos(struct node **q)
{
struct node *temp,*old;
int i,pos;
printf("enter the position ¯ ");
scanf("%d",&pos);
temp=*q;
for(i=0;i<pos;i++)
{
old->link=temp->link;
free(temp);
return;
}
clrscr();
}
Void display(struct node *q)
{
while(q!=null)
{
printf("%d ",q->num);
q=q->link;
}
}
Output:
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>> 2
Enter number >>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>> 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>

/* structure containing a data part and link part */


Struct node
{
int data ;
struct node * link ;
};

Void addcirq ( struct node **, struct node **, int ) ;


Int delcirq ( struct node **, struct node ** ) ;
Void cirq_display ( struct node * ) ;

Void main( )
{
struct node *front, *rear ;

front = rear = null ;

addcirq ( &front, &rear, 10 ) ;


addcirq ( &front, &rear, 17 ) ;
addcirq ( &front, &rear, 18 ) ;
addcirq ( &front, &rear, 5 ) ;
addcirq ( &front, &rear, 30 ) ;
addcirq ( &front, &rear, 15 ) ;

clrscr( ) ;

printf ( "before deletion:\n" ) ;


cirq_display ( front ) ;

delcirq ( &front, &rear ) ;


delcirq ( &front, &rear ) ;
delcirq ( &front, &rear ) ;

printf ( "\n\nafter deletion:\n" ) ;


cirq_display ( front ) ;
}

/* adds a new element at the end of queue */


Void addcirq ( struct node **f, struct node **r, int item )
{
struct node *q ;

/* create new node */


q = malloc ( sizeof ( struct node ) ) ;
q -> data = item ;

/* if the queue is empty */


if ( *f == null )
*f = q ;
else
( *r ) -> link = q ;

*r = q ;
( *r ) -> link = *f ;
}

/* removes an element from front of queue */


Int delcirq ( struct node **f, struct node **r )
{
struct node *q ;
int item ;

/* 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 ;
}

/* displays whole of the queue */


Void cirq_display ( struct node *f )
{
struct node *q = f, *p = null ;

/* traverse the entire linked list */


while ( q != p )
{
printf ( "%d\t", q -> data ) ;

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>

/* structure representing a node of the doubly linked list */


Struct dnode
{
struct dnode *prev ;
int data ;
struct dnode * next ;
};

Void d_append ( struct dnode **, int ) ;


Void d_addatbeg ( struct dnode **, int ) ;
Void d_addafter ( struct dnode *, int , int ) ;
Void d_display ( struct dnode * ) ;
Int d_count ( struct dnode * ) ;
Void d_delete ( struct dnode **, int ) ;

Void main( )
{
struct dnode *p ;

p = null ; /* empty doubly linked list */

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 ) ) ;
}

/* adds a new node at the end of the doubly linked list */


Void d_append ( struct dnode **s, int num )
{
struct dnode *r, *q = *s ;

/* if the linked list is empty */


if ( *s == null )
{
/*create a new node */
*s = malloc ( sizeof ( struct dnode ) ) ;
( *s ) -> prev = null ;
( *s ) -> data = num ;
( *s ) -> next = null ;
}
else
{
/* traverse the linked list till the last node is reached */
while ( q -> next != null )
q = q -> next ;

/* add a new node at the end */


r = malloc ( sizeof ( struct dnode ) ) ;
r -> data = num ;
r -> next = null ;
r -> prev = q ;
q -> next = r ;
}
}

/* adds a new node at the begining of the linked list */


Void d_addatbeg ( struct dnode **s, int num )
{
struct dnode *q ;

/* create a new node */


q = malloc ( sizeof ( struct dnode ) ) ;

/* assign data and pointer to the new node */


q -> prev = null ;
q -> data = num ;
q -> next = *s ;

/* make new node the head node */


( *s ) -> prev = q ;
*s = q ;
}

/* adds a new node after the specified number of nodes */


Void d_addafter ( struct dnode *q, int loc, int num )
{
struct dnode *temp ;
int i ;
/* skip to desired portion */
for ( i = 0 ; i < loc ; i++ )
{
q = q -> next ;
/* if end of linked list is encountered */
if ( q == null )
{
printf ( "\nthere are less than %d elements", loc );
return ;
}
}

/* insert new node */


q = q -> prev ;
temp = malloc ( sizeof ( struct dnode ) ) ;
temp -> data = num ;
temp -> prev = q ;
temp -> next = q -> next ;
temp -> next -> prev = temp ;
q -> next = temp ;
}

/* displays the contents of the linked list */


Void d_display ( struct dnode *q )
{
printf ( "\n" ) ;

/* traverse the entire linked list */


while ( q != null )
{
printf ( "%2d\t", q -> data ) ;
q = q -> next ;
}
}

/* counts the number of nodes present in the linked list */


Int d_count ( struct dnode * q )
{
int c = 0 ;

/* traverse the entire linked list */


while ( q != null )
{
q = q -> next ;
c++ ;
}

return c ;
}

/* deletes the specified node from the doubly linked list */


Void d_delete ( struct dnode **s, int num )
{
struct dnode *q = *s ;

/* traverse the entire linked list */


while ( q != null )
{
/* if node to be deleted is found */
if ( q -> data == num )
{
/* if node to be deleted is the first node */
if ( q == *s )
{
*s = ( *s ) -> next ;
( *s ) -> prev = null ;
}
else
{
/* if node to be deleted is the last node */
if ( q -> next == null )
q -> prev -> next = null ;
else
/* if node to be deleted is any intermediate node */
{
q -> prev -> next = q -> next ;
q -> next -> prev = q -> prev ;
}
free ( q ) ;
}
return ; /* return back after deletion */
}
q = q -> next ; /* go to next node */
}
printf ( "\n%d not found.", num ) ;
}

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 initinfix ( struct infix * ) ;


Void setexpr ( struct infix *, char * ) ;
Void push ( struct infix *, char ) ;
Char pop ( struct infix * ) ;
Void convert ( struct infix * ) ;
Int priority ( char c ) ;
Void show ( struct infix ) ;

Void main( )
{
struct infix q ;
char expr[max] ;

clrscr( ) ;

initinfix ( &q ) ;

printf ( "\nenter an expression in infix form: " ) ;


gets ( expr ) ;

setexpr ( &q, expr ) ;


convert ( &q ) ;

printf ( "the prefix expression is: " ) ;


show ( q ) ;

getch( ) ;
}

/* initializes elements of structure variable */


Void initinfix ( struct infix *pq )
{
pq -> top = -1 ;
strcpy ( pq -> target, "" ) ;
strcpy ( pq -> stack, "" ) ;
pq -> l = 0 ;
}

/* reverses the given expression */


Void setexpr ( struct infix *pq, char *str )
{
pq -> s = str ;
strrev ( pq -> s ) ;
pq -> l = strlen ( pq -> s ) ;
*( pq -> target + pq -> l ) = '\0' ;
pq -> t = pq -> target + ( pq -> l - 1 ) ;
}

/* adds operator to the stack */


Void push ( struct infix *pq, char c )
{
if ( pq -> top == max - 1 )
printf ( "\nstack is full.\n" ) ;
else
{
pq -> top++ ;
pq -> stack[pq -> top] = c ;
}
}

/* pops an operator from the stack */


Char pop ( struct infix *pq )
{
if ( pq -> top == -1 )
{
printf ( "stack is empty\n" ) ;
return -1 ;
}
else
{
char item = pq -> stack[pq -> top] ;
pq -> top-- ;
return item ;
}
}

/* converts the infix expr. To prefix form */


Void convert ( struct infix *pq )
{
char opr ;

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 ) ;

while ( priority ( opr ) > priority ( *( pq -> s ) ) )


{
*( pq -> t ) = opr ;
pq -> t-- ;
opr = pop ( pq ) ;
}
push ( pq, opr ) ;
push ( pq, *( pq -> s ) ) ;
}
else
push ( pq, *( pq -> s ) ) ;
pq -> s++ ;
}

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:

Enter an expression in infix form: <a+b>*c-d+%e

Stack is empty

The prefi x expression is: +-*+abcd%e_


Program 16
Program to implement evaluation of prefix expression
Program code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

#define max 50

Struct prefix
{
int stack[max] ;
int top, nn ;
char *s ;
};

Void initprefix ( struct prefix * ) ;


Void setexpr ( struct prefix *, char * ) ;
Void push ( struct prefix *, int ) ;
Int pop ( struct prefix * ) ;
Void calculate ( struct prefix * ) ;
Void show ( struct prefix ) ;

Void main( )
{
struct prefix q ;
char expr[max] ;

clrscr( ) ;

initprefix ( &q ) ;

printf ( "\nenter prefix expression to be evaluated: " ) ;


gets ( expr ) ;

setexpr ( &q, expr ) ;


calculate ( &q ) ;
show ( q ) ;

getch( ) ;
}

/* initializes data members */


Void initprefix ( struct prefix *p )
{
p -> top = -1 ;
}

/* sets s to point to the given expr. */


Void setexpr ( struct prefix *p, char *str )
{
p -> s = str ;
strrev(p->s);
}

/* adds digit to the stack */


Void push ( struct prefix *p, int item )
{
if ( p -> top == max - 1 )
printf ( "\nstack is full." ) ;
else
{
p -> top++ ;
p -> stack[p -> top] = item ;
}
}

/* pops digit from the stack */


Int pop ( struct prefix *p )
{
int data ;

if ( p -> top == -1 )
{
printf ( "\nstack is empty." ) ;
return null ;
}

data = p -> stack[p -> top] ;


p -> top-- ;

return data ;
}

/* evaluates the prefix expression */


Void calculate( struct prefix *p )
{
int n1, n2, n3 ;
while ( *( p -> s ) )
{
/* skip whitespace, if any */
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
{
p -> s++ ;
continue ;
}

/* 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++ ;
}
}

/* displays the result */


Void show ( struct prefix p )
{
p.nn = pop ( &p ) ;
printf ( "result is: %d", p.nn ) ;
}

Output:
Enter prefix expression to be evaluated :+2*3+45
Result is:29
Program 17

Program to implement infix to postfix 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 ;
};

Void initinfix ( struct infix * ) ;


Void setexpr ( struct infix *, char * ) ;
Void push ( struct infix *, char ) ;
Char pop ( struct infix * ) ;
Void convert ( struct infix * ) ;
Int priority ( char ) ;
Void show ( struct infix ) ;

Void main( )
{
struct infix p ;
char expr[max] ;

initinfix ( &p ) ;

clrscr( ) ;

printf ( "\nenter an expression in infix form: " ) ;


gets ( expr ) ;

setexpr ( &p, expr ) ;


convert ( &p ) ;

printf ( "\nthe postfix expression is: " ) ;


show ( p ) ;

getch( ) ;
}

/* initializes structure elements */


Void initinfix ( struct infix *p )
{
p -> top = -1 ;
strcpy ( p -> target, "" ) ;
strcpy ( p -> stack, "" ) ;
p -> t = p -> target ;
p -> s = "" ;
}

/* sets s to point to given expr. */


Void setexpr ( struct infix *p, char *str )
{
p -> s = str ;
}

/* adds an operator to the stack */


Void push ( struct infix *p, char c )
{
if ( p -> top == max )
printf ( "\nstack is full.\n" ) ;
else
{
p -> top++ ;
p -> stack[p -> top] = c ;
}
}

/* pops an operator from the stack */


Char pop ( struct infix *p )
{
if ( p -> top == -1 )
{
printf ( "\nstack is empty.\n" ) ;
return -1 ;
}
else
{
char item = p -> stack[p -> top] ;
p -> top-- ;
return item ;
}
}

/* converts the given expr. From infix to postfix form */


Void convert ( struct infix *p )
{
char opr ;

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 ) == '*' || *( p -> s ) == '+' || *( p -> s ) == '/' || *( p -> s ) == '%' || *( p


-> s ) == '-' || *( p -> s ) == '$' )
{
if ( p -> top != -1 )
{
opr = pop ( p ) ;
while ( priority ( opr ) >= priority ( *( p -> s ) ) )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
push ( p, opr ) ;
push ( p, *( p -> s ) ) ;
}
else
push ( p, *( p -> s ) ) ;
p -> s++ ;
}

if ( *( p -> s ) == ')' )
{
opr = pop ( p ) ;
while ( ( opr ) != '(' )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
p -> s++ ;
}
}

while ( p -> top != -1 )


{
char opr = pop ( p ) ;
*( p -> t ) = opr ;
p -> t++ ;
}

*( p -> t ) = '\0' ;
}

/* returns the priority of an 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 postfix form of given expr. */


Void show ( struct infix p )
{
printf ( " %s", p.target ) ;
}
Output:
Enter an expression in infix form :<a+b>*cdef/h-$

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 initpostfix ( struct postfix * ) ;


Void setexpr ( struct postfix *, char * ) ;
Void push ( struct postfix *, int ) ;
Int pop ( struct postfix * ) ;
Void calculate ( struct postfix * ) ;
Void show ( struct postfix ) ;

Void main( )
{
struct postfix q ;
char expr[max] ;

clrscr( ) ;

initpostfix ( &q ) ;

printf ( "\nenter postfix expression to be evaluated: " ) ;


gets ( expr ) ;

setexpr ( &q, expr ) ;


calculate ( &q ) ;
show ( q ) ;

getch( ) ;
}

/* initializes data members */


Void initpostfix ( struct postfix *p )
{
p -> top = -1 ;
}

/* sets s to point to the given expr. */


Void setexpr ( struct postfix *p, char *str )
{
p -> s = str ;
}

/* adds digit to the stack */


Void push ( struct postfix *p, int item )
{
if ( p -> top == max - 1 )
printf ( "\nstack is full." ) ;
else
{
p -> top++ ;
p -> stack[p -> top] = item ;
}
}

/* pops digit from the stack */


Int pop ( struct postfix *p )
{
int data ;

if ( p -> top == -1 )
{
printf ( "\nstack is empty." ) ;
return null ;
}

data = p -> stack[p -> top] ;


p -> top-- ;

return data ;
}

/* evaluates the postfix expression */


Void calculate( struct postfix *p )
{
int n1, n2, n3 ;
while ( *( p -> s ) )
{
/* skip whitespace, if any */
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
{
p -> s++ ;
continue ;
}

/* 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++ ;
}
}

/* displays the result */


Void show ( struct postfix p )
{
p.nn = pop ( &p ) ;
printf ( "result is: %d", p.nn ) ;
}

Output:

Enter postfix exression to be evaluated : +2*3+45

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 addq ( int *, int, int *, int * ) ;


Int delq ( int *, int *, int * ) ;

Void main( )
{
int arr[max] ;
int front = -1, rear = -1, i ;

clrscr( ) ;

addq ( arr, 23, &front, &rear ) ;


addq ( arr, 9, &front, &rear ) ;
addq ( arr, 11, &front, &rear ) ;
addq ( arr, -10, &front, &rear ) ;
addq ( arr, 25, &front, &rear ) ;
addq ( arr, 16, &front, &rear ) ;
addq ( arr, 17, &front, &rear ) ;
addq ( arr, 22, &front, &rear ) ;
addq ( arr, 19, &front, &rear ) ;
addq ( arr, 30, &front, &rear ) ;
addq ( arr, 32, &front, &rear ) ;

i = delq ( arr, &front, &rear ) ;


printf ( "\nitem deleted: %d", i ) ;

i = delq ( arr, &front, &rear ) ;


printf ( "\nitem deleted: %d", i ) ;

i = delq ( arr, &front, &rear ) ;


printf ( "\nitem deleted: %d", i ) ;

getch( ) ;
}

/* adds an element to the queue */


Void addq ( int *arr, int item, int *pfront, int *prear )
{
if ( *prear == max - 1 )
{
printf ( "\nqueue is full." ) ;
return ;
}

( *prear )++ ;
arr[*prear] = item ;
if ( *pfront == -1 )
*pfront = 0 ;
}

/* removes an element from the queue */


Int delq ( int *arr, int *pfront, int *prear )
{
int data ;

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 addq ( int *, int, int *, int * ) ;


Int delq ( int *, int *, int * ) ;
Void display ( int * ) ;

Void main( )
{
int arr[max] ;
int i, front, rear ;

clrscr( ) ;

/* initialise data member */


front = rear = -1 ;
for ( i = 0 ; i < max ; i++ )
arr[i] = 0 ;

addq ( arr, 14, &front, &rear ) ;


addq ( arr, 22, &front, &rear ) ;
addq ( arr, 13, &front, &rear ) ;
addq ( arr, -6, &front, &rear ) ;
addq ( arr, 25, &front, &rear ) ;

printf ( "\nelements in the circular queue: " ) ;


display ( arr ) ;

i = delq ( arr, &front, &rear ) ;


printf ( "item deleted: %d", i ) ;

i = delq ( arr, &front, &rear ) ;


printf ( "\nitem deleted: %d", i ) ;

printf ( "\nelements in the circular queue after deletion: " ) ;


display ( arr ) ;

addq ( arr, 21, &front, &rear ) ;


addq ( arr, 17, &front, &rear ) ;
addq ( arr, 18, &front, &rear ) ;
addq ( arr, 9, &front, &rear ) ;
addq ( arr, 20, &front, &rear ) ;

printf ( "elements in the circular queue after addition: " ) ;


display ( arr ) ;

addq ( arr, 32, &front, &rear ) ;

printf ( "elements in the circular queue after addition: " ) ;


display ( arr ) ;

getch( ) ;
}

/* adds an element to the queue */


Void addq ( int *arr, int item, int *pfront, int *prear )
{
if ( ( *prear == max - 1 && *pfront == 0 ) || ( *prear + 1 == *pfront ) )
{
printf ( "\nqueue is full." ) ;
return ;
}

if ( *prear == max - 1 )
*prear = 0 ;
else
( *prear )++ ;

arr[*prear] = item ;

if ( *pfront == -1 )
*pfront = 0 ;
}

/* removes an element from the queue */


Int delq ( int *arr, int *pfront, int *prear )
{
int data ;

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 ;
}

/* displays element in a queue */


Void display ( int * arr )
{
int i ;
printf ( "\n" ) ;
for ( i = 0 ; i < max ; i++ )
printf ( "%d\t", arr[i] ) ;
printf ( "\n" ) ;
}

Output:
Elements in the circular queue:
14 22 13 -6 25 0 0 0 0 0

Item deleted: 14
Item deleted:22

Elements in the circular queue after deletion:


0 0 13 -6 25 0 0 0 0 0
Elements in the circular queue after addition:
0 0 13 -6 25 21 17 18 9 20
Elements in the circular queue after addition:
32 0 13 -6 25 21 17 18 9 20
_
Program 21
Program to implement all operations on a Dqueue
Program code:
#include <stdio.h>
#include <conio.h>

#define max 10

Void addqatbeg ( int *, int, int *, int * ) ;


Void addqatend ( int *, int, int *, int * ) ;
Int delqatbeg ( int *, int *, int * ) ;
Int delqatend ( int *, int *, int * ) ;
Void display ( int * ) ;
Int count ( int * ) ;

Void main( )
{
int arr[max] ;
int front, rear, i, n ;

clrscr( ) ;

/* initialises data members */


front = rear = -1 ;
for ( i = 0 ; i < max ; i++ )
arr[i] = 0 ;

addqatend ( arr, 17, &front, &rear ) ;


addqatbeg ( arr, 10, &front, &rear ) ;
addqatend ( arr, 8, &front, &rear ) ;
addqatbeg ( arr, -9, &front, &rear ) ;
addqatend ( arr, 13, &front, &rear ) ;
addqatbeg ( arr, 28, &front, &rear ) ;
addqatend ( arr, 14, &front, &rear ) ;
addqatbeg ( arr, 5, &front, &rear ) ;
addqatend ( arr, 25, &front, &rear ) ;
addqatbeg ( arr, 6, &front, &rear ) ;
addqatend ( arr, 21, &front, &rear ) ;
addqatbeg ( arr, 11, &front, &rear ) ;

printf ( "\nelements in a deque: " ) ;


display ( arr ) ;

n = count ( arr ) ;
printf ( "\ntotal number of elements in deque: %d", n ) ;

i = delqatbeg ( arr, &front, &rear ) ;


printf ( "\nitem extracted: %d", i ) ;

i = delqatbeg ( arr, &front, &rear ) ;


printf ( "\nitem extracted:%d", i ) ;

i = delqatbeg ( arr, &front, &rear ) ;


printf ( "\nitem extracted:%d", i ) ;
i = delqatbeg ( arr, &front, &rear ) ;
printf ( "\nitem extracted: %d", i ) ;

printf ( "\nelements in a deque after deletion: " ) ;


display ( arr ) ;

addqatend ( arr, 16, &front, &rear ) ;


addqatend ( arr, 7, &front, &rear ) ;

printf ( "\nelements in a deque after addition: " ) ;


display ( arr ) ;

i = delqatend ( arr, &front, &rear ) ;


printf ( "\nitem extracted: %d", i ) ;

i = delqatend ( arr, &front, &rear ) ;


printf ( "\nitem extracted: %d", i ) ;

printf ( "\nelements in a deque after deletion: " ) ;


display ( arr ) ;

n = count ( arr ) ;
printf ( "\ntotal number of elements in deque: %d", n ) ;

getch( ) ;
}

/* adds an element at the beginning of a deque */


Void addqatbeg ( int *arr, int item, int *pfront, int *prear )
{
int i, k, c ;

if ( *pfront == 0 && *prear == max - 1 )


{
printf ( "\ndeque is full.\n" ) ;
return ;
}

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 ;
}
}

/* adds an element at the end of a deque */


Void addqatend ( int *arr, int item, int *pfront, int *prear )
{
int i, k ;

if ( *pfront == 0 && *prear == max - 1 )


{
printf ( "\ndeque is full.\n" ) ;
return ;
}

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 ;
}

/* removes an element from the *pfront end of deque */


Int delqatbeg ( int *arr, int *pfront, int *prear )
{
int 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 ;
}

/* removes an element from the *prear end of the deque */


Int delqatend ( int *arr, int *pfront, int *prear )
{
int 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 ;
}

/* displays elements of a deque */


Void display ( int *arr )
{
int i ;

printf ( "\n front->" ) ;


for ( i = 0 ; i < max ; i++ )
printf ( "\t%d", arr[i] ) ;
printf ( " <-rear" ) ;
}

/* counts the total number of elements in deque */


Int count ( int *arr )
{
int c = 0, i ;

for ( i = 0 ; i < max ; i++ )


{
if ( arr[i] != 0 )
c++ ;
}
return c ;
}
Output:
Deque is full.
Deque is full.

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 dfs ( int, struct node ** ) ;


Struct node * getnode_write ( int ) ;
Void del ( struct node * ) ;

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 ) ;

for ( i = 0 ; i < max ; i++ )


del ( arr[i] ) ;

getch( ) ;
}

Void dfs ( int v, struct node **p )


{
struct node *q ;
visited[v - 1] = true ;

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 ;
}
}

Struct node * getnode_write ( int val )


{
struct node *newnode ;
newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
newnode -> data = val ;
return newnode ;
}

Void del ( struct node *n )


{
struct node *temp ;

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 bfs ( int, struct node ** ) ;


Struct node * getnode_write ( int ) ;
Void addqueue ( int ) ;
Int deletequeue( ) ;
Int isempty( ) ;
Void del ( struct node * ) ;

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 ) ;

for ( i = 0 ; i < max ; i++ )


del ( arr[i] ) ;

getch( ) ;
}

Void bfs ( int v, struct node **p )


{
struct node *u ;

visited[v - 1] = true ;
printf ( "%d\t", v ) ;
addqueue ( v ) ;

while ( isempty( ) == false )


{
v = deletequeue( ) ;
u=*(p+v-1);

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 ;
}
}
}

Struct node * getnode_write ( int val )


{
struct node *newnode ;
newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
newnode -> data = val ;
return newnode ;
}

Void addqueue ( int vertex )


{
if ( rear == max - 1 )
{
printf ( "\nqueue overflow." ) ;
exit( ) ;
}

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 ;
}

Void del ( struct node *n )


{
struct node *temp ;
while ( n != null )
{
temp = n -> next ;
free ( n ) ;
n = temp ;
}
}

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>

/* structure containing a data part and link part */


Struct node
{
int data ;
struct node *link ;
} *newnode, *start, *visit ;

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 ) ;

append ( &new, val ) ;

printf ( "\nany more nodes (y/n): " ) ;


ch = getche( ) ;
} while ( ch == 'y' || ch == 'y' ) ;

start = new ;
}

/* adds a node at the end of a linked list */


Void 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 ;
}

/* displays the contents of the linked list */


Void displaylist( )
{
visit = start ;

/* traverse the entire linked list */


while ( visit != null )
{
printf ( "%d ", visit -> data ) ;
visit = visit -> link ;
}
}

/* counts the number of nodes present in the linked list */


Int count ( struct node * q )
{
int c = 0 ;

/* traverse the entire linked list */


while ( q != null )
{
q = q -> link ;
c++ ;
}

return c ;
}

Void selection_sort ( int n )


{
int i, j, k, temp ;
struct node *p, *q ;

p = start ;
for ( i = 0 ; i < n - 1 ; i++ )
{
q = p -> link ;

for ( j = i + 1 ; j < n ; j++ )


{
if ( p -> data > q -> data )
{
temp = p -> data ;
p -> data = q -> data ;
q -> data = temp ;
}
q = q -> link ;
}
p = p -> link ;
}
}

Void bubble_sort ( int n )


{
int i, j, k, temp ;
struct node *p, *q ;

k=n;
for ( i = 0 ; i < n - 1 ; i++, k-- )
{
p = start ;
q = p -> link ;

for ( j = 1 ; j < k ; j++ )


{
if ( p -> data > q -> data )
{
temp = p -> data ;
p -> data = q -> data ;
q -> data = temp ;
}
p = p -> link ;
q = q -> link ;
}
}
}
Output :
Enter a vakue: 12

Any more node <y/n>: y


Enter a vakue: 45

Any more node <y/n>: y


Enter a vakue: 10

Any more node <y/n>: y


Enter a vakue: 67

Any more node <y/n>:

Linked list before sorting: 12 45 10 67


Nlinked list after selection sorting: 1012 45 67
Program 25
Program to implement reversing of a linked list.
Program code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */


Struct node
{
int data ;
struct node *link ;
};

Void addatbeg ( struct node **, int ) ;


Void reverse ( struct node ** ) ;
Void display ( struct node * ) ;
Int count ( struct node * ) ;

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 ) ) ;
}

/* adds a new node at the beginning of the linked list */


Void 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 ;
}

Void reverse ( struct node **x )


{
struct node *q, *r, *s ;

q = *x ;
r = null ;

/* traverse the entire linked list */


while ( q != null )
{
s=r;
r=q;
q = q -> link ;
r -> link = s ;
}

*x = r ;
}

/* displays the contents of the linked list */


Void display ( struct node *q )
{
printf ( "\n" ) ;

/* traverse the entire linked list */


while ( q != null )
{
printf ( "%d ", q -> data ) ;
q = q -> link ;
}
}

/* counts the number of nodes present in the linked list */


Int count ( struct node * q )
{
int c = 0 ;

/* traverse the entire linked list */


while ( q != null )
{
q = q -> link ;
c++ ;
}
return c ;
}

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];

Void updatedistances(int target) {


int i;
for (i = 0; i < n; ++i)
If ((weight[target][i] != 0) && (d[i] > weight[target][i])) {
d[i] = weight[target][i];
whoto[i] = target;
}
}

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);

/* initialise d with infinity */


for (i = 0; i < n; ++i)
d[i] = 100000;

/* mark all nodes as not beeing in the minimum spanning tree */


for (i = 0; i < n; ++i)
intree[i] = 0;

/* add the first node to the tree */


printf("adding node %c\n", 0 + 'a');
intree[0] = 1;
updatedistances(0);
total = 0;
for (treesize = 1; treesize < n; ++treesize) {
/* find the node with the smallest distance to the tree */
int min = -1;
for (i = 0; i < n; ++i)
If (!intree[i])
If ((min == -1) || (d[min] > d[i]))

Min = i;

/* and add it */
printf("adding edge %c-%c\n", whoto[min] + 'a', min + 'a');
intree[min] = 1;
total += d[min];

updatedistances(min);
}

printf("total distance: %d\n", total);


getch();
return 0;
}

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 insert ( struct btreenode **, int ) ;


Void inorder ( struct btreenode * ) ;
Void preorder ( struct btreenode * ) ;
Void postorder ( struct btreenode * ) ;

Void main( )
{
struct btreenode *bt ;
int req, i = 1, num ;

bt = null ; /* empty tree */

clrscr( ) ;
printf ( "specify the number of items to be inserted: " ) ;
scanf ( "%d", &req ) ;

while ( i++ <= req )


{
printf ( "enter the data: " ) ;
scanf ( "%d", &num ) ;
insert ( &bt, num ) ;
}

printf ( "\nin-order traversal: " ) ;


inorder ( bt ) ;

printf ( "\npre-order traversal: " ) ;


preorder ( bt ) ;

printf ( "\npost-order traversal: " ) ;


postorder ( bt ) ;
}

/* inserts a new node in a binary search tree */


Void insert ( struct btreenode **sr, int num )
{
if ( *sr == null )
{
*sr = malloc ( sizeof ( struct btreenode ) ) ;
( *sr ) -> leftchild = null ;
( *sr ) -> data = num ;
( *sr ) -> rightchild = null ;
return ;
}
else /* search the node to which new node will be attached */
{
/* if new data is less, traverse to left */
if ( num < ( *sr ) -> data )
insert ( &( ( *sr ) -> leftchild ), num ) ;
else
/* else traverse to right */
insert ( &( ( *sr ) -> rightchild ), num ) ;
}
return ;
}

/* traverse a binary search tree in a ldr (left-data-right) fashion */


Void inorder ( struct btreenode *sr )
{
if ( sr != null )
{
inorder ( sr -> leftchild ) ;

/* print the data of the node whose leftchild is null or the path
has already been traversed */
printf ( "\t%d", sr -> data ) ;

inorder ( sr -> rightchild ) ;


}
else
return ;
}

/* traverse a binary search tree in a dlr (data-left-right) fashion */


Void preorder ( struct btreenode *sr )
{
if ( sr != null )
{
/* print the data of a node */
printf ( "\t%d", sr -> data ) ;
/* traverse till leftchild is not null */
preorder ( sr -> leftchild ) ;
/* traverse till rightchild is not null */
preorder ( sr -> rightchild ) ;
}
else
return ;
}

/* traverse a binary search tree in lrd (left-right-data) fashion */


Void postorder ( struct btreenode *sr )
{
if ( sr != null )
{
postorder ( sr -> leftchild ) ;
postorder ( sr -> rightchild ) ;
printf ( "\t%d", sr -> data ) ;
}
else
return ;
}

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 ;

Struct lledge * kminstree ( struct lledge *, int ) ;


Int getrval ( int ) ;
Void combine ( int, int ) ;
Void del ( struct lledge * ) ;

Void main( )
{
struct lledge *temp, *root ;
int i ;

clrscr( ) ;

root = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ;

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 ) ) ;

temp = temp -> next ;


temp -> v1 = 3 ;
temp -> v2 = 2 ;
temp -> cost = 3 ;
temp -> next = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ;

temp = temp -> next ;


temp -> v1 = 4 ;
temp -> v2 = 1 ;
temp -> cost = 4 ;
temp -> next = null ;
root = kminstree ( root, 5 ) ;

for ( i = 1 ; i <= 4 ; i++ )


printf ( "\nstree[%d] -> %d", i, stree[i] ) ;
printf ( "\nthe minimum cost of spanning tree is %d", mincost ) ;
del ( root ) ;

getch( ) ;
}
Struct lledge * kminstree ( struct lledge *root, int n )
{
struct lledge *temp = null ;
struct lledge *p, *q ;
int noofedges = 0 ;
int i, p1, p2 ;

for ( i = 0 ; i < n ; i++ )


stree[i] = i ;
for ( i = 0 ; i < n ; i++ )
count[i] = 0 ;

while ( ( noofedges < ( n - 1 ) ) && ( root != null ) )


{
p = root ;

root = root -> next ;

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 ;
}

Int getrval ( int i )


{
int j, k, temp ;
k=i;
while ( stree[k] != k )
k = stree[k] ;
j=i;
while ( j != k )
{
temp = stree[j] ;
stree[j] = k ;
j = temp ;
}
return k ;
}

Void combine ( int i, int j )


{
if ( count[i] < count[j] )
stree[i] = j ;
else
{
stree[j] = i ;
if ( count[i] == count[j] )
count[j]++ ;
}
}

Void del ( struct lledge *root )


{
struct lledge *temp ;

while ( root != null )


{
temp = root -> next ;
free ( root ) ;
root = temp ;
}
}

Output:
Stree[1] -> 4
Stree[2] -> 4
Stree[3] -> 4
Stree[4] -> 4
Nthe minimum cost of spanning tree is 7

You might also like