您的位置:首页 > 其它

My Data Sructure Templates&Class

2013-03-29 09:17 281 查看
二叉树模板   完成于:2012,11,11 光棍节

首先说明,这个模板的通用性比较差。
目前测试阶段仅仅适用于char类型的数据。
限制就在于出入的无效判断上。
我先去看会书,然后再回来好好解决一下。

#include <iostream>
using namespace std;
template<class T>
class BTree
{
private:
class Node
{
public:
T element;//Can be optimized
Node *LTree;
Node *RTree;
};
Node *root;
bool creat(Node *&root);
void show(Node *root)const;//Read Only
bool B_Delete(Node *&root);
public:
BTree();
~BTree();
void BTShow();
bool BTCreat();
void BTDelete();
};
template<class T>
BTree<T>::BTree()
{
root=NULL;
}
template<class T>
BTree<T>::~BTree()
{
B_Delete(root);
}
template<class T>
bool BTree<T>::BTCreat()
{
if(creat(root));
return true;
return false;
}
template<class T>
bool BTree<T>::creat(Node *&root)
{
T tempaval;
cin>>tempaval;
if(tempaval!='#')
{
root=new Node;
root->element=tempaval;
if(!creat(root->LTree))
root->LTree=NULL;
if(!creat(root->RTree))
root->RTree=NULL;
}
else
return false;
return true;
}
template<class T>
bool BTree<T>::B_Delete(Node *&root)
{
if(root)
{
B_Delete(root->LTree);
B_Delete(root->RTree);
cout<<root->element<<ends;
delete root;
return true;
}
else
return false;
}
template<class T>
void BTree<T>::show(Node *root)const
{
if(root)
{
cout<<root->element<<ends;
show(root->LTree);
show(root->RTree);
}
else
return;
}
template<class T>
void BTree<T>::BTShow()
{
show(root);
}
int main()
{
BTree<char> temp1;
temp1.BTCreat();
temp1.BTShow();
cout << "Hello world!" << endl;
return 0;
}


简单的栈模板  完成于:2012,11,13

这个栈模板呢,我也没太多想说的了。
这个栈的是否是空判断有两个地方,一个是deep变量,另外一个就是当前top的front指针的值是不是空的。
别的,就只剩下了一个top变量了……
#include <iostream>
using namespace std;
template<class T>
class Stack
{
private:
class Node
{
public:
T member;
Node *front;
Node *next;
};
Node *bottom;
Node *top;
int deep;
public:
Stack();
~Stack();
bool Push(T &valT);
bool Pop(T &valT);
bool Isempty();
void show();
T &GetTop();
};
template<class T>
Stack<T>::Stack()
{
bottom=NULL;
top=NULL;
deep=0;
}
template<class T>
Stack<T>::~Stack()
{
T temp;
while(Pop(temp));
}
template<class T>
bool Stack<T>::Push(T &valT)
{
Node *New;
New=new Node;
if(!New)
return false;
deep++;
New->member=valT;
New->next=NULL;
if(top==NULL)
{
top=New;
top->front=NULL;
}
else
{
New->front=top;
top->next=New;
top=New;
}
return true;
}
template<class T>
bool Stack<T>::Pop(T &valT)
{
Node *valtemp;
if(Isempty())
return false;
deep--;
valT=top->member;
valtemp=top->front;
delete top;
if(valtemp==NULL)
top=NULL;
else
{
valtemp->next=NULL;
top=valtemp;
}
return true;
}
template<class T>
bool Stack<T>::Isempty()
{
if(top==NULL)
return true;
else
return false;
}
template<class T>
T &Stack<T>::GetTop()
{
if(!Isempty())
return top->member;
}
template<class T>
void Stack<T>::show()
{
Node *current=top;
if(top==NULL)
return;
while(current!=NULL)
{
cout<<current->member;
current=current->front;
}
cout<<"\nDeep:"<<deep<<endl;
}
int main()
{
cout << "Hello world!" << endl;
int temp;
Stack<int> temp1;
for(int i=0;i<5;i++)
temp1.Push(i);
temp1.show();
for(int i=0;i<5;i++)
{
cout<<temp1.GetTop();
temp1.Pop(temp);
}
return 0;
}


队列模板   完成于2012,11,10

这个是随手写的,刚才测试了一下,没发现什么问题,就顺手贴上来啦。
如果想添加一个自己的类,一定要设置好输入和输出的操作符重载啊。
#include <iostream>
using namespace std;
template<class T>
class Queue
{
private:
T *queuehead;
int maxsize;
int front;
int rear;
public:
Queue(int max=10);
~Queue();
bool Isempty();
bool Isfull();
bool addin(T & add);
bool pop(T & out);
void show();
};
template<class T>
Queue<T>::Queue(int max)
{
if(max>0)
maxsize=max;
else
maxsize=10;
queuehead=new T [max];
if(queuehead==NULL)
abort();
front=0;
rear=0;
}
template<class T>
Queue<T>::~Queue()
{
delete [] queuehead;
}
template<class T>
bool Queue<T>::Isempty()
{
if(front==rear)
return true;
else
return false;
}
template<class T>
bool Queue<T>::Isfull()
{
if((rear+1)%maxsize==front)
return true;
else
return false;
}
template<class T>
bool Queue<T>::addin(T & add)
{
if(!Isfull())
{
queuehead[rear%=maxsize]=add;
rear++;
return true;
}
else
return false;
}
template<class T>
bool Queue<T>::pop(T & out)
{
if(!Isempty())
{
out=queuehead[front%=maxsize];
front++;
return true;
}
else
return false;
}
template<class T>
void Queue<T>::show()
{
cout<<"Data in line:\n";
for(int i=0;i<maxsize;i++)
cout<<queuehead[i]<<ends;
cout<<endl;
cout<<"rear:"<<rear<<endl;
cout<<"front:"<<front<<endl;
}
int main()
{
cout << "Hello world!" << endl;
int a;
Queue<int> temp1;
cin>>a;
while(!cin.fail())
{
temp1.addin(a);
cin>>a;
}
while(!temp1.Isempty())
{
temp1.pop(a);
cout<<a<<ends;
}
cout<<endl;
temp1.show();
return 0;
}


简单的选择排序类  完成于2012,11,12

这个代码,我也实在是没什么想说的了……
#include <iostream>
#include <cstdlib>
using namespace std;
class SelectSort
{
private:
int *Array;
int maxsize;
int preposition;
int maxval;
int maxposition;
void initial();
public:
SelectSort(int *array=NULL,int size=0);
~SelectSort();
bool Sort(int *array,int size);
void show();
};
SelectSort::SelectSort(int *array,int size)
{
if(Array==NULL||size<=0)
{
Array=NULL;
maxsize=0;
}
else
Sort(array,size);
}
SelectSort::~SelectSort()
{
;
}
void SelectSort::initial()
{
preposition=0;
maxposition=0;
}
bool SelectSort::Sort(int *array,int size)
{
initial();
Array=array;
if(!Array||size==0)
return false;
maxsize=size;
for(int i=0;i<maxsize;i++)
{
maxval=Array[i];
for(int j=i+1;j<maxsize;j++)
{
if(maxval<Array[j])
{
maxval=Array[j];
maxposition=j;
}
}
//Do Exchange
Array[maxposition]=Array[i];
Array[i]=maxval;
//Used to initial the wait to compare position
maxposition=i+1;
}
return true;
}
void SelectSort::show()
{
cout<<"\nArray Information\n";
if(Array)
for(int i=0;i<maxsize;i++)
{
cout<<Array[i]<<ends;
}
else
cout<<"The Array Is Invaled\n";
cout<<"\nInside Variable:\n"\
<<"Array Address:"<<Array<<endl
<<"Maxsize:"<<maxsize<<endl
<<"Maxval:"<<maxval<<endl
<<"Maxposition"<<maxposition<<endl;
}
int main()
{
cout << "Hello world!" << endl;
int a[]={4,5,2,6,8,1,2,5};
cout<<endl;
SelectSort temp2;//(a,sizeof(a)/sizeof(int));
temp2.Sort(a,sizeof(a)/sizeof(int));
temp2.show();
SelectSort temp1(a,sizeof(a)/sizeof(int));
temp1.show();
return 0;
}


插入排序类(非模板)  完成于2012,11,13

今天早上计算方法,老师又不点名,一开心就睡到了8点半,然后洗个头,随便吃个饭,就开机写代码了~~~
期间还水了一会睿思,看了会dota视频,有点汗颜啊。
不过最后算法还是实现了,10点8分左右。
这个算法需要注意的就是要对进行交换的值预先存储,初期我就会忘了这点,导致调了半天……
代码:
#include <iostream>

using namespace std;
class InsertSort
{
private:
int *Array;
int maxsize;
public:
InsertSort(int *a=NULL,int size=0);
void movf(int position,int last);
bool Sort(int *a,int size);
};
int main()
{
cout << "Hello world!" << endl;
int a[5]={7,3,9,5,6};
int b=5;
InsertSort temp1(a,b);
temp1.Sort(a,b);
for(int i=0;i<5;i++)
cout<<a[i]<<ends;
return 0;
}
InsertSort::InsertSort(int *a,int size)
{
Array=a;
maxsize=size;
}
void InsertSort::movf(int position,int last)
{
for(int i=last;i>position;i--)
Array[i]=Array[i-1];
}
bool InsertSort::Sort(int *a,int size)
{
int valtemp;
if(a!=NULL||size>0)
{
Array=a;
maxsize=size;
}
else
return false;
for(int i=1;i<maxsize;i++)
{
for(int j=0;j<i;j++)//前部扫描
{
if(Array[i]>Array[j])
continue;
else
{
valtemp=Array[i];
movf(j,i);
Array[j]=valtemp;
}
}
}

}


线性队列类  完成时间:2012,10,16

首先,这个代码存在缺陷。
最后一位无法完全读取,每次满队列都会有一个空余元素,但是并不影响使用。
codeblocks编译通过且运行正常。
以前写的队列是用链表做的,这个用的数组,原因就是软基老师今天好像讲这个了,晚上就写了一下。
最用用C++比较顺手,就直接贴出来了,上次我在空间贴C++代码已经是好久以前的事了~~~
刚才看了好长时间的Jin书,搞得现在才开始贴代码,唉……
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class LineQueue
{
private:
int *queue;
int front;
int rear;
int maxsize;
void Linitial(){front=rear=0;}
void ShowSize(){cout<<"The size of the Queue is"<<maxsize<<endl;}
public:
LineQueue(int ms)
{
maxsize=ms;
queue=new int [maxsize];
Linitial();
}
LineQueue()
{
maxsize=10;
queue=new int [maxsize];
Linitial();

}
virtual ~LineQueue()
{
delete [] queue;
}
bool InsertMem(const int & num);
bool GetMem(int & num);
bool Isempty();
bool Isfull();
void show();
};
//inplementation
bool LineQueue::Isempty()
{
if(front==rear)
return true;
else
return false;
}
bool LineQueue::Isfull()
{
if(front%maxsize==(rear+1)%maxsize)
return true;
else
return false;
}
bool LineQueue::InsertMem(const int & num)
{
if(Isfull())
return false;
queue[(rear++)%maxsize]=num;
return true;
}
bool LineQueue::GetMem(int &num)
{
if(Isempty())
return false;
num=queue[front%maxsize];
queue[front++%maxsize]=0;
return true;
}
void LineQueue::show()
{
cout<<"The present information of the Queue\n";
cout<<"front is "<<front<<";  "<<"rear is "<<rear<<endl;
for(int i=0;i<maxsize;i++)
{
cout<<queue[i]<<ends;
if((i+1)%5==0)
cout<<endl;
}
cout<<endl;
}
int main()
{
srand(time(NULL));
cout << "Hello world!" << endl;
LineQueue Q1;
int temp;
while(!Q1.Isfull())
{
Q1.InsertMem(rand()%10);
}
Q1.show();
while(!Q1.Isempty())
{
Q1.GetMem(temp);
}
Q1.show();
while(!Q1.Isfull())
{
Q1.InsertMem(rand()%10);
}
//cout<<temp<<endl;
Q1.show();
return 0;
}
就是随手写的,如果觉得有错误,请指正。


一个链表类  完成于2012,10,21

指针值得可能有点乱,还有不少定义了没用变量,有兴趣的就自己贴着调试一下吧
另外注释有点太简单了,不过内容基本差不多,没什么技术含量。
代码在code::blocks调试运行通过
/********************START*********************/
#include <iostream>
#include <cstdlib>
#define badswap(a,b) {(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}
#define swap(a,b) if(&a==&b);else{(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}

using namespace std;

typedef struct Item
{
int num;
Item *next;
//wait to be added in new elements
}item;
class List
{
private:
item *head,*last,*pointer;
int maxsize;
int presize;
int prelocation;//present location
//
public:
List(int max=10);
List(const List &);
bool CreatList();
bool insert(const item &it,int position);//two arguments is needed
item* search(/*???*/)const;//finished
bool sort();//no implementation
void show()const;
~List();
//friend istream &operator>>(istream & is,List )
//It seems to be a little difficult to do this,so canceled temporary
};
List::List(int max)
{
head=last=pointer=NULL;
maxsize=max;
presize=0;
prelocation=0;
}
List::List(const List &Another_L)//There is some problem in it~~~
{
cout<<"working~"<<endl;
if(Another_L.head==NULL)
abort();
item *current,*pre,*Ahead=Another_L.head;
head=NULL;
while(Ahead!=NULL)
{
if(head==NULL)
{
head=new item;
if(head==NULL)
abort();
head->num=Ahead->num;
pre=head;
Ahead=Ahead->next;
presize++;
continue;
}
current=new item;
if(current==NULL)
abort();
//checked that it's OK
current->next=NULL;//next initial
pre->next=current;//lingking~~~
pre=current;//save present pointer
//I think I am right to do so when there is no pointer in Item
*current=*Ahead;
//~~~~~~~~~~~~~~
Ahead=Ahead->next;
//finished
}
cout<<"Copy constructor is finished\n";
}
bool List::CreatList()
{
item *current,*pre;
cout<<"This List support int Only now~~\n";
head=new item;
if(head==NULL)
return false;
pre=head;
pre->next=NULL;
cin>>pre->num;
presize++;
//
while(!cin.fail()&&presize<maxsize)
{
current=new item;
if(current==NULL)
return false;
pre->next=current;
pre=current;
current->next=NULL;
presize++;
cin>>current->num;
}
if(presize==maxsize)
current->next=NULL;
else
{
current->num=-1;
current->next=NULL;
}
return true;
}
bool List::insert(const item &it,int position)//insert function
{
item *temp=head,*pre;
if(position>presize)
{
cout<<"there is no position to add"<<endl;
return false;
}
for(int i=1;i<position;i++)
{
pre=temp;
temp=temp->next;
}
pre->next=new item;
if(pre->next==NULL)
{
pre->next=temp;
return false;
}
*(pre->next)=it;
pre->next->next=temp;
return true;
}
void List::show()const
{
item *temp;
temp=head;
cout<<"Present size of the List: "<<presize<<endl;
while(temp!=NULL)
{
cout<<temp->num<<ends;
temp=temp->next;
}
cout<<endl;
}
bool List::sort()
{
item *pointer=head;//initial
item *swappointer,*waittoswap;
if(pointer==NULL)
return false;
if(presize==1)
{
cout<<"there is olny one valid value in the list\n";
return true;
}
//temp data
int max;//used to sign the max number in the list
//save the present infomation
waittoswap=pointer;
max=pointer->num;
swappointer=pointer;
//used for swap
//the uper part has no problem;
//this part can find the maxnumber in the list
while(waittoswap->next!=NULL)
{
max=pointer->num;
while(pointer->next!=NULL)
{
if(max<pointer->next->num)
{
max=pointer->next->num;
swappointer=pointer->next;
}
pointer=pointer->next;//tempswap and pointer must be the same position at the beginning
}
//show();
cout<<max<<endl;
swap(waittoswap->num,swappointer->num);
waittoswap=waittoswap->next;
//this is used to initial the present pointer
//it's very important
pointer=waittoswap;
swappointer=waittoswap;
}
return true;
//
}
List::~List()
{
item *temp;
temp=head;
while(temp!=NULL)
{
//cout<<temp->num<<ends;
head=temp->next;
delete temp;
temp=head;
}
//abort();
cout<<"\ndestructor is finished\n";
}
int main()
{
cout << "Hello world!" << endl;
List temp;
item asd={99,NULL};
temp.CreatList();
temp.show();
temp.insert(asd,10);
temp.show();
return 0;
}
今天下午写的~见错莫怪请指正~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐