You are on page 1of 7

// program 10 : program to implement queue using templates.

#include<iostream.h>
#include<stdlib.h>

#define size 5

template<class t>
class queue
{
t info[size];
int front,rear;

public:
queue()
{
front=0;
rear=-1;
}
void insert(t);

void delete_i();
void display();
};

template<class t>
void queue<t>::insert(t x)
{
if(rear==size-1)
{
cout<<"\nqueue overflow ..\n";
return;
}
info[++rear]=x;
display();
}

template<class t>
void queue<t>::delete_i()
{
if(front>rear)
{
cout<<"\nthe queue is empty..\n";
return;
}
cout<<"\nthe deleted item is : "<<info[front++];
}

template<class t>
void queue<t>::display()
{
if(front>rear)
{
cout<<"\nthe queue is empty..\n";
return;
}
int i=front;
cout<<"\nthe queue contents are :\n";
while(i<=rear)
cout<<info[i++]<<" ";
}

void main()
{
queue<int>q1;
queue<double>q2;
while(1)
{
cout<<"\n--------------- menu --------------\n"
<<"1.insert.\n2.delete.\n3.display.\n4.exit.\n"
<<"-----------------------------------\n"
<<"enter your choice : ";
int ch;
cin>>ch;
switch(ch)
{
case 1: cout<<"\n1.integer.\n2.double.\n\n"
<<"enter your choice : ";
int type;
cin>>type;
switch(type)
{
case 1: cout<<"\nenter a integer to be inserted :
";
int x;
cin>>x;
q1.insert(x);
break;

case 2: cout<<"\nenter a double no. to be


inserted : ";
double y;
cin>>y;
q2.insert(y);
break;
}
break;

case 2: q1.delete_i();
q2.delete_i();
break;

case 3: q1.display();
q2.display();
break;

case 4: exit(0);
}
}
}

/* ---------- output ----------

--------------- menu --------------


1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 1

1.integer.
2.double.

enter your choice : 1

enter a integer to be inserted : 10

the queue contents are :


10
--------------- menu --------------
1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 1

1.integer.
2.double.

enter your choice : 2

enter a double no. to be inserted : 20.5

the queue contents are :


20.5
--------------- menu --------------
1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 1

1.integer.
2.double.

enter your choice : 30

--------------- menu --------------


1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 1

1.integer.
2.double.

enter your choice : 2


enter a double no. to be inserted : 10.5

the queue contents are :


20.5 10.5
--------------- menu --------------
1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 3

the queue contents are :


10
the queue contents are :
20.5 10.5
--------------- menu --------------
1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 2

the deleted item is : 10


the deleted item is : 20.5
--------------- menu --------------
1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 2

the queue is empty..

the deleted item is : 10.5


--------------- menu --------------
1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 2

the queue is empty..

the queue is empty..

--------------- menu --------------


1.insert.
2.delete.
3.display.
4.exit.
-----------------------------------
enter your choice : 4
*/
plain text attachment [ scan and save to computer | save to yahoo! briefcase ]

// program 5: program to implement quick sort using templates.

#include<iostream.h>
#include<stdlib.h>

#define max 10

template<class t>
class qsort
{
t a[max];
int l;
public:
qsort(int);
void getarray();
void swap(t&,t&);
int partition(int,int);
void quicksort(int,int);
void printarray();
};

template<class t>
qsort<t>::qsort(int length)
{
l=length;
for(int i=0;i<l;i++)
a[i]=0;
}

template<class t>
void qsort<t>::getarray()
{
cout<<"\nenter elements for array : \n";
for(int i=0;i<l;i++)
cin>>a[i];
}

template<class t>
void qsort<t>::swap(t &a,t &b)
{
t t;
t=a;
a=b;
b=t;
}

template<class t>
int qsort<t>::partition(int l,int h)
{
t key;
int i,j;
i=l;
j=h;
key=a[l];
while(1)
{
while(i<h && (key>=a[i]))
i++;
while(key<a[j])
j--;
if(i<j)
swap(a[i],a[j]);
else
swap(a[l],a[j]);
return j;
}
}

template<class t>
void qsort<t>::quicksort(int l,int h)
{
int pos;
if(l<h)
{
pos=partition(l,h);
quicksort(l,pos-1);
quicksort(pos+1,h);
}
}

template<class t>
void qsort<t>::printarray()
{
cout<<"\nthe sorted array is : ";
for(int i=0;i<l;i++)
cout<<a[i]<<" ";
cout<<endl;
}

void main()
{
cout<<"\nenter the limit : ";
int n;
cin>>n;
qsort<int>ia(n);
qsort<double>da(n);
while(1)
{
cout<<"\n1.integer sort.\n2.double sort.\n3.exit.\n"
<<"---------------------------\n"
<<"enter your choice : ";
int ch;
cin>>ch;
switch(ch)
{
case 1: ia.getarray();
ia.quicksort(0,n-1);
ia.printarray();
break;

case 2: ia.getarray();
ia.quicksort(0,n-1);
ia.printarray();
break;

case 3: exit(0);
}
}
}

/* --------- output ---------

enter the limit : 5

1.integer sort.
2.double sort.
3.exit.
---------------------------
enter your choice : 1

enter elements for array :


2 9 4 6 1

the sorted array is : 1 2 4 6 9

1.integer sort.
2.double sort.
3.exit.
---------------------------
enter your choice : 2

enter elements for array :


5 9 4 1 6

the sorted array is : 1 4 5 9 6

You might also like