Sunday, December 12, 2010

Substring Search

//PROGRAM TO SEARCH FOR SUBSTRING IN A STRING
#include
#include
#include
#define SIZE 20
int search(char [],char []);
char* delet(char*,char*,int);
void main()
{
int ret=0,pos=0,s=0;
char str[SIZE],sub[SIZE],temp[SIZE];
clrscr();
printf("Enter the string: ");
gets(str);
printf("Enter the sub string: ");
gets(sub);
strcpy(temp,str);
while(ret!=-1)
{
ret=search(temp,sub);
if(s {
pos=ret+s*strlen(sub)+1;
printf("Substring found at position %d\n",pos);
}
s++;
if(ret!=-1)
{
strcpy(temp,delet(temp,sub,ret));
}
}
if(strcmp(temp,str)==0)
printf("Substring not found\n");
else
printf("Substring after deletion is %s\n",temp);
getch();
}
//searching substring

int search(char *a,char *b)
{
int i=0,j=0,m,n,pos;
m=strlen(a);
n=strlen(b);
while(i {
while((a[i]==b[j]) && b[j]!=0)
{
i++;
j++;
}
if(j==n)
{
pos=i-j;
return(pos);
}
else
{
i=i-j+1;
j=0;
}
}
return(-1);
}

//deleting substring
char* delet(char *a,char *b,int pos)
{
int m,i=pos;
m=strlen(b);
do
{
a[i]=a[i+m];
i++;
}
while(a[i]!='\0');
return(a);
}

Sin Function Evaluation using Taylor Series

//PROGRAM TO EVALUATE FUNCTION SIN USING TAYLOR SERIES.
#include
#include
#include
#define PI 3.1415
double sin_x(int,int);
int fact(int);
void main()
{
int x,n;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of n:");
scanf("%d",&n);
printf("\nValue of sin x is ",exp_x(x,n));
getch();
}
double sin_x(int ang_deg,int nt)
{
int term,j;
double value=0.0,ang_rad=0.0;
ang_rad=(double)ang_deg*PI/180;
for(term=1,j=2;term {
value+=(double)pow(-1.0,j)*pow(ang_rad,term)/fact(term);
}
return value;
}

int fact(int num)
{
int f=0;
if(num==1)
return 1;
else
f=num*fact(num-1);
return f;
}

Prime Number or Not

//PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS PRIME OR NOT
#include
#include
int check(int);
void main()
{
int num,ret;
printf("Enter the number : ");
scanf("%d",&num);
ret=check(num);
if(ret==1)
printf("%d is not Prime\n",num);
else if(ret==0)
printf("%d is Prime\n",num);
else
printf("It is neither prime nor composite\n");
getch();
}

int check(int n)
{
int i;
if(n==0)
return 1;
if(n==1)
return -1;
for(i=2;i {
if(n%i==0)
return 1;
}
return 0;
}

Find Mean ,Median And Mode of a set of Numbers

//PROGRAM TO FIND MEAN,MEDIAN AND MODE OF N GIVEN SET OF NUMBERS.
#include
#include
void main()
{
float mean,median;
int mode,sum=0,temp;
int n,i,j,pos,num[20];

int highest=0,repeat=0;
int last_num;

clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
printf("\nEnter the %d numbers:\n",n);
for(i=0;inum[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
if(n%2==1)
{
pos=(n+1)/2;
median=num[pos];
printf("\n\nThe Median is %f",median);
}
else
{
pos=n/2;
median=(num[pos]+num[pos+1])/2;
printf("\n\nThe Median is %f",median);
}

last_num=num[0];
for(i=1;ihighest)
{
highest=repeat;
printf("%d",repeat);
mode=num[i];
}
}
else //the number was not repeated
repeat=0;
last_num=num[i];
}

printf("\n\nThe Mode is %f",mode);
getch();
}

Generate Permuations of the letters of a String

//PROGRAM TO GENERATE ALL PERMUTATIONS OF THE LETTERS OF A GIVEN STRING.
#include
#include
void permute(char *,int,int);
void main()
{
char str[20];
int l;
clrscr();
printf("Enter the string:");
scanf("%s",str);
l=strlen(str);
printf("\nPermutations of the letters of the string:");
printf("\n------------------------------------------");
permute(str,0,l);
printf("\n------------------------------------------");
getch();
}
void permute(char *n,int start,int len)
{
int i,j;
char temp;
for(i=start;i {
for(j=i+1;j {
temp=n[i];
n[i]=n[j];
n[j]=temp;
permute(n,i+1,len);
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
printf("\n%s",n);
}

Hcf and Lcm Of given numbers

//PROGRAM TO FIND THE HCF AND LCM OF THE GIVEN NUMBERS.

#include
#include
int hcf(int a,int b);
int lcm(int x,int y);
void main()
{
int n,ch,num[10],hc,lc,i;
clrscr();
printf("\nEnter the value of n:"); //reading numbers
scanf("%d",&n);
printf("Enter the %d numbers:",n);
for(i=0;i scanf("%d",&num[i]);

hc=num[0]; //calculating hcf
for(i=1;i hc=hcf(hc,num[i]);
printf("\nThe HCF is %d",hc);

lc=num[0]; //calculating lcm
for(i=1;i lc=lcm(lc,num[i]);
printf("\nThe LCM is %d",lc);

getch();
}

int hcf(int a,int b) //hcf function
{
if(b==0)
return a;
else
return hcf(b,a%b);
}

int lcm(int x,int y) //lcm function
{
int l;
l=x*y/hcf(x,y);
return l;
}

Lexicographic Sorting of Strings

//PROGRAM TO PERFORM LEXICOGRAPHIC SORTING OF GIVEN STRINGS.
#include
#include
#include
void sort(char[20][20],int);
void main()
{
char strn[20][20];
int i=0,n;
clrscr();
printf("\nEnter the number of strings to be sorted:");
scanf("%d",&n);
printf("\nEnter the %d strings:\n",n);
for(i=0;i0)
{
strcpy(temp,str[j-1]);
strcpy(str[j-1],str[j]);
strcpy(str[j],temp);
}
}
}
printf("\n-------------------------------------");
printf("\nThe sorted list is \n");
for(i=0;i {
printf("%s\n",str[i]);
}
}

Jordan Elimination Method

//PROGRAM TO FIND SOLUTION TO LINEAR EQAUTIONS USING JORDAN ELIMINATION.

#include
#include
void solution(int a[20][20],int n);
void main()
{
int a[20][20],n,i,j,k,l;
clrscr();
printf("\n-------Jordan Elimination method----------");
printf("\nEnter the number of variables:\n");
scanf("%d",&n);
for(i=0;i {
printf("\nEnter the equation%d\n",i+1);
for(j=0;j {
printf("Enter the coefficient of x%d:",j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter the constant:");
scanf("%d",&a[i][n]);
}
solution(a,n);
getch();
}

void solution(int a[20][20],int n)
{
int k,i,l,j;
for(k=0;k {
for(i=0;i<=n;i++)
{
l=a[i][k];
for(j=0;j<=n;j++)
{
if(i!=k)
a[i][j]=a[k][k]*a[i][j]-l*a[k][j];
}
}
}
printf("\n---------Solution----------");
for(i=0;i {
printf("\nThe value of x%d is %f\n",i+1,(float)a[i][n]/(float)a[i][i]);
}
}

Output
-----------------------------------------

-------Jordan Elimination method----------
Enter the number of variables:
2

Enter the equation1
Enter the coefficient of x1:2
Enter the coefficient of x2:2

Enter the constant:4

Enter the equation2
Enter the coefficient of x1:1
Enter the coefficient of x2:3

Enter the constant:4

---------Solution----------
The value of x1 is 1.000000

The value of x2 is 1.000000

Find Inverse And Determinant of a Matrix

//PROGRAM TO FIND THE INVERSE AND DETERMINANT OF A GIVEN MATRIX USING RECURSION

#include
#include
#include

float detrm(float[25][25],float);
void cofact(float[25][25],float);
void trans(float[25][25],float[25][25],float);

void main()
{
float a[25][25],d;
int i,j,n;
clrscr();
printf("Enter the order of the matrix:\n");
scanf("%d",&n);
printf("Enter the %d elements of the matrix:\n",n);
for(i=0;i {
for(j=0;j {
scanf("%f",&a[i][j]);
}
}
d=detrm(a,n);
printf("\nThe determinant is %f",d);
if(d==0)
printf("\nMatrix is not inversible\n");
else
cofact(a,n);
getch();
}

float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c {
m=0;
n=0;
for(i=0;i {
for(j=0;j {
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}

void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q {
for(p=0;p {
m=0;
n=0;
for(i=0;i {
for(j=0;j {
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}

void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i {
for(j=0;j {
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i {
for(j=0;j {
inv[i][j]=b[i][j]/d;
}
}
printf("\n\nThe inverse of the matrix is :\n");
for(i=0;i {
printf("\n");
for(j=0;j {
printf("%f ",inv[i][j]);
}
}
}

Output
-------------------------------------------
Enter the order of the matrix:
2
Enter the 2 elements of the matrix:
1
2
3
4

The determinant is -2.000000

The inverse of the matrix is :

-2.000000 1.000000
1.500000 -0.500000

File Operations

/*PROGRAM TO ILLUSTARTE FILE OPERATIONS LIKE INSERTION,DELETION,SEARCH,SORT
AND UPDATE OF A RECORD*/

#include
#include
#define SIZE 10
struct student
{
char name[SIZE];
int roll;
int tot;
int m1,m2,m3;
}s,temp[SIZE],t[SIZE],s1;
void insert();
void search();
void display();
void delet();
void sort();
void main()
{
int ch;
FILE *fp;
clrscr();
do
{
printf("\n\tMenu\n");
printf("\t[1] Insert\n");
printf("\t[2] Search\n");
printf("\t[3] Display\n");
printf("\t[4] Delete\n");
printf("\t[0] Sort\n");
printf("\t[6] Exit\n");
printf("\t: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
search();
break;
case 3:
display();
break;
case 4:
delet();
break;
case 5:
sort();
break;
case 6:
break;
default:
printf("Wrong input");
break;
}
}while(ch!=6);
getch();
}

void insert()
{
FILE *fp;
fp=fopen("stud","a+");
printf("Name\t\t: ");
scanf("%s",s.name);
printf("Roll Number\t: ");
scanf("%d",&s.roll);
printf("Enter marks in 3 subjects\t\t: ");
scanf("%d %d %d",&s.m1,&s.m2,&s.m3);
s.tot=s.m1+s.m2+s.m3;
printf("Total : %d\n",s.tot);
fwrite(&s,sizeof(s),1,fp);
fclose(fp);
}

void search()
{
int i=0,m,j,flag=0;
FILE *fp;
fp=fopen("stud","r");
while(fread(&s,sizeof(s),1,fp)==1)
temp[i++]=s;
printf("Enter Roll Number : ");
scanf("%d",&m);
for(j=0;jt[k].tot)
{
s1=t[j];
t[j]=t[k];
t[k]=s1;
}
}
}
fclose(fp);
fp=fopen("stud","w");
for(i=0;i fwrite(&t[i],sizeof(t[i]),1,fp);
fclose(fp);
display();
}



Output
------------------------------------
Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 1
Name : Ramu
Roll Number : 2
Enter marks in 3 subjects: 23 24 25
Total : 72

Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 1
Name : Raju
Roll Number : 1
Enter marks in 3 subjects: 45 34 23
Total : 102

Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 1
Name : Rohan
Roll Number : 5
Enter marks in 3 subjects: 23
34
35
Total : 92


Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 3

Name : Ramu
Roll number : 2
Marks : 23 24 25
Total : 72

Name : Raju
Roll number : 1
Marks : 45 34 23
Total : 102

Name : Rohan
Roll Number : 5
Marks : 23 34 35
Total : 92

Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 2
Enter Roll Number : 1

Name : Raju
Total : 102


Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 5

Name : Ramu
Roll number : 2
Marks : 23 24 25
Total : 72

Name : Rohan
Roll number : 5
Marks : 23 34 35
Total : 92

Name : Raju
Roll number : 1
Marks : 45 34 23
Total : 102

Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 4
Roll Number : 5

Deleted record

Name : Rohan
Roll number : 5
Marks: 23 34 35
Total : 92

Menu
[1] Insert
[2] Search
[3] Display
[4] Delete
[5] Sort
[6] Exit
: 3

Name : Ramu
Roll number : 2
Marks : 23 24 25
Total : 72

Name : Raju
Roll number : 1
Marks : 45 34 23
Total : 102
*/

e^x Function Evaluation Using Taylor Series

//PROGRAM TO EVALUATE FUNCTION e^x USING TAYLOR SERIES.
#include
#include
#include
#define PI 3.1415
float exp_x(int,int);
int fact(int);
void main()
{
int x,n;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of n:");
scanf("%d",&n);
printf("\nValue of e^x is ",exp_x(x,n));
getch();
}
float exp_x(int x,int n)
{
int i=1;
float ex=1;
while(i {
ex+=(float)pow(x,i)/fact(i);
++i;
}
return ex;
}
int fact(int num)
{
int f=0;
if(num==1)
return 1;
else
f=num*fact(num-1);
return f;
}

Cos x Function Evaluation using Taylor series

//PROGRAM TO EVALUATE FUNCTION COS X USING TAYLOR SERIES.
#include
#include
#include
#define PI 3.1415

double cos_x(int,int);
int fact(int);
void main()
{
int x,n;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of n:");
scanf("%d",&n);
printf("\nValue of cos x is ",exp_x(x,n));
getch();
}
double cos_x(int ang_deg,int nt)
{
int term,j;
double value=1.0,ang_rad=0.0;
ang_rad=(double)ang_deg*PI/180;
for(term=2,j=1;term<=nt;term+=2,j++)
{
value+=(double)pow(-1.0,j)*pow(ang_rad,term)/fact(term);
}
return value;
}

int fact(int num)
{
int f=0;
if(num==1)
return 1;
else
f=num*fact(num-1);
return f;
}

Binary to Octal And Octal to Binary Conversion

//PROGRAM TO CONVERT BINARY TO OCTAL AND VICE-VERSA.

#include
#include
#include
#define SIZE 20

int binary_to_decimal(int);
void decimal_to_binary(int);
void binary_to_octal(int);
void octal_to_binary(int);
void main()
{
int ch=0,num=0;
clrscr();
do
{
printf("\n--------------Menu------------------");
printf("\n1_Binary to octal");
printf("\n2_octal to binary");
printf("\n3_Exit from program");
printf("\n\nEnter your choice<1-3>:");
scanf("%d",&ch);
switch(ch)
{
case 1: // Binary to octal
printf("\nEnter the binary number:");
scanf("%d",&num);
printf("\nThe octal equivalent of %d is ",num);
binary_to_octal(num);
break;
case 2: // Octal to Binary
printf("\n\nEnter the octal number:");
scanf("%d",&num);
printf("\n\nThe Binary equivalent of %d is ",num);
octal_to_binary(num);
break;
case 3: // Exit
exit(0);
break;
default:
printf("\nWrong choice!!!!!!");
break;
}
}
while(ch!=3);
}

int binary_to_decimal(int num)
{
int i=0,sum=0;
while(num!=0)
{
sum=sum+(num%10)*pow(2,i++);
num/=10;
}
return sum;
}

void decimal_to_binary(int num)
{
int array[SIZE],i=0;
while(num!=0)
{
array[i++]=num%2;
num/=2;
}
for(--i;i>=0;i--)
printf("%d",array[i]);
}

void binary_to_octal(int num)
{
int array[SIZE],i=0;
num=binary_to_decimal(num);
if(num==0)
{
array[i++]=0;
}
while(num!=0)
{
array[i++]=num%8;
num/=8;
}
for(--i;i>=0;i--)
printf("%d",array[i]);
}

void octal_to_binary(int num)
{
int i=0,sum=0;
while(num!=0)
{
sum=sum+(num%10)*pow(8,i++);
num/=10;
}
decimal_to_binary(sum);
}

Binary to Hexadecimal And Hexadecimal to Binary Conversion

//PROGRAM TO CONVERT BINARY TO HEXADECIMAL AND VICE-VERSA
#include
#include
#include
#define SIZE 20

int binary_to_decimal(int);
void decimal_to_binary(int);
void binary_to_hexa(int);
void hexa_to_binary(char []);
void main()
{
int ch=0,num=0;
char hexa[10];
clrscr();
do
{
printf("\n\n------------------Menu----------------------");
printf("\n1_Binary to Hexadecimal");
printf("\n2_Hexadecimal to Binary");
printf("\n3_Exit from program");
printf("\n\nEnter your choice<1-3>:");
scanf("%d",&ch);
switch(ch)
{
case 1: // Binary to Hexadecimal
printf("\nEnter the binary number:");
scanf("%d",&num);
printf("\nThe Hexadecimal equivalent of %d is ",num);
binary_to_hexa(num);
break;
case 2: // Hexadecimal to Binary
printf("\nEnter the hexadecimal number:");
scanf("%s",&hexa);
printf("\nThe Binary equivalent of %s is ",hexa);
hexa_to_binary(hexa);
break;
case 3: // Exit
exit(0);
break;
default:
printf("\nWrong choice!!!!!!");
break;
}
}
while(ch!=3);
}

int binary_to_decimal(int num)
{
int i=0,sum=0;
while(num!=0)
{
sum=sum+(num%10)*pow(2,i++);
num/=10;
}
return sum;
}

void decimal_to_binary(int num)
{
int array[SIZE],i=0;
while(num!=0)
{
array[i++]=num%2;
num/=2;
}
for(i=i-1;i>=0;i--)
printf("%d",array[i]);
}

void binary_to_hexa(int num)
{
int array[SIZE],i=0;
num=binary_to_decimal(num);
if(num==0)
{
array[i++]=0;
}
while(num!=0)
{
array[i++]=num%16;
num/=16;
}
for(i=i-1;i>=0;i--)
{
if(array[i]>=10&&array[i]<=15) printf("%c",array[i]+55); else printf("%d",array[i]); } } void hexa_to_binary(char s[]) { int a[SIZE],i,dn=0,j=0; for(i=0;s[i]!=NULL;i++) { if(s[i]<=57&&s[i]>=48)
a[i]=s[i]-48;
else if(s[i]>=65&&s[i]<=70) a[i]=s[i]-55; else { printf("Wrong Input"); return; } } for(;--i>=0;)
{
dn+=a[i]*pow(16,j++);
}
decimal_to_binary(dn);
}

Binary to Decimal And Decimal to Binary Conversion

//PROGRAM TO CONVERT BINARY TO DECIMAL AND VICE-VERSA
#include
#include
#include
#define SIZE 20

int binary_to_decimal(int);
void decimal_to_binary(int);
void main()
{
int ch=0,num=0,temp;
clrscr();
do
{
printf("\n--------------Menu------------------");
printf("\n1_Binary to decimal");
printf("\n2_Decimal to binary");
printf("\n3_Exit from program");
printf("\nEnter your choice<1-3>:");
scanf("%d",&ch);
switch(ch)
{
case 1: // Binary to Decimal
printf("Enter the binary number:");
scanf("%d",&num);
temp=num;
printf("\nThe decimal equivalent of %d is %d",temp,binary_to_decimal(num));
break;
case 2: // Decimal to Binary
printf("\nEnter the decimal number:");
scanf("%d",&num);
temp=num;
printf("\nThe Binary equivalent of %d is ",temp);
decimal_to_binary(num);
break;
case 3: // Exit
exit(0);
break;
default:
printf("\nWrong choice!!!!!!");
break;
}
}
while(ch!=3);
}

int binary_to_decimal(int num)
{
int i=0,sum=0;
while(num!=0)
{
sum=sum+(num%10)*pow(2,i++);
num/=10;
}
return sum;
}

void decimal_to_binary(int num)
{
int array[SIZE],i=0;
while(num!=0)
{
array[i++]=num%2;
num/=2;
}
for(i=i-1;i>=0;i--)
printf("%d",array[i]);
}

Heap Sort

//Program to Perform Heap Sort
#include
#include

#define SIZE 10
class heap
{
int a[SIZE],n;

public:
heap()
{
n=0;
}
void read();
void swap(int*,int*);
void heapsort();
void display();
};
void heap::read()
{
cout<<" HEAP SORT\n";
cout<<" ---------\n\n";
cout<<"\nEnter the number of elements:";
cin>>n;

cout<<"\nEnter the "<
for(int i=0;i< n;++i)
cin>>a[i];

heapsort();
}

void heap::swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

void heap::heapsort()
{
int i,s,f;
for(i=1;i {
s=i;
f=(s-1)/2;
while(a[f] {
swap(&a[f],&a[s]);
s=f;
if(s==0)
break;
f=(s-1)/2;
}
}
for(i=n-1;i>=1;--i)
{
swap(&a[0],&a[i]);
f=0;
s=1;
if(i==1)
break;
if(i>2)
if(a[2]>a[1])
s=2;
while(a[f] {
swap(&a[f],&a[s]);
f=s;
s=2*f+1;
if(i>s+1)
if(a[s+1]>a[s])
s=s+1;
if (s>=i)
break;
}
}
}

void heap::display()
{
cout<<"\nThe Sorted List is\n\n";
for(int i=0;i cout<<" "< }
void main()
{
clrscr();
heap h;
h.read();
h.display();
getch();
}



OUTPUT



HEAP SORT
---------

Enter the number of elements:7

Enter the 7 elements:
89
4
1
78
103
8
11

The Sorted List is

1 4 8 11 78 89 103

Priority Queue

//program to illustrate Priority Queue Operations
#include
#include

class PriorityQueue
{
struct node
{
int info;
int prio;
node *next;
node *prev;
}*p;

public:
PriorityQueue()
{
p=new node;
p->next=NULL;
p->prev=NULL;
p->prio=-1;
}
void insert(int,int);
void delet(int);
void disp();
};


//inserts a node of given priority
void PriorityQueue::insert(int item,int priority)
{
node *temp,*q;

temp=new node;
temp->info=item;
temp->prio=priority;

//checks the queue is empty
if(p->next==NULL)
{
p->next=temp;
temp->prev=p;
temp->next=NULL;
return;
}

q=p;

while(q->next!=NULL&&q->prio q=q->next;

if(q->prio>priority)
q=q->prev;

temp->next=q->next;
temp->prev=q;
q->next=temp;
(temp->next)->prev=temp;
}


//Deletes an item from the queue
void PriorityQueue::delet(int item)
{
node *q;

if(p->next==NULL)
{
cout<<"\nThe Queue is Empty\n";
return;
}

for(q=p->next;q!=NULL;q=q->next)
{
if(q->info==item)
{
(q->prev)->next=q->next;
(q->next)->prev=q->prev;
cout<<"\nItem Deleted:"<info< cout<<"Priority:"<prio< delete q;
return;
}
}
cout<<"Item not found\n";

}


//displays the node and its contents
void PriorityQueue::disp()
{
node *q;

if(p->next==NULL)
{
cout<<"\nThe Queue is Empty\n";
return;
}

cout<<"\nQUEUE\n-------\n";
for(q=p->next;q!=NULL;q=q->next)
cout<<"Info:"<info<<" Prio:"<prio<
}



void main()
{
PriorityQueue PQ;
int item,prio,ch;
char opt;
int done=1;
clrscr();
cout<<"\n\tPRIORITY QUEUE \n\t---------------\n";
while(done)
{
cout<<"\n\n\tOPERATIONS\n\t-----------\n";
cout<<"\n\t1.INSERT\n\t2.DELETE\n\t3.DISPLAY\n\t4.EXIT\n";
cout<<"\nEnter your option:";
cin>>ch;
switch(ch)
{
case 1: opt='y';
while(opt=='y')
{
cout<<"\nEnter the information:";
cin>>item;
cout<<"\nEnter the priority:";
cin>>prio;
PQ.insert(item,prio);
cout<<"\n\nDo you want to continue?(y/n):";
cin>>opt;
}
break;

case 2: opt='y';
while(opt=='y')
{
cout<<"\nEnter the item:";
cin>>item;
PQ.delet(item);
cout<<"\n\nDo you want to continue?(y/n):";
cin>>opt;
}
break;
case 3: opt='y';
PQ.disp();
break;
case 4: done=0;
break;
default: break;
}
} //end of while...
getch();
}

Floyd Warshall Algorithm

//program to illustrate Floyd Warshall Algorithm

#include
#include
#define inf 9999

class floyd
{
int i,j,n,k,a[20][20],p[20][20],t;

public:
void read();
void process();
int min(int,int);
void display();
};

void floyd::read()
{
cout<<"Floyd Warshall Algorithm\n";
cout<<"------------------------\n";
cout<<"Enter the number of vertices :";
cin>>n;
cout<<"Enter the weighted adjacency matrix :\n";

for ( i = 1;i <= n;i++ )
for ( j = 1;j <= n;j++ )
cin>>a[i][j];

process();
}

void floyd::process()
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(a[i][j]==0)
p[i][j]=inf;
else
p[i][j]=a[i][j];
}

for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
t=p[i][k]+p[k][j];
p[i][j]=min(p[i][j],t);
}
}
}

int floyd::min(int a,int b)
{
if(a return(a);
else
return(b);
}

void floyd::display()
{
cout<<"\nThe Shortest path is\n";

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(p[i][j]==inf)
cout<<"- ";
else
cout< }

cout<<"\n";
}
}

void main()
{
clrscr();

floyd f;

f.read();
f.display();
getch();
}



OUTPUT

Floyd Warshall Algorithm
------------------------
Enter the number of vertices :4
Enter the weighted adjacency matrix :
0 0 1 0
0 1 0 0
0 0 0 1
0 0 0 0

The Shortest path is
- - 12
- 1- -
- - - 1
- - - -

Dijkstra's Algorithm

//Program to illustrate Dijkstra's Algorithm

#include
#include
#include
#include

class dijkstra
{
int node[50], pred[50],dist[50],status[50],path[50];
int n,source,dest,adj[50][50],i,j,k,x,temp;
int infinity;

public:

dijkstra()
{
infinity=10000;
}
void read();
void process();
void display();
};

void dijkstra::read()
{
system("clear");
cout<<"Enter the number of nodes: ";
cin>>n;
cout<<"Enter the source node :";
cin>>source;
cout<<"Enter the destination node:";
cin>>dest;
if(source==dest)
{
cout<<"Source and Destination are same";
exit(0);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"Destination of "< cin>>adj[i][j];
}
pred[i]=0;
status[i]=0;
dist[i]=infinity;
node[i]=i;
}
process();

}

void dijkstra::process()
{
dist[source]=0;
temp=source;
status[source]=1;
for(x=1;x<=n;x++)
{
for(j=1;j<=n;j++)
if((adj[source][j]!=0) && (status[j]==0))
{
pred[j]=node[source];
dist[j]=dist[source]+adj[source][j];
}
infinity=10000;
for(k=1;k<=n;k++)

if(status[k]==0)
if(dist[k] {
infinity=dist[k];
source=node[k];
}
if(source==dest)
break;
else
{
status[source]=1;
}
}

path[1]=dest;
k=2;
for(i=1;i<=n;i++)
{
path[k]=pred[dest];
dest=pred[dest];
k++;
if(dest==temp)
break;
}
}

void dijkstra::display()
{
cout<<"Shortest path is:";
for(k=i+1;k>=1;k--)
cout<<"-->"< cout<<"\n";
}

void main()
{
clrscr();

dijkstra d;
d.read();
d.display();
getch();
}


OUTPUT

Enter the number of nodes: 3
Enter the source node :1
Enter the destination node:2
Destination of 1th node to 1th node:4
Destination of 1th node to 2th node:20
Destination of 1th node to 3th node:3
Destination of 2th node to 1th node:2
Destination of 2th node to 2th node:4
Destination of 2th node to 3th node:4
Destination of 3th node to 1th node:1
Destination of 3th node to 2th node:2
Destination of 3th node to 3th node:2
Shortest path is:-->1-->3-->2

Kruskal's Algorithm

//program to illustrate Kruskal's Algorithm

#include
#include
#include

struct graph
{
int u,v,wt,select;
};

class kruskal
{
graph x[30];
int n1,n2,rootn1,rootn2,mincost;
int w,k,n,c,i,j;
int father[10];

public:
kruskal()
{
k=1;
c=0;
}
void read();
void sort();
void display();
};

void kruskal::read()
{
cout<<"\n\t\tEnter the number nodes:";
cin>>n;
cout<<"\n\t\tEnter the weights\n";
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
cout<<"\t\t\t\t"<"< cin>>w;
if(w>0)
{
x[k].u=i;
x[k].v=j;
x[k].select=0;
x[k].wt=w;
k++;
}
}
}
sort();
}

void kruskal::sort()
{
struct graph temp;
int i,j;
for(i=1;i<=k;i++)
{
for(j=1;j<=k;j++)
{
if(x[j].wt>x[j+1].wt)
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
}

void kruskal::display()
{
cout<<"\n\t\tMinimal spanning tree is: ";
for(i=1;i<=k;i++)
{
if(c==n)
break;
n1=x[i].u;
n2=x[i].v;
while(n1>0||n2>0)
{
if(n1>0)
{
rootn1=n1;
n1=father[n1];
}
else
{
rootn2=n2;
n2=father[n2];
}
}
if(rootn1!=rootn2)
{
father[rootn2]=rootn1;
x[i].select=1;
cout<<"\n\t\t\t"<"< mincost+=x[i].wt;
c++;
}
}
cout<<"\n\t\tMinimum cost= "<< mincost;
}

void main()
{
kruskal k;

k.read();
k.display();
getch();
}



OUTPUT

Enter the number nodes:4

Enter the weights
1->2= 2
1->3= 3
1->4= 1
2->3= 2
2->4= 5
3->4= 6

Minimal spanning tree is:
1->4=1
1->2=2
2->3=2
Minimum cost= 5