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


Prim's Algorithm

//Program to illustrate Prims Algorithm
#include
#include
#include

class prims
{
int i,j,k,l,n;
int w[20][20],low;
char node[26],c,d;
public:
prims()
{
low=9999;
}
void read();
void disp();
void prim();
};

//function reads elements
void prims::read()
{
cout<<"Entrer the no of nodes:";
cin>>n;
if(n==1)
{
cout<<"Only one vertex.\n";
return;
}
cout<<"Enter the nodes:";
for(i=1;i<=n;i++)
cin>>node[i];
cout<<"Enter the weight of matrix:";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>w[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(w[i][j]==0)
w[i][j]=9999;
}


//function to calculate minimum spanning tree
void prims::prim()
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(w[i][j] {
low=w[i][j];
k=j;
}
else
w[i][j]=0;
}
j=j-1;
for(l=1;l<=n;l++)
{
if(l!=k)
w[i][l]=0;
}
low=9999;
}
}


//function to display tree
void prims::disp()
{
int p,q;
cout<<"\nMinimal spanning tree:\n";
for(p=1;p<=n;p++)
{
for(q=1;q<=n;q++)
{
if(w[p][q]!=0)
{
if(w[p][q]==w[q][p])
w[p][q]=w[q][p]=0;
cout< }
}
}
}


void main()
{
prims p;
clrscr();
cout<<”PRIM’S ALGORITHM\n-----------------------------\n”;
p.read();
p.prim();
p.disp();
getch();
}


OUTPUT


PRIM'S ALGORITHM
--------------------------------

Entrer the no of nodes:5
Enter the nodes:a b c d e
Enter the weight of matrix:
0 2 8 0 0
2 0 3 5 0
8 3 0 7 4
0 5 7 0 0
0 0 4 0 0

Minimal spanning tree:
a-b
c-b
d-b
e-c

Breadth First Search Algorithm

//program to perform Breadth First Search Algorithm
#include
#include

class bfs
{
char vertex[10],visit[10],v;
int gptr[10][10],queue[10],rear,front;
int n,u,v1,k;

public:
bfs()
{
front=0;
rear=0;
v1=k=0;
}
void input();
void bfs_am(int);
void enqueue(int);
int dequeue();
int search(int);
void insert(int);
void disp();
};

// read input
void bfs::input()
{
cout<<"Enter the no of vertices:";
cin>>n;
if(n==0)
cout<<"Tree is empty";
else if(n==1)
cout<<"\nThere is only one vertex";
else
{
cout<<"\nEnter the vertices:";
for(int i=1;i<=n;i++)
cin>>vertex[i];
cout<<"Enter the adjacency matrix:\n";
for(i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>gptr[i][j];
cout<<"\nEnter the vertex from which the traversal begin:";
cin>>v;

//find index of the source vertex
for(i=1;i<=n;i++)
{
if(vertex[i]==v)
{
v1=i;
}
if(v1!=0)
break;
}
cout<<"Traversal from "< bfs_am(v1);
}
}

void bfs::bfs_am(int v)
{
u=v;
enqueue(u);
while(front!=0)
{
u=dequeue();
if(search(u)==0)
{
insert(u);
for(int i=1;i<=n;i++)
if(gptr[u][i]==1)
enqueue(i);
}
}
}

//insert a vetex into the queue
void bfs::enqueue(int u1)
{
if(front==0)
front=rear=1;
else
rear=rear+1;
queue[rear]=u1;
}

//retreve a vertex from the queue
int bfs::dequeue()
{
int item;
if(front==rear)
{
item=queue[front];
front=rear=0;
}
else
{
item=queue[front];
front=front+1;
}
return item;
}


//insert a vertex into the array
void bfs::insert(int u1)
{
k=k+1;
visit[k]=vertex[u1];
}

int bfs::search(int u)
{
int flag=0;
if(k>=1)
{
for(int i=1;i<=k;i++)
{
if(visit[i]==vertex[u])
flag=1;
}
}
return flag;
}




void bfs::disp()
{
if(n==1)
return;
for(int i=1;i<=n;i++)
cout<<"\t"< }

void main()
{
clrscr();
bfs b;
cout<<”BREADTH FIRST SEARCH\n-----------------------------\n”;
b.input();
b.disp();
getch();
}


OUTPUT


Enter the no of vertices:4

Enter the vertices:a b c d
Enter the adjacency matrix:
0 1 1 0
1 0 0 0
1 0 0 1
0 0 1 0

Enter the vertex from which the traversal begin:a
Traversal from a is:
a b c d

Depth First Search Algorithm

//program to perform Depth First Search Algorithm
#include
#include

class dfs
{
char vertex[10],visit[10],v;
int gptr[10][10],stack[10],top;
int n,k,v1,u;
public:
dfs()
{
top=0;
k=0;v1=0;
}
void input();
void dfs_am(int);
void push(int);
int pop();
int search(int);
void insert(int);
void disp();
};

void dfs::input()
{
cout<<"Enter the no of vertices:";
cin>>n;
if(n==0)
cout<<"\nThe tree is empty";
else if(n==1)
cout<<"There is only one vertex";
else
{
cout<<"Enter the vertices:";
for( int i=1;i<=n;i++)
cin>>vertex[i];
cout<<"Enter the adjacency matrix:\n";
for(i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>gptr[i][j];
cout<<"\nEnter the vertex from which the traversal begin:";
cin>>v;

//find index of the source vertex
for(i=1;i<=n;i++)
{
if(vertex[i]==v)
v1=i;
if(v1!=0)
break;
}
cout<<"Traversal from "< dfs_am(v1);
}
}


void dfs::dfs_am(int v)
{
u=v;
push(u);
while(top!=0)
{
u=pop();
if(search(u)==0)
{
insert(u);
for(int i=1;i<=n;i++)
if(gptr[u][i]==1)
push(i);
}
}
}


//function to push a vertex into the stack
void dfs::push(int u1)
{
top=top+1;
stack[top]=u1;
}

//function to pop a vertex from the stack
int dfs::pop()
{
int item;
item=stack[top];
top=top-1;
return item;
}


//functon to unsert a vertex into the array visit
void dfs::insert(int u1)
{
k=k+1;
visit[k]=vertex[u1];
}

//functon to search a vertex already exist
int dfs::search(int u)
{
int flag=0;
if(k>=1)
{
for(int i=1;i<=k;i++)
{
if(visit[i]==vertex[u])
flag=1;
}
}
return flag;
}

void dfs::disp()
{
if(n==1)
return;
for(int i=1;i<=k;i++)
cout<<"\t"< }
void main()
{
dfs s;
clrscr();
cout<<”DEPTH FIRST SEARCH\n-----------------------------\n”;
s.input();
s.disp();
getch();
}




OUTPUT


Enter the no of vertices:4
Enter the vertices:a b c d
Enter the adjacency matrix:
0 1 1 0
1 0 0 0
1 0 0 1
0 0 1 0

Enter the vertex from which the traversal begin:a
Traversal from a is a c d b

Infix Expression Evaluation Using Expression Tree

//program to Evaluate Infix Expression Using Expression Tree
#include
#include
#include
#include
#include

class infix
{
typedef struct stack
{
char c;
struct stack *next;
}stack;
stack *top;
char ch,post[20],t,f;
int i,j,val[10],temp;

public:
infix()
{
top=NULL;
i=j=-1;
}

void read();
int precidence(char);
void push(char);
char pop();
};
void infix::read()
{
system("clear");
push('(');
cout<<"-----INFIX EXPRESSION EVALUATION-------\n";
cout<<"\nEnter the Infix expression : ";
do
{
ch=getchar();
if(ch=='\n')
{
t=ch;
ch=')';
}
if(ch=='(')
push(ch);
else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
post[++i]=ch;
else if(ch==')')
{
ch=pop();
while(ch!='(')
{
post[++i]=ch;
ch=pop();
}
}
else
{
f=pop();
if(precidence(ch)>precidence(f))
{
push(f);
push(ch);
}
else
{
post[++i]=f;
push(ch);
}
}
}while(t!='\n');
post[++i]='\0';
cout<<"\nPostfix Expression : ";
for(i=0;post[i]!='\0';++i)
cout< cout<<"\n";
for(i=0;post[i]!='\0';++i)
{
ch=post[i];
switch(ch)
{
case '*': val[j-1]*=val[j--];
break;
case '/': val[j-1]/=val[j--];
break;
case '+': val[j-1]+=val[j--];
break;
case '-': val[j-1]-=val[j--];
break;
case '^': temp=pow(val[j-1],val[j]);
val[--j]=temp;
break;
default: cout<<"Enter the value of "< cin>>val[++j];
break;
}
}
cout<<"\nResult after evaluation:"<}
int infix::precidence(char c)
{
switch(c)
{
case '(': return 0;
case '+':
case '-': return 1;
case '/':
case '%': return 2;
case '^':
case '$': return 3;

}
return 0;
}
void infix::push(char ch)
{
stack *p;
p=new stack;
p->c=ch;
p->next=top;
top=p;
}
char infix::pop()
{
stack *temp=top;
char ch=top->c;
top=top->next;
delete(temp);
return ch;
}
void main()
{
infix in;
in.read();

getch();
}

OUTPUT


-----INFIX EXPRESSION EVALUATION-------

Enter the Infix expression : a*b+c/d-e^f

Postfix Expression : ab*cd/ef^-+

Enter the value of 'a' :5
Enter the value of 'b' :6
Enter the value of 'c' :8
Enter the value of 'd' :4
Enter the value of 'e' :2
Enter the value of 'f' :3

Result after evaluation: 24

Binary Search Tree

//Program to illustarte Binary Search Tree
#include
#include

#define FALSE 0
#define TRUE 1

struct node
{
int data;
node *lchild;
node *rchild;
};


class btree
{
node *root;

public:
btree()
{
root=NULL;
}

void build_tree(int);
void insert(node**,int);
void inorder(node *);
void search(node **,int,node **, node**,int *);
void delet(int);
void remove(node **,int);
void traverse(int);
void disp();



};

void btree::insert(node **sr,int num)
{
if(*sr==NULL)
{
*sr=new node;
(*sr)->data=num;
(*sr)->lchild=(*sr)->rchild=NULL;
}
else
{
//if data less traverse list
if(num==(*sr)->data)
{
cout<<"\nCannot insert.\n";
return;
}
if(num<(*sr)->data)
insert(&(*sr)->lchild,num);
else
insert(&(*sr)->rchild,num);
}
}


void btree::build_tree(int num)
{
insert(&root,num);
}


void btree::delet(int num)
{
remove(&root,num);

}


void btree::traverse(int num)
{

int found;
node *parent,*x;


if(root==NULL)
{
cout<<"\nTree empty.\n\n";
return;
}

parent=x=NULL;

search(&root,num,&parent,&x,&found);

if(found==TRUE)
cout<<"\nNode Found.\n\n";
else
cout<<"\nNode not found.\n\n";



}


void btree::remove(node **sr,int num)
{
int found;
node *parent,*x,*xsucc;

if(*sr==NULL)
{
cout<<"\nTree empty .Deletion not possible.\n\n";
return;
}

parent=x=NULL;

search(&root,num,&parent,&x,&found);

if(found==FALSE)
{
cout<<"\nElement not found.\n\n";
return;
}

else
{
//the node to be deleted has two children
if(x->lchild!=NULL&&x->rchild!=NULL)
{
parent=x;
xsucc=x->rchild;

while(xsucc->lchild!=NULL)
{
parent=xsucc;
xsucc=xsucc->lchild;

}

x->data=xsucc->data;
x=xsucc;
}


//if the node has only left child

if(x->lchild!=NULL&&x->rchild==NULL)
{
if(parent->lchild==x)
parent->lchild=x->lchild;
else
parent->rchild=x->lchild;
delete x;
return;
}

//if the node has only right child



if(x->rchild!=NULL&&x->lchild==NULL)
{
if(parent->lchild==x)
parent->lchild=x->rchild;
else
parent->rchild=x->rchild;
delete x;
return;
}


//if the node to be deleted has no child

if(x->lchild==NULL&&x->rchild==NULL)
{
if(parent->rchild==x)
parent->rchild=NULL;
else
parent->lchild=NULL;
delete x;
return;
}

}
}


void btree::search(node **sr,int num,node **par,node **x,int *found)
{
node *q;
q=*sr;
*found=FALSE;
*par=NULL;

while(q!=NULL)
{
//if the node to be deleted is found
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data>num)
q=q->lchild;
else
q=q->rchild;

}
}

void btree::inorder(node *sr)
{ if(sr!=NULL)
{
inorder(sr->lchild);
cout<data<<"\t";
inorder(sr->rchild);

}
}



void btree::disp()
{
if(root==NULL)
cout<<"\nTree empty!!\n";
inorder(root);

}

void main()
{

clrscr();
int num,ch;
char opt;
btree bt;

cout<<"\n\tBINARY SEARCH TREE OPERATIONS\n\t-------------------------------\n\n";

do
{
cout<<"\n\n1.INSERT\n2.DELETE\n3.TRAVERSE\n4.DISPLAY\n";
cout<<"\nEnter the choice:";
cin>>ch;

switch(ch)
{
case 1:

cout<<"\nEnter the data:";
cin>>num;
bt.build_tree(num);
break;

case 2:

cout<<"\nEnter the data:";
cin>>num;
bt.delet(num);
break;


case 3: cout<<"\nEnter the data:";
cin>>num;
bt.traverse(num);
break;

case 4:
bt.disp();
break;
default:
cout<<"\nInvalid Option!!!\n";
break;

}

cout<<"\nDo u want to continue?";
cin>>opt;
}while(opt=='y'||opt=='Y');

getch();

}

Quick Sort Using Recursion

// program to perform Quicksort using recursion

#include
#include

class QuickSort
{
int a[10],n;
public:
void read();
void disp();
void qsort();
void quicksort(int,int);
int split(int,int);

};


void QuickSort::read()
{
cout<<"Enter the no of elements:";
cin>>n;

cout<<"Enter the elements:";
for(int i=0;i cin>>a[i];



}


void QuickSort::disp()
{
cout<<"\n\nSorted Array:\n\n";
for(int i=0;i cout< }


void QuickSort::qsort()
{
quicksort(0,n-1);

}

void QuickSort::quicksort(int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(lower,upper);
quicksort(lower,i-1);
quicksort(i+1,upper);
}
}

int QuickSort::split(int lower,int upper)
{

int i,p,q,temp;

p=lower+1;
q=upper;
i=a[lower];

while(q>=p)
{

while(a[p] p++;
while(a[q]>i)
q--;

if(q>p)
{
temp=a[q];
a[q]=a[p];
a[p]=temp;
}
}

temp=a[lower];
a[lower]=a[q];
a[q]=temp;

return q;
}





void main()
{

QuickSort qs;

clrscr();

qs.read();
qs.qsort();
qs.disp();

getch();


}






Mergesort Using Recursion

//Program to perform mergesort Using recursion

#include
#include

#define SIZE 10

class Array
{
int a[SIZE];
int n;

public:
void read();
void merge_sort(int,int);
void merge(int,int,int);
void display();

};


void Array::read()
{

cout<<"Enter the size of the array:";
cin>>n;
cout<<"Enter the "< for(int i=1;i<=n;i++)
cin>>a[i];

merge_sort(1,n);
}



void Array::merge_sort(int low,int high)
{
int mid;
if(low {
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}

}


//merges the subarrays
void Array::merge(int low,int mid,int high)
{
int i,j,k;
int c[SIZE];

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++;
}
}
if(i>mid)
{
for(int m=j;m<=high;m++,k++)
c[k]=a[m];
}

else
{
for(int m=i;m<=mid;m++,k++)
c[k]=a[m];
}

for(i=low;i<=high;i++)
a[i]= c[i];

}

void Array::display()
{
for(int i=1;i<=n;i++)
cout< }

void main()
{

Array a;

clrscr();
a.read();
cout<<"\nAfter Sorting the array is\n";
a.display();

getch();
}


OUTPUT


Enter the size of the array:6
Enter the 6 elements:
6
5
4
3
2
1


After Sorting the array is

1 2 3 4 5 6

Stack Using Array

//Program to implement stack using array

#include
#include
#include

#define SIZE 20

class stack
{
int arr[SIZE],top,ele;
public:
stack();
void push();
void pop();
void display();
};

stack::stack()
{
top=-1;
}

void stack::push()
{
if(top>SIZE-1)
{
cout<<"\nCannot perform push operation,Stack overflow!!!";
}
else
{
cout<<"\nEnter the element to be inserted";
cin>>ele;
arr[++top]=ele;
}
}

void stack::pop()
{
if(top==-1)
{
cout<<"\nCannot perform pop operation,Satck underflow!!!";
}
else
{
cout<<"\nThe element popped is "< }
}

void stack::display()
{
int i;
if(top==-1)
{
cout<<"\nStack is empty!!!!!";
}
else
{
cout<<"\nThe Stack is now\n";
for(i=top;i>=0;i--)
cout<";
}
}

void main()
{
stack s1;
int ch;
clrscr();
do
{
cout<<"\n------------Stack Operations-------------";
cout<<"\n1_PUSH";
cout<<"\n2_POP";
cout<<"\n3_DISPLAY";
cout<<"\n4_EXIT";
cout<<"\nEnter choice<1-4>:";
cin>>ch;

switch(ch)
{
case 1:
s1.push();
break;

case 2:
s1.pop();
break;

case 3:
s1.display();
break;

case 4:
exit(0);
break;

default:
cout<<"\nInvalid choice!!!!!";
break;
}
}while(ch!=4);
getch();
}

Stack using linked list

//Program to implement stack using linked list.

#include
#include
#include


struct node
{
int data;
node *LINK;
};

class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push(int);
void pop();
void display();
~stack();
};

void stack::push(int num)
{
node *temp;
temp=new node;
if(temp==NULL)
{
cout<<"\nThe Linked Stack is full";
return;
}
temp->data=num;
temp->LINK=NULL;
if(top==NULL)
{
top=temp;
return;
}
temp->LINK=top;
top=temp;
return;
}

void stack::pop()
{ node *temp;

if(top==NULL)
{
cout<<"\nThe Linked Stack is empty";
return;
}
temp=top;
cout<<"\nThe deleted element is "<data;
top=top->LINK;
delete temp;
}

void stack:: display()
{

node *temp;
if(top==NULL)
{
cout<<"\nThe Linked stack is empty";
return;
}
temp=top;
while(temp!=NULL)
{
cout<data<<" ";
temp=temp->LINK;
}
}

stack::~stack()
{
node *temp;

while(top!=NULL)
{
temp=top;
top=top->LINK;
delete temp;
}
}

void main()
{
stack s;
int num,c;
char ch;
clrscr();
do
{
cout<<"\n-----------------Linked Stack Operations-----------------";
cout<<"\n1_PUSH";
cout<<"\n2_POP";
cout<<"\n3_DISPLAY";
cout<<"\n4_EXIT";
cout<<"\nEnter your choice<1-4>:";
cin>>c;

switch(c)
{
case 1: cout<<"\nEnter the number to be pushed: " ;
cin>>num;
s.push(num);
break;
case 2:
s.pop();
break;

case 3:
s.display();
break;

case 4:
exit(0);
break;

default:
cout<<"\nInvalid choice";

}
cout<<"\nDo you want to continue:";
cin>>ch;
}while(ch=='y'||ch=='Y');

getch();
}

Queue using linked list

//program to implement queue using linked list

#include
#include
#include

class queue
{
struct node
{
int data;
node *link;
}*front,*rear;

public:
queue();
void enqueue(int);
void dequeue();
void display();
~queue();
};

queue::queue()
{
front=rear=NULL;
}

void queue::enqueue(int item)
{
node *temp;
temp=new node;

if(temp==NULL)
{
cout<<"\nQueue is Full.....cannot insert";
return;
}

temp->data=item;
temp->link=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}

void queue::dequeue()
{
node *temp;

if(front==NULL)
{
cout<<"\nThe Linked Queue is empty";
return;
}

temp=front;
cout<<"\nThe element deleted from linked queue ";
cout<data;
front=front->link;
delete(temp);
}

void queue::display()
{
node *temp;

temp=front;
cout<<"\nThe linked queue is now\n";
while(temp!=NULL)
{
cout<data<<"<--";
temp=temp->link;
}
}

queue::~queue()
{
node *temp;

if(front==NULL)
{
return;
}
while(front!=NULL)
{
temp=front;
front=front->link;
delete(temp);
}
}

void main()
{
int ch,item;
queue q1;

do
{
cout<<"\n------------Linked Queue Operations-------------\n";
cout<<"\n1_Enqueue";
cout<<"\n2_Dequeue";
cout<<"\n3_Display";
cout<<"\n4_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>ch;

switch(ch)
{
case 1:
cout<<"\nEnter the item to be inserted:";
cin>>item;
q1.enqueue(item);
break;

case 2:
q1.dequeue();
break;

case 3:
q1.display();
break;

case 4:
exit(0);
break;

default:
cout<<"\nInvalid choice";
break;
}
}while(ch!=4);
getch();
}

Double ended queue using array

//Progrom to illustrate double ended queue using array.
#include
#include
#include

#define SIZE 5

class dequeue
{

int a[10],front,rear,count;

public:
dequeue();
void add_at_beg(int);
void add_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void display();
};


dequeue::dequeue()
{
front=-1;
rear=-1;
count=0;
}


void dequeue::add_at_beg(int item)
{
if(front==-1)
{
front++;
rear++;
a[rear]=item;
count++;
}
else if(rear>=SIZE-1)
{
cout<<"\nInsertion is not possible,overflow!!!!";
}
else
{
for(int i=count;i>=0;i--)
{
a[i]=a[i-1];
}
a[i]=item;
count++;
rear++;
}
}



void dequeue::add_at_end(int item)
{

if(front==-1)
{
front++;
rear++;
a[rear]=item;
count++;
}
else if(rear>=SIZE-1)
{
cout<<"\nInsertion is not possible,overflow!!!";
return;
}
else
{
a[++rear]=item;
}


}



void dequeue::display()
{

for(int i=front;i<=rear;i++)
{
cout< }
}


void dequeue::delete_fr_front()
{
if(front==-1)
{
cout<<"Deletion is not possible:: Dequeue is empty";
return;
}
else
{
if(front==rear)
{
front=rear=-1;
return;
}
cout<<"The deleted element is "<
front=front+1;
}


}

void dequeue::delete_fr_rear()
{
if(front==-1)
{
cout<<"Deletion is not possible:Dequeue is empty";
return;
}
else
{
if(front==rear)
{
front=rear=-1;
}
cout<<"The deleted element is "<< a[rear];
rear=rear-1;
}


}



void main()
{
int c,item;
dequeue d1;
clrscr();
do
{
cout<<"\n\n****DEQUEUE OPERATION****\n";
cout<<"\n1-Insert at beginning";
cout<<"\n2-Insert at end";
cout<<"\n3_Display";
cout<<"\n4_Deletion from front";
cout<<"\n5-Deletion from rear";
cout<<"\n6_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>c;

switch(c)
{
case 1:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_beg(item);
break;

case 2:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_end(item);
break;

case 3:
d1.display();
break;

case 4:
d1.delete_fr_front();
break;
case 5:
d1.delete_fr_rear();
break;

case 6:
exit(1);
break;

default:
cout<<"Invalid choice";
break;
}

}while(c!=7);
getch();

}


















Doubly linked list

//program to illustrate Double ended linked list.

#include
#include
#include

struct node
{
int data;
node *prev,*next;
};

class dlist
{
struct node *header;

public:
dlist();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void delete_at_beg();
void delete_at_end();
void delete_at_pos(int);
void display();
~dlist();
};

dlist::dlist()
{
header->next=NULL;
header->prev=NULL;

}

void dlist::insert_at_beg(int num)
{
node *temp;

temp=new node;
temp->data=num;
if(temp==NULL)
{
cout<<"\nMemory is full!!!! cannot create new node";
return;
}
temp->next=header->next;
(header->next)->prev=temp;
header->next=temp;
temp->prev=header;
//temp->data=info;

}

void dlist::insert_at_end(int num)
{
node *q,*temp;

temp=new node;
temp->data=num;
if(temp==NULL)
{
cout<<"\nMemory is full!!!! cannot create new node";
return;
}
q=header;
while(q->next!=NULL)
{
q=q->next;
}
temp->next=NULL;
temp->prev=q;
q->next=temp;
}

void dlist::insert_at_pos(int num,int pos)
{
node *q,*temp;

temp=new node;
temp->data=num;
if(temp==NULL)
{
cout<<"\nMemory is full!!!! cannot create new node";
return;
}
q=header;
while(q->data!=pos)
{
q=q->next;
}
if(q->data==pos)
{
temp->prev=q;
temp->next=q->next;
q->next=temp;
}
}

void dlist::delete_at_beg()
{
node *q;

q=header;
if(q->next==NULL)
{
cout<<"\nThe Doubly Linked list is empty!!!!!!";
return;
}
q=q->next;
cout<<"\nThe element deleted from beginning is "<data;
header->next=q->next;
(q->next)->prev=header;

delete(q);
}

void dlist::delete_at_end()
{
node *q;

q=header;
if(q->next==NULL)
{
cout<<"\nThe Doubly Linked list is empty!!!!";
return;
}
while(q->next!=NULL)
{
q=q->next;
}
cout<<"\nThe element deleted is "<data;
(q->prev)->next=NULL;
delete(q);
}

void dlist::delete_at_pos(int ele)
{
node *q,*temp;

q=header;
if(q->next==NULL)
{
cout<<"\nThe Doubly linked list is empty!!!";
return;
}
while(q->data!=ele)
{
q=q->next;
}
if(q->data==ele)
{
temp=q->prev;
temp->next=q->next;
temp=q->next;
temp->prev=q->prev;
delete(q);
}
else
{
cout<<"\nThe element is not found in the linked list!!!";
}
}

void dlist::display()
{
node *t;
if(header->next==NULL)
{
cout<<"\nThe doubly linked list is empty!!!!";
return;
}
cout<<"\nThe doubly linked list is now!!";
for(t=header->next;t!=NULL;t=t->next)
{
cout<data<<"<-->";
}
}

dlist::~dlist()
{
node *q;
if(header==NULL)
{
return;
}
while(header!=NULL)
{
q=header->next;
delete(header);
header=q;
}
}
void main()
{
int ch,ele,pos,prev;
dlist d;
clrscr();

do
{
cout<<"\n----------Doubly Linked List Operations---------";
// cout<<"\n\n---------Insert Options-----------";
cout<<"\n1_Insert at Begin";
cout<<"\n2_Insert at End";
cout<<"\n3_Insert at Given position";
// cout<<"\n\n-----------Delete Options----------";
cout<<"\n4_Delete at Beginning:";
cout<<"\n5_Delete at End";
cout<<"\n6_Delete at Given position";
cout<<"\n7_Display Doubly linked list";
cout<<"\n8_EXIT";
cout<<"\nEnter your choice<1-8>:";
cin>>ch;

switch(ch)
{
case 1:
cout<<"\nEnter the element to be inserted:";
cin>>ele;
d.insert_at_beg(ele);
break;

case 2:
cout<<"\nEnter the element to be inserted:";
cin>>ele;
d.insert_at_end(ele);
break;
case 3:
cout<<"\nEnter the element to be inserted:";
cin>>ele;
cout<<"\nEnter the element after which insertion is to be performed:";
cin>>pos;
d.insert_at_pos(ele,pos);
break;
case 4:
d.delete_at_beg();
break;
case 5:
d.delete_at_end();
break;
case 6:
cout<<"\nEnter the element to be deleted:";
cin>>pos;
d.delete_at_pos(pos);
break;
case 7:
d.display();
break;
case 8:
exit(0);
break;
default:
cout<<"\nInvalid choice!!!!";
break;
}
d.display();
}while(ch!=8);
getch();
}

Circular Queue using array

//program to implement circular queue using an array.

#include
#include
#include

#define SIZE 5

class cqueue
{
int arr[SIZE];
int rear,front;

public:
cqueue();
void enqueue(int);
void dequeue();
void display();
};

cqueue::cqueue()
{
front=rear=-1;
}

void cqueue::enqueue(int item)
{
int next;

if(front==-1)
{
front=rear=0;
arr[rear]=item;
}
else
{
next=(rear%SIZE)+1;
if(next==SIZE)
{
next=0;
}
if(next==front)
{ cout<<"\nCircular queue is full!!!";
//
}
else
{
rear=next;
arr[rear]=item;
//cout<<"\nCircular queue is full!!!";
}
}
}

void cqueue::dequeue()
{
int item;

if(front==-1)
{
cout<<"\nThe queue is empty!!!";
return;
}
else
{
item=arr[front];
cout<<"\nThe element deleted from circular queue is "< if(front==rear)
{
front=rear=-1;
}
else
{
front=(front%SIZE)+1;
}
}
}

void cqueue::display()
{
int i=0,next;

if(front==-1)
{
cout<<"\nCircular Queue is empty !!!!";
return;
}
do
{
next=(front%SIZE)+i;
if(next>=SIZE)
next=next-SIZE;

cout<";
i++;
}while(next!=rear);

}

void main()
{
int data,ch;
cqueue c1;

clrscr();
do
{
cout<<"\n-----------Circular Queue-----------";
cout<<"\n1_Enqueue";
cout<<"\n2_Dequeue";
cout<<"\n3_Display";
cout<<"\n4_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the item to be inserted:";
cin>>data;
c1.enqueue(data);
c1.display();
break;

case 2:
c1.dequeue();
break;

case 3:
c1.display();
break;

case 4:
exit(0);
break;

default:
cout<<"\nInvalid choice!!!!";
break;
}
}while(ch!=4);
getch();
}

Array operations

/*program to perform array operations
1_Insertion
2_Deletion
3_Searching
4_Sorting
5_Merging
6_Traversal*/

#include
#include
#include

#define SIZE 10

class array
{
int a[SIZE],n;

public:
array();
void read();
void insert();
void move_ins(int);
void delet();
void move_del(int);
void search();
void sort();
void merge(array,array);
void display();
};

array::array()
{
int i;
for(i=0;i a[i]=-1;
};

void array::read()
{
int i;
cout<<"\nEnter the number of elements:";
cin>>n;
if(n>SIZE)
{
cout<<"Array Limit Exceeded!!!Please Re-try";
return;
}
cout<<"\nEnter the "< for(i=0;i cin>>a[i];
}

void array::insert()
{
int pos=0,ch,ele;

do
{
cout<<"\n------Insertion------";
cout<<"\n1_At Begin";
cout<<"\n2_At End";
cout<<"\n3_Given Position";
//cout<<"\n4_Next Position";
cout<<"\n4_Return to Main Menu";
cout<<"\nEnter choice<1-4>:";
cin>>ch;

if(n+1>SIZE)
{
cout<<"\nArray Overflow!!! Cannot insert element";
}
else
{
switch(ch)
{
case 1:
cout<<"\nEnter the element to be inserted(non -ve integer):";
cin>>ele;
pos=0;
move_ins(pos);
a[pos]=ele;
break;

case 2:
cout<<"\nEnter the element to be inserted(non -ve integer):";
cin>>ele;
pos=n;
move_ins(pos);
a[pos]=ele;
break;

case 3:
cout<<"\nEnter the element to be inserted(non -ve integer):";
cin>>ele;
cout<<"\nEnter the position of the element:";
cin>>pos;
pos--;
if(pos>SIZE-1||pos>n)
cout<<"\nInvalid position!!!cannot insert element";
else
{
move_ins(pos);
a[pos]=ele;
}
break;

/*case 4:
cout<<"\nEnter the element to be inserted:";
cin>>ele;
pos=pos+1;
if(pos>SIZE-1)
cout<<"\nInvalid position!!!cannot insert element";
else
{
move_ins(pos);
a[pos]=ele;
}
break;*/

case 4:
return;
//break;

default:
cout<<"\nInvalid choice";
break;
}
display();
}
}while(ch!=5);
}

void array::move_ins(int p)
{
int i;
for(i=n;i>p;i--)
{
a[i]=a[i-1];
}
n++;
}

void array::delet()
{
int ch,pos;



do
{
if(n==0)
{
cout<<"\nInsert first!!!!";
return;
}
cout<<"\n-------Deletion----------";
cout<<"\n1_Begin";
cout<<"\n2_End";
cout<<"\n3_Given Position";
cout<<"\n4_Return to Main Menu";
cout<<"\nEnter choice<1-4>:";
cin>>ch;

switch(ch)
{
case 1:
pos=0;
move_del(pos);
break;

case 2:
pos=n-1;
cout<<"\nThe element deleted is "< a[pos]=-1;
n=n-1;
break;

case 3:
cout<<"\nEnter the position of the element to be deleted:";
cin>>pos;
pos--;
if(pos>n)
{
cout<<"\nCannot Insert !!!!!Invalid Position";
return;
}
move_del(pos);
break;

case 4:
return;
//break;

default:
cout<<"\nInvalid Choice!!!!";
break;
}
display();
}while(ch!=4);

}


void array::move_del(int p)
{
int i;
cout<<"\nThe element deleted is "< for(i=p;i {
a[i]=a[i+1];
}
n--;
}

void array::search()
{
int ele,flag=0,i,pos;

if(n==0)
{
cout<<"\nThe array is empty!!!!";
return;
}

cout<<"\nEnter the element to be searched:";
cin>>ele;
for(i=0;i {
if(a[i]==ele)
{
pos=i;
flag=1;
break;
}
}
if(flag==0)
cout<<"\nThe element "< else
cout<<"\nThe element "< }
void array::sort()
{
int i,j,ch,temp;

if(n==0)
{
cout<<"\nThe array is empty";
return;
}

do
{
cout<<"\n------Sorting-------";
cout<<"\n1_Ascending Sort";
cout<<"\n2_Descending Sort";
cout<<"\n3_Main Menu";
cout<<"\nEnter choice<1-3>:";

cin>>ch;

switch(ch)
{
case 1:
for(i=0;i {
for(j=i+1;j {

while(a[i]==-1)
i++;

while(a[j]==-1)
j++;

if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
display();
break;
case 2:
for(i=0;i {
for(j=i+1;j {

while(a[i]==-1)
i++;
while(a[j]==-1)
j++;

if(a[i] {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
display();
break;

case 3:
return;

default:
cout<<"\nInvalid Choice!!!";
break;
}
}while(ch!=3);
}

void array::merge(array arr1,array arr2)
{
int i,j;
if((arr1.n+arr2.n)>2*SIZE)
{
cout<<"\nCannot perform merging...array cannot hold elements!!!";
}
else
{
for(i=0;i {
a[i]=arr1.a[i];
}
for(i=arr1.n,j=0;i<(arr1.n+arr2.n);i++,j++)
{
a[i]=arr2.a[j];
}
}
}

void array::display()
{
int i;

if(n==0)
{
cout<<"\nThe array is empty!!!!";
return;
}
cout<<"\nArray is now ";
for(i=0;i {
if(a[i]!=-1)
cout<<"\na["< }
}

void main()
{
int ch;
array a1,a2,a3,a4;
clrscr();
do
{
cout<<"\n----------Array operations----------";
cout<<"\n1_Read";
cout<<"\n2_Insert";
cout<<"\n3_Delete";
cout<<"\n4_Search";
cout<<"\n5_Sort";
cout<<"\n6_Display";
cout<<"\n7_Merge";
cout<<"\n8_Exit";
cout<<"\nEnter choice<1-8>:";
cin>>ch;

switch(ch)
{
case 1:
a1.read();
break;
case 2:
a1.insert();
break;
case 3:
a1.delet();
break;
case 4:
a1.search();
break;
case 5:
a1.sort();
break;
case 6:
a1.display();
break;
case 7:
cout<<"\nEnter the first array\n";
a2.read();
cout<<"\nEnter the second array\n";
a3.read();
cout<<"\nThe first ";
a2.display();
cout<<"\nThe second ";
a3.display();
a4.merge(a2,a3);
cout<<"\nmerged array\n";
a4.display();
break;
case 8:
exit(0);
break;
default:
cout<<"\nInvalid choice!!!!";
break;

}
}while(ch!=8);
getch();
}

Merge two arrays

//Program to merge two arrays

#include
#include

#define SIZE 20
class array
{
int a[SIZE];
int n;

public:
array();
void read();
void sort(array,array);
void merge(array,array);
void display();
};

array::array()
{
int i;
n=0;
for(i=0;i<<"\nEnter the number of elements:"; cin>>n;
if(n>SIZE)
{
cout<<"The array cannot hold "<<<" elements...please Re-enter size"; goto start; } else { cout<<"\nEnter the "<<<" elements of array:"; for(i=0;i>a[i];
}
}

void array::sort(array a1,array a2)
{
int i,j,ch,temp;

for(i=0;ia1.a[j])
{
temp=a1.a[i];
a1.a[i]=a1.a[j];
a1.a[j]=temp;
}
}
}
for(i=0;i2*SIZE)
{
cout<<"\nCannot perform merging...array cannot hold elements!!!";
}
else
{
for(i=0,j=0;i<(arr1.n+arr2.n);i++,j++)
{
a[i]=arr2.a[j];
}
}
}

void array::display()
{
int i;
for(i=0;i
{
cout<<" "<
}
}

void main()
{
array a1,a2,a3;

a1.read();
a2.read();

cout<<"\nThe first array is \n";
a1.display();

cout<<"\nThe Second array is \n";
a2.display();

a3.merge(a1,a2);

cout<<"\nThe merged array is\n";
a3.display();
getch();
}

Linked List Operations

/*Program to create a linked list and perform the following operations
1_Append
2_Add @ begin
3_Add @ Next Position
4_Delete
5_Display */

#include
#include
#include

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

class linklist
{
node *p;

public:
linklist();
void append(int);
void add_at_beg(int);
void add_after(int,int);
void del(int);
void display();
int count();
~linklist();

} ;

linklist::linklist()
{
p=NULL;
}


void linklist::append(int num)
{
node *q,*t;
//checks if list is empty
if(p==NULL)
{
//insert first node
p=new node;
p->data=num;
p->link=NULL;
}

else
{
q=p;
while(q->link!=NULL)
{
q=q->link;
}

t=new node;
t->data=num;
t->link=NULL;
q->link=t;
}
}


void linklist::add_at_beg(int num)
{
node *t;
t=new node;
t->data=num;
t->link=p;
p=t;
}


//add a node after a specified number of node

void linklist::add_after(int num,int c)
{
node *q,*t;
q=p;
//skip to the desired position
for(int i=1;i {
q=q->link;
//if end of linklist is encountered
if(q==NULL)
{
cout<<"There are less than "< return;
}
}
//insert new node
t=new node;
t->data=num;
t->link=q->link;
q->link=t;
}

//delete the specified node linklist

void linklist::del(int num)
{
node *q;
q=p;

//traverse linklist till the last but one node is reached
while(q!=NULL)
{
//if node to be deleted in front node
if(q->data==num)
{
p=q->link;
delete(q);
return;
}
q=q->link;
}
cout<<"Element "<
}

void linklist::display()
{
node *q;

cout<<"The linklist is\n";
for(q=p;q!=NULL;q=q->link)
cout<data<<"->";
}


int linklist::count()
{
node *q;
int count=0;
q=p;
while(q!=NULL)
{
count++;
q=q->link;
}
return(count);
}

linklist::~linklist()
{

node *q;
if(p==NULL)
return;

while(p!=NULL)
{
q=p->link;
delete(p);
p=q;
}
}

void main()
{
int c,ele,pos;
clrscr();
linklist l;
do
{
cout<<"\n------Link List operations---------";
cout<<"\n1_Append";
cout<<"\n2_Add at Beginning";
cout<<"\n3_Add at Next position:";
cout<<"\n4_Deletion";
cout<<"\n5_Count the linkedlist";
cout<<"\n6_Display Linkedlist";
cout<<"\n7_Exit";
cout<<"\nEnter choice<1-7>:";
cin>>c;
switch(c)
{
case 1:
cout<<"\nEnter the item to append:";
cin>>ele;
l.append(ele);
break;

case 2:
cout<<"\nEnter the item to insert at beginning:";
cin>>ele;
l.add_at_beg(ele);
break;

case 3:
cout<<"\nEnter the position to insert:";
cin>>pos;
cout<<"\nEnter the item to be inserted:";
cin>>ele;
l.add_after(ele,pos);
break;

case 4:
cout<<"\nEnter the item to delete:";
cin>>ele;
l.del(ele);
break;

case 5:
cout<<"\nThe number of nodes in linked list is "< break;

case 6:
l.display();
break;

case 7:
exit(0);
break;

default:
cout<<"\nInvalid choice!!!";
break;
}
}while(c!=7);
getch();
}

Queue using array

#include
#include
#include

#define SIZE 20

class queue
{
int a[SIZE];
int front,rear;

public:
queue();
void insert();
void delet();
void display();
};

void queue::queue()
{
front=rear=-1;
}

void queue::insert()
{
int ele;

if(front==-1)
{
cout<<"\nEnter the element to be inserted:";
cin>>ele;
front++;
a[++rear]=ele;
display();
}

else if(rear>SIZE-1)
{
cout<<"\nThe queue is full....cannot insert elements";
}

else
{
cout<<"\nEnter the element to be inserted:";
cin>>ele;
a[++rear]=ele;
display();
}

}

void queue::delet()
{
if((front==rear+1)||front==-1)
{
cout<<"\nThe queue is empty.......cannot perform deletion";
front=rear=-1;
}

else
{
cout<<"The element deleted from queue is "< front++;
display();
}
}

void queue::display()
{
int i;

if((front==rear+1)||front==-1)
{
cout<<"\nThe queue is empty.......cannot perform deletion";
front=rear=-1;
}
else
{
cout<<"\nThe queue is now\n";
for(i=front;i<=rear;i++)
{
cout< }
}
}

void main()
{
int ch;
queue q1;
clrscr();
do
{
cout<<"\n--------------Queue operations--------------";
cout<<"\n1_Insertion";
cout<<"\n2_Deletion";
cout<<"\n3_Display";
cout<<"\n4_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>ch;

switch(ch)
{
case 1:
q1.insert();
break;

case 2:
q1.delet();
break;

case 3:
q1.display();
break;

case 4:
exit(0);
break;

default:
cout<<"\nInvalid choice!!!!";
break;
}
}while(ch!=4);
getch();
}

Binary search on array

#include
#include

#define SIZE 20

class array
{
int a[SIZE];
int n;

public:
array();
void read();
void binary_search();
void display();
};

array::array()
{
n=0;
}

void array::read()
{
int i;
cout<<"\nEnter the size of the array:";
cin>>n;
cout<<"\nEnter the "< for(i=0;i {
cin>>a[i];
}
}

void array::binary_search()
{
int i,j,mid,lb,ub,flag;
int temp,item;

for(i=0;i {
for(j=i+1;j {
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\nThe sorted array is:\n";
for(i=0;i cout<<"\t" < cout<<"\nEnter the item to be searched:";
cin>>item;



for(lb=0,ub=n-1;lb {
mid=(lb+ub)/2;
if(a[mid]==item)
{
flag=1;
break;
}
else if(item>a[mid])
{
lb=mid+1;
}
else if(item {
ub=mid-1;
}
else
{
flag=0;
}
}
if(flag==1)
{
cout<<"\n\nThe element "< }
else
{
cout<<"\nThe element is not found in array!!!!";
}
}

void array::display()
{
int i;

cout<<"\nThe array is \n";
for(i=0;i {
cout< }
}

void main()
{
array a1;

clrscr();

a1.read();
a1.display();
a1.binary_search();

getch();
}