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

Allprogs

The document contains C++ code implementing various sorting algorithms using templates including merge sort, bubble sort, insertion sort, and quick sort. It defines template classes for each sorting algorithm that take a generic data type, implement functions to get user input, sort the data, and display the output. Main functions demonstrate using the template classes to sort both integer and floating point data.

Uploaded by

nathansel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Allprogs

The document contains C++ code implementing various sorting algorithms using templates including merge sort, bubble sort, insertion sort, and quick sort. It defines template classes for each sorting algorithm that take a generic data type, implement functions to get user input, sort the data, and display the output. Main functions demonstrate using the template classes to sort both integer and floating point data.

Uploaded by

nathansel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

//Ex no:4

#include<iostream>
using namespace std;
const int SIZE=10;
class vector
{
private:
int *array;
public:
void *operator new(size_t s)
{
vector *my_vector;
my_vector=::new vector;
my_vector->array=new int[SIZE];
return my_vector;
}
void operator delete(void *vec)
{
vector *my_vect;
my_vect=(vector *) vec;
delete(int *)my_vect->array;
::delete vec;
}
void read();
int sum();
};
void vector::read()
{
for(int i=0;i<SIZE;i++)
{
cout<<"vector["<<i<<"]=?";
cin>>array[i];
}
}
int vector::sum()
{
int sum=0;
for(int i=0;i<SIZE;i++)
sum=sum+array[i];
return sum;
}
int main()
{
vector *my_vector=new vector;
cout<<"enter vector data..";
my_vector->read();
cout<<"sum of vector="<<
my_vector->sum();
delete my_vector;
}
//Exno 5:
#include<iostream>
using namespace std;
template <class type>
struct node
{
type data;
node* next;
};
template<class type>
class list
{
public:
list();
int length(void) const;
void makeempty(void);
void insert(void);
void remove(void);
void display(void);
private:
node<type>* linklist;
int count;
};
template <class type>
void list<type>::display(void)
{
node<type>* cur = linklist;
cout<<"\nThe linked list is\n";
while(cur!=NULL)
{
cout<<cur->data<<"->";
cur=cur->next;
}
cout<<"NULL\n";
}
template<class type>
list<type>::list()
{
count=0;
linklist=NULL;
}
template<class type>
int list<type>::length(void) const
{
return count;
}
template <class type>
void list<type>::makeempty(void)
{
node<type>* temp;
while(linklist !=NULL)
{
temp=linklist;
linklist=linklist->next;
delete temp;
}
count=0;
cout<<"\nNow List is empty";
}
template <class type>
void list<type>::insert(void)
{
node<type>* temp;
type item;
cout<<"\nEnter the item to insert";
cin>>item;
temp=new node<type>;
temp->data = item;
temp->next = linklist;
linklist=temp;
count++;
}
template<class type>
void list<type>::remove(void)
{
node<type>* cur = linklist;
type item;
cout<<"\nEnter the item to remove";
cin>>item;
node<type>* temp;
if(item==linklist->data)
{
temp = cur;
linklist=linklist->next;
}

else
{
while(!(item==(cur->next->data)))
cur=cur->next;
temp = cur->next;
cur->next = (cur->next)->next;
}
delete temp;
count--;
}
int main()
{
int ch;
list<int> list1;
while(1)
{
cout<<"\n\nSingle Linked List Menu\n";
cout<<"\n1.Insert \n2.Delete\n3.Empty\n4.Exit\n";
cout<<"\nEnter your Choice";
cin>>ch;
switch(ch)
{
case 1:
list1.insert();
list1.display();
break;
case 2:
if(list1.length()>0)
{
list1.remove();
list1.display();
}
else
cout<<"\nList Empty";
break;
case 3:
list1.makeempty();
break;
case 4:
exit(0);
default:
cout<<"\nInvalid Choice\n";
}
}
}

Ex.No.6a
Merge sort
#include<iostream>
#include<iomanip>
using namespace std;
template <class t>
class sort
{
t a[10];
public:
void get(int);
void merge(int,int);
void mergesort(int,int,int);
void display(int);
};
template <class t>
void sort <t>::get(int n)
{
int i;
cout<<"\n\n Enter the array elements:";
for(i=1;i<=n;i++)
cin>>a[i];
}
template <class t>
void sort <t>::display(int n)
{
int i;
cout<<"\n The sorted array is\n";
for(i=1;i<=n;i++)
cout<<a[i]<<setw(5);
}
template <class t>
void sort <t>::merge(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge(low,mid);
merge(mid+1,high);
mergesort(low,mid,high);
}
}
template <class t>
void sort<t>::mergesort(int low,int mid,int high)
{
t b[10];
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i=i+1;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i=i+1;
}
}
for(k=low;k<=high;k++)
a[k]=b[k];
}
int main()
{
int n;
cout<<"\n\t\t MERGE SORT USING TEMPLATES";
cout<<"\n\t\t";
sort<int>n1;
sort<float>n2;
cout<<"\n Enter the array size:";
cin>>n;
n1.get(n);
n1.merge(1,n);
n1.display(n);
n2.get(n);
n2.merge(1,n);
n2.display(n);
return 0;
}

Ex No 6.b
Bubble sort
#include<iostream>
#include<iomanip>
Using namespace std;
template <class t>
class bubble
{
t a[25];
public:
void get(int);
void sort(int);
void display(int);
};
template <class t>
void bubble <t>::get(int n)
{
int i;
cout<<"\nEnter the array elements:";
for(i=0; i<n;i++)
cin>>a[i];
}
template <class t>
void bubble <t>::display(int n)
{
int i;
cout<<"\n The sorted array is\n";
for(i=0;i<n;i++)
cout<<a[i]<<setw(10);
}
template <class t>
void bubble <t>::sort(int n)
{
int i,j;
t temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
int main()
{
int n;
bubble<int> b1;
bubble<float> b2;
cout<<"\n Bubble Sort on Integer Values";
cout<<"\n Enter the size of array:\n";
cin>>n;
b1.get(n);
b1.sort(n);
b1.display(n);
cout<<"\n Bubble Sort on Float Values";
cout<<"\n Enter the size of array:\n";
cin>>n;
b2.get(n);
b2.sort(n);
b2.display(n);
return 0;
}

Ex No:6.c
Insertion sort

#include<iostream>
#include<iomanip>
using namespace std;
template <class t>
class insertion
{
t a[25];
public:
void get(int);
void sort(int);
void display(int);
};
template <class t>
void insertion<t>::get(int n)
{
int i;
cout<<"\nEnter the array elements:";
for(i=0; i<n;i++)
cin>>a[i];
}
template <class t>
void insertion <t>::display(int n)
{
int i;
cout<<"\n The sorted array is\n";
for(i=0;i<n;i++)
cout<<a[i]<<setw(10);
}
template <class t>
void insertion <t>::sort(int n)
{
int i,j;
t temp;
for(i=1;i<n;i++)
{
j=i;
while(j>=1)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
j--;
}
}
}
int main()
{
int n;
insertion<int> i1;
insertion<float> i2;
cout<<"\n Insertion Sort on Integer Values";
cout<<"\n Enter the size of array:\n";
cin>>n;
i1.get(n);
i1.sort(n);
i1.display(n);
cout<<"\n Insertion Sort on Float Values";
cout<<"\n Enter the size of array:\n";
cin>>n;
i2.get(n);
i2.sort(n);
i2.display(n);
return 0;
}

Ex No: 6.d
Quick Sort
#include<iostream>
using namespace std;
template <class w>
class quick
{
w a[50];
int n;
public:
void get();
void sort(int,int);
int partition(int,int);
void put();
};
template <class w>
void quick <w>::get()
{
int i;
cout<<"\n Enter the no of terms:";
cin>>n;
cout<<"\n Enter the values:\n";
for(i=1;i<=n;i++)
cin>>a[i];
sort(1,n);
}
template <class w>
void quick <w>::sort(int p,int q)
{
int j;
if(p<q)
{
j=partition(p,q+1);
sort(p,j-1);
sort(j+1,q);
}
}
template <class w>
int quick <w>::partition(int m,int p)
{
int i,j,t;
w v;
v=a[m];
i=m;j=p;
do
{
do
{
i++;
}while(a[i]<v);
do
{
j--;
}while(a[j]>v);
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}while(i<j);
a[m]=a[j];
a[j]=v;
return j;
}
template <class w>
void quick<w>::put()
{
int i;
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
}
int main()
{

quick<int>q1;
quick<float>q2;
cout<<"\n\t\t QUICK SORT USING TEMPLATES";
cout<<"\n\t\t";
q1.get();
cout<<"\n\n Sorted array of integer values:\n";
q1.put();
q2.get();
cout<<"\n\n Sorted array of floating values:\n";
q2.put();
return 0;
}
//program 7a
#include<iostream>
#include<iomanip>
using namespace std;
class stack
{
int *s,max,top;
public:
class FULL{}; //for exception handling
class EMPTY{}; //for exception handling
stack(int);
void push(int);
int pop(void);
void display(void);
};
stack::stack(int m)
{
s=new int[m];
top=-1;
max=m;
}
void stack::push(int item)
{
if(top<max-1)
s[++top]=item;
else
throw FULL(); //FULL object is thrown
}
int stack::pop(void)
{
if(top>=0)
return s[top--];
else
throw EMPTY(); //EMPTY object is thrown
}
void stack::display(void)
{
if(top>=0)
for(int i=top;i>=0;i--)
cout<<"\n\t"<<setw(4)<<s[i]<<"\n\t";
else
throw EMPTY();
}
int main()
{
int item, size,ch=1;
cout<<"\nEnter the size of stack";
cin>>size;
stack s1(size);
cout<<"\nStack with Exception Handling";
cout<<"\n\n\tMENU\n1.PUSH\n2.POP\n3.SHOW STACK\n4.EXIT";
cout<<"\nEnter your choice";
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<"\nEnter the item to push";
cin>>item;
try
{
s1.push(item);
}
catch(stack::FULL) //FULL object is caught
{
cout<<"\n***Stack Overflow***\n";
}
break;
case 2:
try
{
cout<<"\nPoped Item is"<<s1.pop();
}
catch(stack::EMPTY) //EMPTY object caught
{
cout<<"\n***Stack Empty***\n";
}
break;
case 3:
cout<<"\nThe Stack is\n:";
try
{ s1.display(); }
catch(stack::EMPTY)
{
cout<<"\n***Stack Empty***\n";
}
break;
case 4:
exit(0);
}
cout<<"\nEnter your choice";
cin>>ch;
}while(ch<5);
return 0;
}
#include<iostream>
using namespace std;
#define MAX 50
#define TRUE1
#define FALSE 0
#define MAXINT 250
class node
{
public:
int no;
node(){ }
node(int a)
{
no=a;
}
};
class arc
{
public:
int adj,weight;
arc() { }
arc(int a)
{
adj=a;
}
};
class graph
{
public:
node nodes[MAX];
arc arcs[MAX][MAX];
graph(int n)
{
for(int i=1;i<=n;i++)
{
nodes[i].no=0;
for(int j=1;j<=n;j++)
arcs[i][j].adj=FALSE;
}
}
void join(node n1,node n2,int w)
{
arcs[n1.no][n2.no].adj=w;
arcs[n2.no][n1.no].adj=w;
}
void dispalyadj(int n)
{
cout<<"\nThe adjacent matrix\n";
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<"\t"<<arcs[i][j].adj;
cout<<"\n";
}
cout<<"\n";
}

void shortpath(int n)
{
int lcost[20];
int clost[20],i,j,k,min;
for(i=2;i<=n;i++)
{
lcost[i]=arcs[1][i].adj;
clost[i]=1;
}
cout<<"Minium cost spanning tree adges are:\n";
for(i=2;i<=n;++i)
{
min=lcost[2];
k=2;
for(j=3;j<=n;++j)
if(lcost[j]<min)
{
min=lcost[j];
k=j;
}
cout<<"\n"<<k<<"<->"<<clost[k];
lcost[k]=MAXINT;
for(j=2;j<=n;++j)
if((arcs[k][j].adj<lcost[j])&&(lcost[j]<MAXINT))
{
lcost[j]=arcs[k][j].adj;
clost[j]=k;
}
}
}
};
int main()
{
int n;
cout<<"\n Enter total no. of nodes..";
cin>>n;
graph g(n);
cout<<"\n assigning no for each node..";
for(int i=1;i<=n;i++)
g.nodes[i].no=i;
char ch='y';
int w;
do
{
node a,b;
cout<<"Creat path between the nodes";
cout<<"\nEnter source node..";
cin>>a.no;
cout<<"\nEnter destination node..";
cin>>b.no;
cout<<"\nEnter the weight";
cin>>w;
g.join(a,b,w);
cout<<"\nWant to continue [y]es [n]o";
cin>>ch;
}while(ch=='y');
g.dispalyadj(n);
g.shortpath(n);
return 0;
}
Exno9
#include<iostream>
#include<typeinfo>
using namespace std;
class shape
{
int linestyle;
int fillcolor;
public:
virtual float area()
{}
};
class circle:public shape
{
float radius,pi;

public:
circle()
{
pi=3.14;
}

float area()
{
cout<<"\nEnetr radius";
cin>>radius;
return radius*radius*pi;
}
};
class rectangle:public shape
{
float l,b;
public:
float area()
{
cout<<"\nEnetr lengeth & breadth";
cin>>l>>b;
return l*b;
}
};
class square:public shape
{
float a;
public:
float area()
{
cout<<"\nEnetr side";
cin>>a;
return a*a;
}
};
int main()
{
shape *ptr;
circle cir;
rectangle rect;
square sqr;
ptr=&cir;
cout<<"\n\nBase pointer now pointing to "<<typeid(*ptr).name();
cout<<"Area is :"<<ptr->area();
ptr=&rect;
cout<<"\n\nBase pointer now pointing to "<<typeid(*ptr).name();
cout<<"Area is :"<<ptr->area();
ptr=&sqr;
cout<<"\n\nBase pointer now pointing to "<<typeid(*ptr).name();
cout<<"Area is :"<<ptr->area();
return 0;
}

You might also like