锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
//鏁版嵁緇撴瀯闃熷垪綆鍗曞疄鐜幫紙寰幆闃熷垪錛?/span>
2
#ifndef QUEUE_H
3
#define QUEUE_H
4
5
template<class T>
6
class Queue
7

{
8
public:
9
Queue(int maxsize=50);
10
virtual ~Queue();
11
bool IsFull()
{return (rear+1)%MaxSize==front?true:false;}//婊★紵
12
bool IsEmpty()
{return front==rear?true:false;};//絀猴紵
13
Queue<T>& Push(const T& val);//鍚戦槦灝炬彃鍏ュ厓绱?/span>
14
Queue<T>& Pop(T& e);//浠庡爢澶村垹闄ゅ厓绱?/span>
15
int Size()
{return _Size;};//榪斿洖闃熷垪鍏冪礌涓暟
16
T& Front();//榪斿洖瀵瑰ご鍏冪礌
17
T& Back();//榪斿洖闃熷熬鍏冪礌
18
19
private:
20
T *data;
21
int front,rear;
22
int MaxSize;
23
int _Size;
24
};
25
//------------------------------------------------
26
template<class T>
27
Queue<T>::Queue(int maxsize)
28

{
29
data=new T[maxsize+1];
30
MaxSize=maxsize+1;
31
front=rear=_Size=0;
32
33
}
34
//------------------------------------------------
35
template<class T>
36
Queue<T>::~Queue()
37

{
38
delete[] data;
39
}
40
//------------------------------------------------
41
template<class T>
42
Queue<T>& Queue<T>::Push(const T& val)
43

{
44
if(IsFull()) throw exception("闃熷垪宸叉弧");
45
rear=(rear+1)%MaxSize;
46
data[rear]=val;
47
_Size++;
48
return *this;
49
}
50
//------------------------------------------------
51
template<class T>
52
Queue<T>& Queue<T>::Pop(T& e)
53

{
54
if(IsEmpty()) throw exception("闃熷垪宸茬┖");
55
front=(front+1)%MaxSize;
56
e=data[front];
57
_Size--;
58
return *this;
59
}
60
//------------------------------------------------
61
template<class T>
62
T& Queue<T>::Front()
63

{
64
if(IsEmpty()) throw exception("闃熷垪宸茬┖");
65
return data[(front+1)%MaxSize];
66
67
}
68
//------------------------------------------------
69
template<class T>
70
T& Queue<T>::Back()
71

{
72
if(IsEmpty()) throw exception("闃熷垪宸茬┖");
73
return data[rear];
74
75
}
76
#endif
]]>
//鏁版嵁緇撴瀯鏍堝疄鐜幫紙鍩轟簬閾捐〃錛?/span>
2
#ifndef STACKLIST_H
3
#define STACKLIST_H
4
//鑺傜偣緇撴瀯
5
template<class T>
6
class Node
7

{
8
public:
9
Node(const T & val)
{data=val;next=NULL;}
10
T data;
11
Node<T> *next;
12
};
13
14
//鏍堝疄鐜?/span>
15
template<class T>
16
class Stack
17

{
18
public:
19
Stack();
20
virtual ~Stack();
21
bool IsEmpty();
22
Stack<T>& Push(const T &val);
23
Stack<T>& Pop(T &e);
24
T& GetTop() const
{return Top->data;};
25
int Size() const
{return _Size;};
26
27
private:
28
int _Size;
29
Node<T> *Top;
30
31
};
32
//-----------------------------------------------------------
33
template<class T>
34
Stack<T>::Stack()
35

{
36
Top=NULL;
37
_Size=0;
38
}
39
40
//-----------------------------------------------------------
41
template<class T>
42
bool Stack<T>::IsEmpty()
43

{ return !Top?true:false;
44
45
}
46
//-----------------------------------------------------------
47
template<class T>
48
Stack<T>::~Stack()
49

{
50
Node<T>* move=NULL;
51
while(Top)
52
{
53
move=Top->next;
54
delete Top;
55
Top=move;
56
}
57
58
}
59
//-----------------------------------------------------------
60
template<class T>
61
Stack<T>& Stack<T>::Push(const T &val)
62

{
63
64
Node<T> *NewNode = new Node<T>(val);
65
NewNode->next=Top;
66
Top=NewNode;
67
++_Size;
68
return *this;
69
}
70
//-----------------------------------------------------------
71
template<class T>
72
Stack<T>& Stack<T>::Pop(T &e)
73

{
74
if(Top==NULL) throw exception("鏍堜笅婧?/span>");
75
e=Top->data;
76
Node<T> *p=Top;
77
Top=Top->next;
78
delete p;
79
--_Size;
80
return *this;
81
}
82
83
84
#endif
]]>
//鏁版嵁緇撴瀯鍫嗘爤瀹炵幇錛堝熀浜庢暟緇勶級
2
#ifndef STACK_H
3
#define STACK_H
4
template<class T>
5
class Stack
6

{
7
public:
8
Stack(int maxsize=50);
9
virtual ~Stack();
10
bool IsEmpty();
11
Stack<T>& Push(const T &val);
12
Stack<T>& Pop(T &e);
13
T& GetTop() const
{return Data[Top];};
14
int Size() const
{return Top+1;};
15
16
private:
17
T *Data;
18
int MaxSize;
19
int Top;
20
21
};
22
23
24
//-----------------------------------------------------------
25
template<class T>
26
Stack<T>::Stack(int maxsize)
27

{
28
MaxSize=maxsize;
29
Data=new T[MaxSize];
30
Top=-1;
31
}
32
33
//-----------------------------------------------------------
34
template<class T>
35
bool Stack<T>::IsEmpty()
36

{ return Top==-1?true:false;
37
38
}
39
//-----------------------------------------------------------
40
template<class T>
41
Stack<T>::~Stack()
42

{
43
delete[] Data;
44
}
45
//-----------------------------------------------------------
46
template<class T>
47
Stack<T>& Stack<T>::Push(const T &val)
48

{
49
if(Top==MaxSize-1) throw exception("鏍堜笂婧?/span>");
50
Data[++Top]=val;
51
return *this;
52
}
53
//-----------------------------------------------------------
54
template<class T>
55
Stack<T>& Stack<T>::Pop(T &e)
56

{
57
if(Top==-1) throw exception("鏍堜笅婧?/span>");
58
e=Data[Top--];
59
return *this;
60
}
61
#endif
]]>
]]>
#include "BinTree.h"
template<class K,class E>
class BinSearchTree:public BinTree<E>

{
public:
bool Search(const K &k,E &e);//浠ュ叧閿瓧K榪涜鎼滅儲錛岀粨鏋滆繑鍥炲埌e涓?/span>
BinSearchTree<K,E>& Insert(const E &e);//灝嗗厓绱爀鎻掑叆鍒版爲涓?/span>
BinSearchTree<K,E>& Delete(const K &k,E &e);//閫氳繃鎸囧畾鍏抽敭瀛梜榪涜鑺傜偣鐨勫垹闄?/span>
};
//----------------------------------------------------------
template<class K,class E>
bool BinSearchTree<K,E>::Search(const K &k,E &e)

{
BinTreeNode<E> *p=root;
while(p)

{
if(k<p->data)
p=p->LeftChild;
else if(k>p->data)
p=p->RightChild;
else//鎵懼埌鍏冪礌

{
e=p->data;
return true;
}
}
return false;
}
//----------------------------------------------------------
template<class K,class E>
BinSearchTree<K,E>& BinSearchTree<K,E>::Insert(const E &e)

{
BinTreeNode<E> *p=root,*pp=NULL;
while(p)

{
pp=p;
if(e<=p->data)
p=p->LeftChild;
else
p=p->RightChild;
}
BinTreeNode<E> *NewNode=new BinTreeNode<E>(e);
if(root)

{
if(e<=pp->data)
pp->LeftChild=NewNode;
else
pp->RightChild=NewNode;
}
else
root=NewNode;
return *this;
}
//----------------------------------------------------------
template<class K,class E>
BinSearchTree<K,E>& BinSearchTree<K,E>::Delete(const K &k,E &e)

{
BinTreeNode<E> *p=root,*pp=NULL;
while(p&&p->data!=k)

{pp=p;
if(k<p->data) p=p->LeftChild;
else p=p->RightChild;
}

if(!p)
{throw exception("娌℃湁鎵懼埌鎸囧畾鍏冪礌");}
e=p->data;
if(p->LeftChild&&p->RightChild)//p鐨勫乏鍙沖瀛愬潎涓嶄負絀?/span>

{
BinTreeNode<E> *s=p->LeftChild,*ps=p;
while(s->RightChild)

{
ps=s;
s=s->RightChild;
}
p->data=s->data;
p=s;
pp=ps;
}//if
BinTreeNode<E> *c;
if(p->LeftChild) c=p->LeftChild;
else c=p->RightChild;
if(p==root) root=c;
else

{
if(p==pp->LeftChild) pp->LeftChild=c;
else pp->RightChild=c;
}
delete p;
return *this;
}