template yapıda stack, kuyruk ve binary agac sınıfları
Kod:
/*
Bu kütüphanede template yapıda stack ,kuyruk ve binary ağaç sınıfları tanımlanmıştır.Hepsini
kontrol ettim ama eğer bir problemle karsılasırsanız cvp yazabilir veya
[email protected] 'a mail atabilirsiniz
*/
#define top front
template<class tip>
class veri
{
public:
virtual int ekle(tip)=0;
virtual tip cek()=0;
veri(int =100);
protected:
int front; //stackde top olarak kullanılacak
int rear;
int var; //queue için
tip *nodes;
int max;
};
template<class tip>
veri<tip>::veri(int max_eleman_sayisi)
{ max=max_eleman_sayisi;
front=0;
rear=0;
var=0;
nodes=new tip[max_eleman_sayisi];
}
template<class tip>
class stack : public veri<tip>{
public:
int ekle(tip);
tip cek();
void degeral(tip *&,int &,int &);
int empty(){return((top==-1)?1:0);}
int full() {return((top==max-1)?1:0);}
stack<tip> operator=(const stack<tip> &);
stack(int max_size=100):veri<tip>(max_size){top=-1;}
void printall();
};
template<class tip>
void stack<tip>::printall()
{
for(int a=0;a<=top;a++)
cout<<nodes[a]<<" ";
cout<<"\n";
}
template<class tip>
void stack<tip>::degeral(tip *&nod,int &tp,int &mx)
{
tp=top,
mx=max;
nod=nodes;
}
template<class tip>
stack<tip> stack<tip>::operator=(const stack<tip> &y)
{
tip *temp;
y.degeral(temp,top,max);
nodes=new tip[max];
for(int a=0;a<=top;a++)
nodes[a]=temp[a];
return *this;
}
template<class tip>
int stack<tip>::ekle(tip x)
{ /*buraya görsel uyarı koyulabilir fakat kütüphane konsol ve
win32 uyguamalarında da kullanılacağından dolayı eğer stack dolduysa
başarısızlık durumunu belirtmek için hata olarak 0 değeri döndürür*/
if(full())
return 0;
else
{
nodes[top+1]=x;
top++;
}
return 1;
}
template<class tip>
tip stack<tip>::cek()
{
if(empty())
unexpected();
else
{
top--;
return nodes[top+1];
}
}
/*XXXXXXXXXXXXXXXXXXXXXXX queue XXXXXXXXXXXXXXXXXXX*/
template <class tip>
class queue : public veri<tip>
{
public:
int ekle(tip);
tip cek();
int empty(){return((var==0)?1:0);}
int full() {return((var==max)?1:0);}
void degeral(tip *&,int &,int&,int&,int &);
queue<tip> operator=(const queue<tip> &y);
queue(int max_size=100):veri<tip>(max_size){}
void printall();
};
template<class tip>
void queue<tip>::printall()
{
for(int a=front;a<var+front;a=(a+1)%max)
cout<<nodes[a]<<" ";
cout<<endl;
}
template<class tip>
queue<tip> queue<tip>::operator=(const queue<tip> &y)
{
tip *temp;
y.degeral(temp,front,rear,var,max);
nodes=new tip[max];
for(int a=0;a<max;a++)
nodes[a]=temp[a];
return *this;
}
template<class tip>
void queue<tip>::degeral(tip *&nod,int &frnt,int &rer,int &vr,int &mx)
{
vr=var;
nod=nodes;
rer=rear;
frnt=front;
mx=max;
}
template <class tip>
int queue<tip>::ekle(tip x)
{
if(full())
return 0;
else
{
nodes[rear]=x;
var++;
rear=(rear+1)%max;
}
return 1;
}
template<class tip>
tip queue<tip>::cek()
{
if(empty())
unexpected();
else
{
front=(front+1)%max;
}
return(nodes[(front-1)%max]);
}
template <class tip>
class dugum
{
public:
tip data;
dugum<tip> *sol,*sag;
dugum(){sol=0;sag=0;}
void solaekle(tip);
void sagaekle(tip);
};
template <class tip>
void dugum<tip>::solaekle(tip veri)
{
sol=new dugum<tip>;
sol->data=veri;
}
template <class tip>
void dugum<tip>::sagaekle(tip veri)
{
sag=new dugum<tip>;
sag->data=veri;
}
///*********** sayı düğümden küçükse sağa, büyükse sol kola eklenecek ******////
template<class tip>
class agac
{
dugum<tip> *root;
dugum<tip> * uygunyer(tip, dugum<tip> *);
dugum<tip> * takas(tip,dugum<tip> *&);
void ShowInorder(dugum<tip> *); //sol orta sağ
void ShowPreorder(dugum<tip> *); // orta sol sağ
void ShowPostorder(dugum<tip> *); //Parelel aşağıdan yukarı
public:
void ekle(tip);
//void sil(tip);
dugum<tip> * bul(tip);
void SolGoster();
void OrtaGoster();
void SondanGoster();
agac();
};
template<class tip>
dugum<tip> * agac<tip>::takas(tip x,dugum<tip> *&ara)
{
if (ara->sol==0)
{
if(ara->sag!=0)
return sag;
else
takas(x,ara->sag);
}
takas(x,ara->sol);
}
template<class tip>
dugum<tip> * agac<tip>::bul(tip)
{
takas
}
template<class tip>
void agac<tip>::SondanGoster()
{
ShowPostorder(root);
}
template<class tip>
void agac<tip>::SolGoster()
{
ShowInorder(root);
}
template<class tip>
void agac<tip>::ShowPostorder(dugum<tip> *p)
{
if(p==0)
return;
ShowPreorder(p->sol);
ShowPreorder(p->sag);
cout<<p->data<<" ";
}
template<class tip>
void agac<tip>::ShowPreorder(dugum<tip> *p)
{
if(p==0)
return;
cout<<p->data<<" ";
ShowPreorder(p->sol);
ShowPreorder(p->sag);
}
template<class tip>
void agac<tip>::OrtaGoster()
{
ShowPreorder(root);
}
template<class tip>
void agac<tip>::ShowInorder(dugum<tip> *p) //sol orta sağ
{
if(p==0)
return;
ShowInorder(p->sol);
cout<<p->data<<" ";
ShowInorder(p->sag);
}
template <class tip>
dugum<tip> * agac<tip>::uygunyer(tip x,dugum<tip> *ara)
{ if(ara==0)
return ara;
if(ara->data>x&&ara->sag==0||ara->data<x&&ara->sol==0)
return ara;
if(ara->data>x)
uygunyer(x,ara->sag);
if(ara->data<x)
uygunyer(x,ara->sol);
}
template <class tip>
agac<tip>::agac()
{
root=0;
}
template <class tip>
void agac<tip>::ekle(tip x)
{
dugum<tip> *ptr=uygunyer(x,root);
if(ptr==0) //ağaç yeni ise
{
dugum<tip> *temp=new dugum<tip>;
temp->data=x;
root=temp;
return;
}
if(ptr->data>x)
ptr->sagaekle(x);
else
ptr->solaekle(x);
}