9.19--9.27

template<class T> //栈的顺序实现
class SeqStack:public Stack<T>
{
private:
int top;
int maxTop;
T *s;
};










template<class T> //顺序表类
SeqStack<T>::SeqStack(int mSize)
{
maxTop=mSize-1;
    s = new T[mSize];
top = -1;
}
#include "linearlist.h"
template <class T>
class SeqList:public LinearList<T>
{
public:
SeqList(int msize);
~ SeqList() { delete [] elments;}
bool IsEmpty() const;
int Length() const;
bool Find(int i,T& x) const;
int Searh(T x) const;
bool Insert(int i,T x);
bool Delete(int i);
bool Update(int x,T x);
void Output(ostream& out)const;
private:
int maxLength;
T *elements;
};
template<class T>
SeqList<T>::SeqList(int mSize)
{
maxLength = mSize;
elements = new T[maxLength];
n = 0;
}
template<class T>
bool SeqList<T>::IsEmpty() const
{
return n == 0;
}
template<class T>
int SeqList<T>::Length() const
{
return n;
}
template<class T>
bool SeqList<T>::Find(int i,T& x)const
{
if(i<0 || i>n-1)
{
cout<<"Out of Bounds"<<endl;
return false:
}
x=elements[i];
return true;
}
template<class T>
int SeqList<T>::Search(T x) const
{
for(int j=0;j<n;j++)
if(elements[j]= =x) return j;
return -1;
}
template<class T>
bool SeqList<T>::Insert(int i,T x)
{
if(i<-1 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
if( n == maxLength)
cout<<"OverFlow"<<endl; return false;
for(int j=n-1;j>i;j--)
{
elements[j+1]=elements[j];
}
elements[i+1]=x;
n++;
return true;
}
template<class T>
bool SeqList<T>::Delete(int i)
{
if(!n)
cout<<"UnderFlow"<<endl; return false;
if(i<0 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
for(int j=i+1;j<n;j++)
elements[j-1]=elements[j];
n--;
return true;
}
template<class T>
bool SeqList<T>::Update(int i,T x)
{
if(i<0 ||i>n-1)
cout<<"Out of Bounds"<<endl; return false;
elements[i]=x;
return true;
}
template<class T>
void SeqList<T>::Output(ostream& out)const
{
for(int i=0;i<n;i++)
out<<elements[i]<<"  ";
out<<endl;
}












#include "linearlist.h" //结点类和单链表类
template <class T> class SingleList;
template<class T>
class Node
{
private:
T elements;
Node<T> *link;
friend class SingleList<T>;
};
template<class T>
class SingleList:public LinearList<T>
{
public:
SingleList(){first = NULL; n=0;}
~SingleList();
bool IsEmpty() const;
int Length() const;
bool Find(int i, T& x) const;
int Search(T x) const;
bool Insert(int i,T x);
bool Delete(int i);
bool Update(int i,T x);
void Clear();
void Output(ostream& out) const;
private:
Node<T>* first;
};
template<class T>
SingleList<T>::~SingleList()
{
Node<T> *p;
while(first)
{
p = first->link;
delete first;
first = p;
}
}
template<class T>
bool SeqList<T>::Insert(int i,T x)
{
if(i<0 ||i>n-1)
cout<<"Out of Bounds"<<endl; return false;
Node<T> * q=new Node<T>;
q->element=x;
Node<T> *p=first;
for(int j=0;j<i;j++)
p=p->link;
if(i>-1)
{
q->link=p->link;
p->link=q;
}
else{
q->link=first;
first=q;
}
n++;
return true;
}
template<class T>
bool SeqList<T>::Delete(int i)
{
if(!n)
cout<<"UnderFlow"<<endl; return false;
if(i<0 ||i>n-1)
cout<<"Out of Bounds"<<endl; return false;
Node<T> *p=first, *q=first;
for(int j=0;j<i-1;j++)
q=q->link;
if(i==0)
first=first->link;
else{
p=q->link;
q->link=p->link;
}
delete p;
n--;
return true;
}














void Polynominal::PolyAdd(Polynominal& r) //多项式相加
{
Term* q,*q1=theList, *p;
p=r.theList->link;
q=q1->link;
while(p->exp>=0)
{
while(p->exp<q->exp)
{
q1=q;
q=q->link;
}
if(p->exp == q->exp)
{
q->coef+=p->coef;
if(q->coef==0)
{
q1->link=q->link;
delete(q);
q=q1->link;
}
else{
q1=q;q=q->link;
}
}
else
{
q1=q1->InsertAfter(p->coef,p->exp);
p=p->link;
}
}
}














#include" stack.h" //顺序栈类
template<class T>
class SeqStack:public Stack<T>
{
public:
SeqStack(int mSize);
~SeqStack() { delete []s;}
bool Top(T &x)const;
bool Push(T x);
bool Pop();
void Clear() {top=-1};
private:
int top;
int maxTop;
T *s;
};
template<class T>
SeqStack<T>::SeqStack(int mSize)
{
maxTop=mSize-1;
s=new T[mSize];
top=-1;
}
template<class T>
bool SeqStack<T>::Top(T &x) const
{
if(IsEmpty())
{
cout<<"Empty"<<endl;
return false;
}
x=s[top];
return true;
}
template<class T>
bool SeqStack<T>::Push(T x)
{
if(IsFull())
cout<<"Overflow"<<endl; return false;
s[++top]=x;
return true;
}
template<class T>
bool SeqStack<T>::Pop()
{
if(IsEmpty())
cout<<"Underflow"<<endl; return false;
top--;
return true;
}














#include"queue.h" //循环队列类
template <class T>
class SeqQueue:public Queue<T>
{
public:
SeqQueue(int mSize);
~SeqQueue() { delete[]q;}
bool IsEmpty() const { return front==rear;}
bool IsFull()  const { return (rear+1)%maxSize==front;}
bool Front(T& x) const;
bool EnQueue(T x);
bool DeQueue();
Void Clear() {front=rear=0;}
private:
int front,rear;
int maxSize;
T *q;
};
template<class T>
SeqQueue<T>::SeqQueue(int mSize)
{
maxSize=mSize;
q=new T[maxSize];
front=rear=0;
}
template<class T>
bool SeqQueue<T>::Front(T& x) const
{
if(IsEmpty())
cout<<"empty"<<endl; return false;
x= q[(front+1)%maxSize];
return true;
}
template<class T>
bool SeqQueue<T>::EnQueue(T x)
{
if(IsFull())
cout<<"Full"<<endl; return false;
q[(rear+1)%maxSize]=x;
return true;
}
template<class T>
bool SeqQueue<T>::DeQueue()
{
if(IsEmpty())
cout<<"Underflow"<<endl; return false;
front=(front+1)%maxSize;
return true;













void String::Fail() //失败函数
{
int j=0, k=-1,;f[0]=-1;
while(j<n)
{
if((k==-1) || (str[j] == str[k]))
{
j++;k++;
if(str[j]==str[k])
f[j]=f[k];
else f[j]=k;
}
else k=f[k];
}
}








template <class T> //向下调整
void AdjustDown( T heap[],int r, int j)
{
int child=2*r+1;
T temp=heap[r];
while(child<=j)
{
if((child<j) && (heap[child]>heap[child+1])) child++;
if((temp<=heap[child]) break;
heap[(child-1)/2]=heap[child];
child=child*2+1;
}
heap[(child-1)/2]=temp;
}
template <class T>
void CreatHeap(T heap[],int n)
{
for(int i=(n-2)/2;i>-1;i--)
AdjustDown(heap,i,n-1);
}








template <class T>
HfmTree<T> CreatHfmTree (T w[],int n)
{
PrioQueue<HfmTree<T>> pq(n);
HfmTree<T> x,y,z,zero;
for(int i=0;i<n;i++)
{
z.MakeTree(w[i],x,y);z.putW(W[i]);
pq.Append(z);
z.SetNull();
}
for (i=1;i<n;i++)
{
pq.Serve(x);pq.Serve(y);
z.MakeTree(x.getW()+y.getW(),x,y);
z.putW(x.getW()+y.getW());
pq.Append(z);
z.SetNull();
}
pq.Serve(z);
return z;
}








template<class T> //二叉搜索树的删除运算
ResultCode BSTree<T>::Remove(T& x)
{
BTNode<T> *c,*s,*r,*p=root,*q=NULL;
while(p && p->element!=x)
{
q=p;
if(x<p->element) p=p->lChild;
else p=p->rChild;
}
if(!p) return NotPresent;
if(p->lChild && p->rChild)
{
s=p->rChild;r=p;
while (s->lChlid)
{
r=s;
s=s->lChild;
}
p->element=s->element;
p=s;q=r;
}
if(p->lChild) c=p->lChid;else c=p->rChild;
if(p==root) root=c;
else if (p==q->lChild) q->lChild=c;
else q->rChild=c;
delete p;
return Success;
}








r=s->lChild; //二叉平衡树的LL旋转
if(r->bF==1)
{
s->lChild=r->rChild;
r->rChild=s;
s->bF=0;s=r;
}












template<class T> //二叉平衡树左旋转函数
void AVLTree<T>::LRotation(AVLNode<T>* &s bool &unBalanced)
{
AVLNode<T>*u,*r=s->lChild;
if (r->bF==1)
{
s->lChild=r->rChild; r->rChild=s;
s->bF=0;s=r;
}
else
{
u=r->rChild;r->rChild=u->lChild;
u->lChild=r;s->lChild=u->rChild;
u->rChild=s;
switch(u->bF)
{
case 1:s->bF=-1;r->bF=0;break;
case 0:s->bF=r->bF=0;break;
case -1:s->bF=0;r->bF=1;
}
s=u;
}
s->bF=0;
unBalanced=false;
}
















template <class T> //跳表的搜索
ResultCode SkipList<T>::Search(T& x)const
{
if(x>=tail->element) return RangeError;
SNode<T>*p=head;
for(int i=levels;i>=0;i--)
while(p->link[i]->element<x) p=p->link[i];
if(x==p->link[0]->element) return Success;
else return NotPresent;
}
















template <class T> //跳表的插入
int SkipList<T>::Level()
{
int lev=0;
while(rand()<= RAND_MAX/2) level++;
return (lev<=maxLevel)?lev:maxLevel;
}
template <class T>
SNode<T>* SkipList<T>::SaveSearch(const T& x)
{
SNode<T>* p=head;
for(int i=levels;i>=0;i--)
{
while(p->link[i]->element<x) p=p->link[i];
last[i]=p;
}
return (p->link[i]);
}
template <class T>
ResultCode SkipList<T>::Insert(T& x)
{
if(x>=tail->element) return RangeError;
SNode<T>* p=SaveSearch(x);
if(p->element==x) return Duplicate;
int lev=Level();
if(lev>levels)
{
lev=++levels;last[lev]=head;
}
SNode<T>* y=new SNode<T>(lev+1);
y->element=x;
for(int i=0;i<=lev;i++)
{
y->link[i]=last[i]->link[i];
last[i]->link[i]=y;
}
return Success;
}






















template <class T> //跳表的删除
ResultCode SkipList<T>::Remove(T& x)
{
if(x>=tail->element) return RangeError;
SNode<T>*p=SaveSearch(x);
if(p->element!=x) return NotPresent;
for(int i=0;i<=levels && last[i]->link[i]==p;i++)
last[i]->link[i]=p->link[i];
while(levels>0 && head->link[levels]==tail) levels--;
return Success;
}












template <class T> //线性探查散列表的搜索
ResultCode HashTable<T>::Find(T& x,int pos)const
{
pos=h(x);
int i=pos; j=-1;
do{
if(ht[pos] = NeverUsed && j==-1) j=pos;
if(empty[pos]) break;
if(ht[pos]=x)
return Success;
pos=(pos+1)%M;
}while(pos!=i);
if(j==-1) return Overflow;
pos=j;return NotPresent;
}
template <class T>
ResultCode HashTable<T>:: Search(T& x)const
{
int pos;
if(Find(x,pos)==Success) return Success;
return NotPresent;
}


















template <class T> //DFS算法
void ExtLGrath<T>::DFS()
{
bool* visited=new bool[n];
for(int i=0;i<n;i++) visited[i]=false;
for(i=0;i<n;i++)
if(!visited[i]) DFS(i,visited);
delete[]visited;
}
template <class T>
boid ExtlGraph<T>::DFS(int v,bool* visited)
{
visited[v]=true;cout<<" "<<v;
for(ENode<T> *w;w;w=w->nextArc)
if(!visited[w->adjVex]) DFS(w->adjVex,visited);
}
















template<class T> //BFS算法
void ExtLGraph<T>::BFS(int v,bool* visited)
{
SeqQueue<int> q(QSize);
visited[v]=true;cout<<"  "<<v;
q.EnQueue(v);
while(! q.IsEmpty())
{
q,Front(v);q.DeQueue();
for(ENode<T> *w=a[v];w;w=w->nextArc)
if(!visited[w->adjVex]
{
visited[w->adjVex]=true;
cout<<"  "<<w->adjVex;
   q.EnQueue(w->adjVex);
}
}
}




















template <class T> //CalInDegree函数
void ExtLGraph<T>::CalInDegree(int *InDegree)
{
for(int i=0;i<n;i++)
InDegree[i]=0;
for(i=0;i<n;i++)
for(ENode<T>* p=a[i];p;p=p->nextArc)
InDegree[p->adjVex]++;
}














template<class T> //拓扑排序
void ExtLGraph<T>::TopoSort(int *order)
{
int *InDegree=new int[n[;
int i,j,k.top=-1;
CalInDegree(InDegree);
for(i=0;i<n;i++)
if(!InDegree[i])
InDegree[i]=top;top=i;
for(i=0;i<n;i++)
if(top==-1)
throw HasCycle;
else
{
j=top;top=InDegree[top];
order[i]=j;cout<<j<<"  ";
for(ENode<T>* p=a[j];p;p=p->nextArc)
{
k=p->adjVex; InDeGree[k]--;
if(!InDegree[k])
InDegree[k]=top;top=k;
}
}
}


















template<class T> //Prim算法
void ExtLGraph<T>::Prim(int k,int *nearest,T *lowcost)
{
bool *mark=new bool[n];
ENode<T> *p;
if(k<0||k>n-1) throw OutofBounds;
for(int i=0;i<n;i++)
{
nearest[i]=-1;mark[i]=false;
lowcost[i]=INFTY;
}
lowcost[k]=0;nearest[k]=k;mark[k]=true;
for(i=1;i<n;i++)
{
for(p=a[k];p;p=p->nextArc)
{
int j=p->adjVex;
if((!mark[j[)&&(lowcost[j]>p->w))
lowcost[j]=p->w;nearest[j]=k;
}
T min=INFTY;
for(int j=0;j<n;j++)
if((!mark[j[)&&(lowcost[j]<min))
min=lowcost[j];k=j;
mark[k]=true;
}
}
 
















template<class T> //Kruskal算法
void ExtLGraph<T>::Kruskal(PrioQueue<eNode<T>>& pq)
{
eNode<T> x;
UFSet s(n);
int u,v,k=0;
while(k<n-1 && !pq.IsEmpty())
{
pq.Serve(x);
u=s.Find(x.u);v=s.Find(x.v);
if(u!=v)
{
s,Union(u,v);k++;
cout<<"("<<x.u<<","<<x.v<<","<<x.w<<")  ";
}
}
if(k<n-2) throw NonConnected;
}
















template<class T> //Dijkstra算法
int ExtLGraph<T>::Choose(int *d,bool *s)
{
int i;minpos;T min;
min=INFTY; minpos=-1;
for(i=0;i<n;i++)
{
if(d[i]<=min &&!s[i])
min=d[i];minpos=i;
}
return minpos;
}
template<class T>
void ExtLGraph<T>::Dijkstra(int v,T *d,int *path)
{
int i,k,w;
if(v<0||v>n-1) throw OutOfBounds;
bool *s=new bool[n];
for(i=0;i<n;i++)
{
s[i]=flase;d[i]=a[v][i];
if(i!=v && d[i]<INFTY) path[i]=v;
else path[i]==-1;
}
s[v]=true; d[v]=0;
for(i=1;i<n;i++)
{
k=Choose(d,s);s[k]=true;
for(w=0;w<n;w++)
if(!s[w] && d[k]+a[k][w]<d[w])
d[w]=d[k]+a[k][w];path[w]=k;
}
}


















template<class T> //Floyd算法
void ExtLGraph<T>::Floyd(T **d,int **path)
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
d[i][j]=a[i][j];
if(i!=j && a[i][j]<INFTY) path[i][j]=i;
else path[i][j]=-1;
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(d[i][k]+d[k][j]<d[i][j])
{
d[i][j]=d[i][k]+d[k][j];
path[i][j]=path[k][j];
}
}












template<class T> //简单选择排序
void SelectSort(T A[], int n)
{
int small;
for(int i=0;i<n-1;i++)
{
small=i;
for(int j=i+1;j<n;j++)
if(A[j]<A[small]) small=j;
Swap(A[i],A[small]);
}
}










template<class T> //直接插入排序
void InsertSort(T A[],int n)
{
for(int i=1;i<n;i++)
{
int j=i;
T temp=A[i];
while(j>0 && temp<A[j-1])
{
A[j]=A[j=1];j--;
}
A[j]=temp;
}
}










template<class T> //冒泡排序
void BubbleSort(T A[],int n)
{
int i,j,last;
i=n-1;
while(i>0)
{
last=0;
for(j=0;j<i;j++)
if(A[j+1]<A[j])
{
Swap([A],A[j+1]);
last=j;
}
i=last;
}
}
















template<class T> //快速排序
void QuickSort(T A[],int n)
{
QSort(A,0,n-1);
}
template<class T>
void QSort(T A[],int left,int right)
{
int i,j;
if(left<right)
{
i=left; j=right+1;
do{
do i++;while(A[i]<A[left];
do j++;while(A[j]>A[left];
if(i<j) Swap(A[i],A[j]);
}while(i<j);
Swap(A[left],A[j]);
QSort(A,left,j-1);
QSort(A,j+1,right);
}
}












template<class T> //两路合并
void Merge(T A[],int i1,int j1,int i2,int j2)
{
T *Temp=new T[j2-i1+1];
int i=i1,j=i2,k=0;
while(i<=j1&&j<=j2)
{
if(A[i]<=A[j]) Temp[k++]=A[i++];
else Temp[k++]=A[j++];
}
while(i<=j1) Temp[k++]=A[i++];
while(j<=j2) Temp[k++]=A[j++];
for(i=0;i<k;i++) A[i1++]=Temp[i];
delete []Temp;
}








template<class T> //合并排序
void MergeSort(T A[],int n)
{
int i1,j1,i2,j2;
int size=1;
while(size<n)
{
i1=0;
while(i1+size<n)
{
i2=i1+size;
j1=i2-1;
if(i2+size-1>n-1)
j2=n-1;
else
j2=i2+size-1;
Merge(A,i1,j1,i2,j2);
i1=j2+1;
}
size*=2;
}
}










template<class T> //堆排序
void AdjustDown(T A[],int r,int j)
{
int child=2*r+1; T temp=A[r];
while(child<=j)
{
if((child<j)&&(A[child]<A[child+1])) child++;
if(temp>=A[child]) break;
A[(child-1)/2]=A[child];
child=2*child+1;
}
A[(child-1)/2]=temp;
}
template<class T>
void HeapSort(T A[],int n)
{
for(int i=(n-2)/2;i>-1;i--) AdjustDown(A,i,n-1);
for(i=n-1;i>0;i--)
{
Swap(A[0],A[i]);
AdjustDown(A,0,i-1);
}
}






template<class T> //基数排序
void RadixSort(Term <T> L[])
{
int tail;
int f[radix],r[radix]);
for(int i=0;i<d;i++)
{
for(int j=0;j<radix;j++) f[j]=0;
for(int k=L[0].next;k;k=L[k].next)
{
j=L[k].keys[i];
if(!f[j]) f[j]=k;
else L[r[j]].next=k;
r[j]=k;
}
for(j=0;!f[j];j++);
L[0].next=f[j];
tail=r[j];
while(j<radix-1)
{
for(j++;(j<radix-1)&&(!f[j]);j++);
if(f[j])
{
L[tail].next=f[j];
tail=r[j];
}
}
L[tail].next=0;
}
}




















//以上内容全部抄自书本,以下内容将为全部自己书写,纵然过程艰难,我将无所畏
















#include <iostream.h> //规范线性表类格式,作为顺序表类和链表类的基类
template<class T>
class LinearList
{
protected:
int n;
public:
virtual bool IsEmpty() const=0;
//virtual bool IsFull() const=0;
virtual int Length() const=0;
virtual bool Find(int i,T &x) const=0;
virtual int Search(T &x) const=0;
virtual bool Insert(int i,T &x) const=0;
//virtual bool Delete(T &x) const=0;
virtual bool Delete(int i);
virtual bool Update(int i,T &x) const=0;
//virtual void Output() const=0;
virtual void Output(ostream& out) const=0;
};


#include"LinearList.h" //线性表的顺序表示
template<class T>
class SeqList:public LinearList
{
private:
int maxLength; //maxLength表示最大长度,而n表示顺序表中元素个数
T *s;
public:
SeqList(int mSize);
//~SeqList(){delete []s;}
bool IsEmpty();
int Length();
bool IsFull();
bool Insert(int i,T &x);
bool Delete(int i);
bool Find(int i,T &x);
int  Search(T &x);
bool Update(int i,T &x);
void Output(ostream& out);
};
template<class T>
SeqList::SeqList(int mSize) //使用时:template<int> LA(SIZE);
{
maxLength=mSize;
s=new T[maxLength];
n=0;
}
template<class T>
bool SeqList::IsEmpty()
{
/*if(!n)
return true;
return false;*/
return n==0;
}
template<class T>
int SeqList::Length()
{
return n;
}
template<class T>
bool SeqList::IsFull()
{
return n==maxLength;
}
template<class T>
bool SeqList::Insert(int i,T &x)
{
if(IsFull())
cout<<"OverFlow"<<endl; return false;
if(i<-1 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
for(int j=n;j>i;j--)
s[j]=s[j-1];
s[i]=x;
n++;
return true;
}
template<class T>
bool SeqList::Delete(int i)
{
if(i<0 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
if(IsEmpty())
cout<<"UnderFlow"<<endl; return false;
for(int j=i;j<n-1;j++)
s[j]=s[j+1];
n--;
return true;
}
template<class T>
bool SeqList::Find(int i,T &x)
{
if(i<0 ||i>n-1)
cout<<"Out of Bounds"<<endl; return false;
x=s[i];
return true;
}
template<class T>
int SeqList::Search(T &x)
{
for(int i=0;i<n;i++)
if(x==s[i]) return i;
return -1;
}
template<class T>
bool SeqList::Update(int i,T &x)
{
if(i<0 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
s[i]=x;
return true;
}
template<class T>
void SeqList::Output(ostream& out) //使用时:LA.Output(cout); 
{
for(int i=0;i<n;i++)
out<<s[i]<<"  ";
out<<endl;
}














#include"SeqList.h"
template<class T>
void Union(SeqList<T> & LA,SeqList<T> & LB)
{
T x;
//for(int i=0;i<LB.n;i++)
for(int i=0;i<LB.Length();i++) //n为私有成员,外函数无法直接调用,但却可以调用公有函数
{
LB.Find(i,x);
if(!LA.Search(x))
LA.Insert(LA.Length()-1,x);
}
}






#include"LinearList.h" //结点类和单链表类
提前声明???
template<class T> class SingleList;
template<class T>
class Node
{
private:
T element;
Node<T> *link;
friend class SingleList;
};
template<class T>
class SingleList:public LinearList
{
private:
Node<T> * first;
public:
SingleList();
~SingleList();
bool Insert(int i,T x);
bool Delete(int i);
bool Find(int i,T& x) const;
int  Search(T x) const;
bool Update(int i,T x);
bool IsEmpty() const;
int Length() const;
void Output(ostream& out) const;
void Clear();
};
template<class T>
SingleList::SingleList()
{
first=NULL;
n=0;
}
template<class T>
SingleList::~SingleList()
{
for(int i=0;i<n;i++)
{
Node<T> * p=first;
first=first->link;
delete p;
}
}
template<class T>
int SingleList::Length() const
{
return n;
}
template<class T>
bool SingleList::IsEmpty() const
{
return n==0;
}
template<class T>
bool SingleList::Find(int i,T &x) const
{
if(i<0 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
Node<T>* p=first;
for(int j=0;j<i;j++)
p=p->link;
x=p->element;
return true;
}
template<class T>
int  SingleList::Search(T x) const
{
Node<T>* p=first;
for(int i=0;i<n;i++)
{
if(p->element==x)
return i;
p=p->link;
}
return -1;
/*
Node<T>* p=first;
for(int j=0;p&&p->element!=x;j++) p=p->link;
if(p) return j;
return -1;*/
}
template<class T>
bool SingleList::Insert(int i,T x)
{
Node<T>* p=first;
if(i<-1 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
for(int j=0;j<i;j++)
p=p->link;
Node<T>* s=new Node<T>; s->element=x;
if(i==-1)
{
s->link=first;
s=first;
}
else
{
s->link=p->link;
p->link=s;
}
n++;
return true;
}
template<class T>
bool SingleList::Delete(int i)
{
if(i<0 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
Node<T>* p=first, *q=first;
for(int j=0;j<i-1;j++)
p=p->link;
if(i==0)
first=first->link;
else
{
q=p->link;
p->link=q->link;
}
delete q;
n--;
return true;
}
template<class T>
bool SingleList::Update(int i,T x)
{
if(i<0 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
Node<T>* p=first;
for(int j=0;j<i;j++)
p=p->link;
p->element=x;
return true;
}
template<class T>
void SingleList::Output(ostream& out) const
{
while(first)
{
out<<first->element<<"  ";
first=first->link;
}
out<<endl;
}










#include"singlelist.h"
template<class T>
void Intersection(SingleList<T> &LA,SingleList<T> &LB)
{
T x;
for(int i=0;i<LA.Length();i++)
{
LA.Find(i,x);
if(!LB.search(x))
LA.Delete(x);
}
}






template<class T>
HeaderList<T>::HeaderList()
{
Node<T>* first=new Node<T>;
first->link=NULL;
n=0;
}
template<class T>
HeaderList<T>::~HeaderList()
{
Node<T>*p=first;
while(p)
{
p=p->link;
delete p;
}
}
template<class T>
bool HeaderList<T>::Insert(int i,T x)
{
Node<T>* p=first;
Node<T>*q=new Node<T>;
q->element=x;
if(i<-1 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
for(int j=0;j<i;j++)
p=p->link;
q->link=p->link;
p->link=q;
n++;
return true;
}
template<class T>
bool HeaderList<T>::Delete(int i)
{
if(!n)
cout<<"UnderFlow"<<endl; return false;
if(i<0 || i>n-1)
cout<<"Out of Bounds"<<endl; return false;
Node<T>* p=first; *q=first;
for(int j=0;j<i-1;j++)
p=p->link;
q=p->link;
p->link=q->link;
delete q;
n--;
return true;
}










template<class T> class DoubleList;
template<class T>
class DNode
{
private:
DNode<T>* rLink, * lLink;
T element;
friend class DoubleList<T>;
}


DNode<T>*q=new DNode<T>, q->element=x;
q->lLink=p->lLink;q->rLink=p;p->lLink->rLink=q;p->lLink=q;
p->lLink->rLink=p->rLink;
p->rLink->lLink=p->lLink;


class Polynominal;
class Term
{
public:
Term(int c,int e);
Term(int c,int e,Term* nxt);
Term* InsertAfter(int c,int e);
private:
int coef,exp;
Term* link;
friend class Polynominal;
friend ostream & operator <<(ostream& out,const Term &);
};
Term::Term(int c,int e)
{
coef=c;
exp=e;
link=0;
}
Term::Term(int c,int e,Term* nxt)
{
coef=c;
exp=e;
link=nxt;
}
Term* Term::InsertAfter(int c,int e)
{
link=new Term(c,e,link);
return link;
}
ostream & operator << (ostream& out,Term & val)
{
if(val.coef==0) return out;
out<<val.coef;
switch(val.exp){
case 0:break;
case 1:out<<"X"; break;
default: out<<"X^"<<val.exp; break;
}
return out;
}


class Polynominal
{
public:
Polynominal();
~Polynominal();
void Input(istream & in);
void Output(ostream & out);
private:
Term * theList;
friend istream & operator >>(istream & in,const Polynominal &);
friend ostream & operator <<(ostream & out,const Polynominal &);
friend Polynominal& operator +(Polynominal &, Polynominal &);
};
Polynominal::Polynominal()
{
theList=new Term(0,-1);
theList->link=theList;
}
Polynominal::~Polynominal()
{
Term *p=thelist->link;
while(p!=thelist)
{
thelist->link=p->link;
delete p;
p=thelist->link;
}
delete thelist;
}
void Polynominal::Input(istream & in)
{
Term *q=thelist;
int c,e;
for(;;)
{
cout<<"Input a term(coef,exp):"<<endl;
cin>>c>>e;
if(e<0) break;
q=q->InsertAfrer(c,e);
}
}
void Polynominal::Output(ostream & out)
{
int first=1; Term* p=thelist->link;
for(;p!=thelist;p=p->link)
{
if(!first && p->coef>0) out<<"+";
first=0;
out<<*p;
}
cout<<endl;
}
void Polynominal::PolyAdd(Polynominal &r)
{
Term* q,*q1=theList,*p;
p=r.theList->link;
q=q1->link;
while(p->exp>=0)
{
while(p->exp<q->exp)
{
q1=q;q=q->link;
}
if(p->exp==q->exp)
{
q->coef+=p->coef;
if(q->coef==0)
{
q1->link=q->link;delete q;
q=q1->link;
}
else
{
q1=q;q=q->link;
}
}
else
{
q1=q1->InsertAfter(p->coef,p->exp);
}
p=p->link;

}
}
ostream & operator << (ostream & out,const Polynominal &x)
{
x.Output(out);return out;
}
istream & operator >>(istream & in ,const Polynominal &x)
{
x.Input(in);return in;
}
Polynominal& operator +(Polynominal &a,Polynominal & b)
{
a.Polynominal(b);return a;
}
void main()
{
Polynominal p,q;
cin>>p; cout<<p;
cin>>q; cout<<q;
q=q+p; cout<<q;
}






#include<iostream> //堆栈类 规范堆栈格式
using namespace std;
template <class T>
class Stack
{
public:
virtual bool IsEmpty() const=0;
virtual bool IsFull() const=0;
virtual bool Top(T &x) const=0;
virtual bool Push(T x)=0;
virtual bool Pop()=0;
virtual bool Clear()=0;
};
 

#include"stack.h"
template<class T>
class SeqStack:public Stack
{
private:
int top;
int maxTop;
T *s;
public:
SeqStack(int mSize);
~SeqStack(){delete []s};
bool IsEmpty() {return top==-1;}
bool IsFull() {return top==maxTop;}
bool Top(T &x) const;
bool Pop();
bool Push(T x);
bool Clear() {top=-1;}
};
template<class T>
SeqStack<T>::SeqStack(int mSize)
{
top=-1;
maxTop=mSize-1;
s=new T[mSize];
}
template<class T>
bool SeqStack<T>::Top(T &x) const
{
if(IsEmpty())
cout<<"Empty"<<endl; return false;
x=s[top]; return true;
}
template<class T>
bool SeqStack<T>::Pop()
{
if(IsEmpty())
cout<<"Empty"<<endl; return false;
topp--; return true;
}
template<class T>
bool SeqStack<T>::Push(T x)
{
if(IsFull())
cout<<"Full"<<endl; return false;
s[++top]=x;
return true;
}






#include<iostream.h>
template<class T>
class Queue
{
public:
virtual bool IsEmpty() const=0;
virtual bool IsFull() const=0;
virtual bool Front(T &x) const=0;
virtual bool EnQueue(T x)=0;
virtual bool DeQueue()=0;
virtual void Clear()=0;
};
#include"queue.h"
template<class T>
class SeqQueue:public Queue<T>
{
private:
int front,rear; //front指向对头元素的前一单元
int maxSize;
T *q;
public:
SeqStack(int mSize);
~SeqStack() { delete []q;}
bool IsEmpty() const { return rear=front;}
bool IsFull() const { return front==(rear+1)%maxSize;}
bool Front(T &x) const;
bool EnQueue(T x);
bool DeQueue();
void Clear() {front=rear=0;}
};
template<class T>
SeqStack<T>::SeqStack(int mSize)
{
maxSize=mSize;
front=rear=0;
q=new T[maxSize];
}
template<class T>
bool SeqStack<T>::Front(T &x)
{
if(IsEmpty())
cout<<"Empty"<<endl; return false;
T x=q[(front+1)%maxSize];
return true;
}
template<class T>
bool SeqStack<T>::EnQueue(T x)
{
if(IsFull())
cout<<"Full"<<endl; return false;
q[(rear+1)%maxSize]=x;
return true;
}
template<class T>
bool SeqStack<T>::DeQueue()
{
if(IsEmpty())
cout<<"Empty"<<endl; return false;
front=(front+1)%maxSize;
return true;
}
#include"stack.h"
#include<math.h>
class Calculator
{
public:
Calculator(int mSize):S(mSize);
bool Clear(s.Clear());
void Run();
private:
Stack<double> s;
bool GetOperands(double &,double &);
void DoOperator(char);
};
bool Calculator::GetOperands(double &op1,double &op2)
{
if(s.Top(op1))
cout<<"Missing Operands"<<endl; return false;
s.Pop();
if(s.Top(op2))
cout<<"Missing Operands"<<endl; return false;
s.Pop();
return true;
}
void Calculator::DoOperator(char c)
{
bool result;
double op1,double op2;
result=GetOperands(op1,op2);
if(result)
{
switch(c){
case'+':s.push(op2+op1);break;
case'-':s.push(op2-op1);break;
case'*':s.push(op2*op1);break;
case'/':s.push(op2/op1);break;
case'^':s.push(op2^op1);break;
}
}
else cout<<"Missing Operands"<<endl;
}
void Calculator::Run()
{
char c;
double newop;
while(cin>>c,c!='#')
{
if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^')
DoOperator(c);
else
{
cin.putback(c);
cin>>newop;
s.push(newop);
}
}
if(s.Top(newop)) cout<<newop<<endl;
}
void InfixToPostfix()
{
Stack<char> s(size);
char c,y;
s.push('#');
while(cin>>c,c!='#')
{
if(isdigit(c) || isalpha(c)) cout<<c;
else
{
if(c==')')
for(s.Top(y),s.Pop();y!='(';s.Top(y),s.Pop()) cout<<y;
else
{
for(s.Top(y),s.Pop();icp(c)<=isp(y);s.Top(y),s.Pop())
cout<<y;
s.push(y);
s.push(c);
}
}
}
while(!s.Empty())
{
s.Top(y);
s.Pop();
if(y!='#')
cout<<y;
}
cout<<endl;
}




#include<assert.h>
template<class T>
class Array1D
{
private:
T *s;
int size;
public:
Arry1D(int SIZE=0);
~Array1D(delete []s;)
T& operator [](int i)const;
Array1D<T>& operator =(const Array1D<T> &a);
friend ostream& operator <<( ostream& out,Array1D<T> &r);
friend istream& operator >>( istream& in, Array1D<T> &r);
};
template<class T>
Array1D<T>::Array1D(int SIZE)
{
assert(SIZE>=0);
size=SIZE;
s=new T[size];
}
template<class T>
T& Array1D<T>::operator [](int i)const
{
assert(i>=0&&i<size);
return s[i];
}
template<class T>
Array1D<T>& Array1D<T>::operator =(const Array1D<T> &a)
{
if(this!=&a)
{
size=a.size;
delete []s;
s=new T[size];
for(int i=0;i<size;i++)
s[i]=a.s[i];
}
return *this;
}
template<class T>
ostream& Array1D<T>::operator <<(ostream& out,Array1D<T> &r)
{
cout<<"Array=";
for(int i=0;i<size;i++)
out<<r.s[i]<<"  ";
return out;
}
istream& Array1D<T>::operator >>(istream& in, Array1D<T> &r)
{
cout<<"Input array:"<<endl;
for(int i=0;i<size;i++)
in>>r.s[i];
return in;
}




template<class T>
void SeqTriple<T>::Transpose(SeqTriple<T>& B)const
{
int *num=new int[n]; int *k=new int[n];
B.m=n; B.n=m; B.t=t;
if(t>0)
{
for(int i=0;i<n;i++) num[i]=0;
for(i=0;i<t;i++) num[trip[i].col]++;
k[0]=0;
for(i=1;i<n;i++) k[i]=k[i-1]+num[i-1];
for(i=0;i<t;i++)
{
int j=k[trip[i].col]++;
B.trip[j].row=trip[i].col;
B.trip[j].col=trip[i].row;
B.trip[j].value=trip[i].value;
}
}
delete []num; delete []k;
}






int String::Find(int i,String &p)
{
if(i<0 || i>n-1)
{
cout<<"Out of Bounds"<<endl; 
return -1;
}
char *pp=P.str;
char *t=str+i;
while(*pp!='\x0'&&i<=n-P.n)
{
if(*pp++!=*t++)
{
pp=P.str;
t=str+(++i);
}
if(*pp=='\0') return i;
return -1;
}


}
int String::FindKMP(int i,String &P)
{
if(i<0 || i>n-1)
cout<<"Out of bounds!"<<endl; return -1;
int j=0,m=P.n;
while(i<n && j<m)
{
if(j==-1 || str[i]==str[j])
i++; j++;
else 
j=P.f[j];
}
return((j==m)?i-m:-1;
}
void String::Find()
{
int j=0;k=-1;f[0]=-1;
while(j<n)
{
if(k==-1 ||str[j]==str[k])
{
j++;k++;
if(str[j]==str[k]) f[j]=f[k];
else
f[j]=k;
}
else
k=f[k];
}
}




template<class T> //二叉树结点类
struct BTNode
{
BTNode() {lChild=rChild=NULL;}
BTNode(const T& x)
{
element=x;lChild=rChild=NULL;
}
BTNode(const T& x,BTNode<T> *l,BTNode<T> *r)
{
element=x;lChild=l;rCHild=r;
}
T element;
BTNode<T>* rChild, *lChild;
};
template<class T>
bool BinaryTree<T>::Root(T &x)const
{
if(root)
x=root->element; return true;
return false;
}
template<class T>
void BinaryTree<T>::MakeTree(const T&x, BinaryTree<T>& left,BinaryTree<T>& right)
{
if(root ||&left==&right) return;
root=new BTNode<T>(x,left.root,right.root);
letf.root=right.root=NULL;
}
template<class T>
void BInaryTree<T>::BreakTree(T &x,BinaryTree<T>& left,BinaryTree<T>& right)
{
if(!root || &right==&left || right.root || left.root) return;
x=root.element;
left.root=root->lChild;right.root=root->rChild;
delete root;
}
template<class T>
void Visit(T& x)
{
cout<<x<<" ";
}
template<class T>
void BinaryTree<T>::PreOrder(void (*Visit) (T& x))
{
PreOrder(Visit,root);
}
void BinaryTree<T>::PreOrder(void (*Visit) (T& x),BTNode<T>* t)
{
if(t)
{
Visit(t->element);
PreOrder(Visit,t->lChild);
PreOrder(Visit,t->rChild);
}
}




int BinartTree<T>::Size(BTNode<T>* t) //递归思想
{
if(!t) return 0;
return Size(t->lChild)+Size(t->rChild)+1;
}
template<class T>
BTNode<T>* BinartTree<T>::Copy(BTNode<T> *t)
{
if(!t) return NULL;
BTNode<T>* q=new BTNode<T>(t->element);
q->lChild=Copy(t->lChild);
q->rChild=Copy(t->rChild);
return q;
}




int String::Find(int i,String &P)
{
if(i<0 || i>n-1) 
cout<<"Out of bounds!"<<endl; return -1;
char* p=P.str;
char* q=str+i;
while(i<n-P.n && *p!='\x0')
{
if(*p==*q)
p++;q++; 
else
q=str+(++i);p=P.str;
}
if(*p=='\x0') return i;
return -1;
}


template<class T> //规范遍历器类格式 
class BIterator
{
public:
virtual T* GoFirst(const BinaryTree<T>& bt)=0;
virtual T* Next(void)=0;
virtual void Traverse(void (*Visit)(T &x),const BinarytTree<T>& bt);
protected:
BTNode<T>* r,*current;
};
template<class T>
void BIterator<T>::Traverse(void (*Visit)(T &x),const BinaryTree<T>& bt)
{
T* p=GoFirst(bt);
while(p)
{
Visit(*p);p=Next();
}
}






template<class T>
class IInOrder:public BIterator<T>
{
public:
IInOrder(BinaryTree<T>& bt,int mSize)
{
r=bt.root;current=NULL;
s=new SeqStack<BTNode<T>* >(mSize);
}
T* GoFirst(const BinartTree<T>& bt);
T* Next(void);
private:
SeqStack<BTNode<T>*> *s;
}
template<class T>
T* IInOrder<T>::GoFirst(const BinaryTree<T>& bt)
{
current=bt.root;
if(!current) return NULL;
while(current->lChild){
s->Push(current); current=current->lChild;
}
return &current->element;
}
template<class T>
T* IInOrder<T>::Next(void)
{
BTNode<T>* p;
if(current->rChild)
{
p=current->rChild;
while(p->lChild)
{
s->push(p);
p=p->lChild;
}
current=p;
}
else if(s->IsEmpty()==true) current=NULL; return NULL;
s->Top(current);s->Pop();
return &current->element;
}










 void CreatHeap(int n, T heap[])
 {
for(int i=n/2;i>0;i--) AdjustDown(heap,i,n);
 }
 void AdjustDown(T heap[],int i,int j)
 { 
int child=2*i;
T temp=heap[i];
while(child<=j)
{
if(child+1<=j&&heap[child]>heap[child+1]) child++;
if (temp<=heap[child]) break;
heap[child/2]=heap[child];
child=2*child;
}
heap[child/2]=temp;
 }










template<class T>
class PrioQueue
{
public:
PrioQueue(int mSize);
~PrioQueue() {delete []q;}
bool IsEmpty() {return n==1;}
bool IsFull() {return n==maxSize;}
void Append(T x);
void Serve(T &x);
private:
int n,maxSize;
T* q;
void AdjustDown(int i,int j);
void AdjustUp(int i);
};
template<class T>
PrioQueue<T>::PrioQueue(int mSize)
{
maxSize=mSize;n=0;
q=new T[maxSize+1];
}
template<class T>
void Prioqueue<T>::Append(T x)
{
if(IsFull()) throw Overflow;
q[++n]=x;
AdjustUp(n);
}
template<class T>
void PrioQueue<T>::Serve(T &x)
{
if(IsEmpty()) throw Underflow;
x=q[1];q[1]=q[n--];
AdjustDown(1,n);
}
template<class T>
void PrioQueue<T>::AdjustUp(int i)
{
int j=i;
T temp=q[j];
while(j>=2)
{
if(q[j/2]<=q[j]) break;
q[j]=q[j/2];j=j/2;
}
q[j]=temp;
}
void main()
{
try{}
catch(ResultCode err){
switch(err){
case Overflow:cout<<"Overflow!"<<endl;break;
case Underflow:cout<<"Underflow!"<<endl;break;
}
}
}




template<class T> //哈夫曼树
HfmTree<T>::CreatHfmTree(T w[],int n)
{
PrioQueue<HfmTree<T>> pq(n);
HfmTree<T> x,y,z,zero;
for(int i=0;i<n;i++)
{
z.MakeTree(w[i],x,y);z.putW(w[i]);
pq.Append(z);
z.SetNull();
}
for(int i=0;i<n;i++)
{
pq.Serve(x);pq.Serve(y);
z.MakeTree(x.getW()+y.getW(),x,y);
z.putW(x.getW()+y.getW());
pq.Append(z);
z.SetNull();
}
pq.Serve(z);
return z;
}










#include<iostream.h>
class UFSet
{
public:
UFSet(int mSize);
~UFSet(){delete []parent;}
int Find(int i)const;
void Union(int x,int y);
private:
int *parent;
int size;
};
UFSet::UFSet(int mSize)
{
size=mSize;
parent=new int[size];
for(int i=0;i<n;i++) parent[i]=-1;
}
int UFSet::Find(int i)
{
int r,t,l;
for(r=i;parent[r]>=0;r=parent[r]);
if(i!=r)
{
for(t=i;parent[t]!=r;t=l)
l=parent[t];parent[t]=r;
}
return r;
}
void UFSet::Union(int x,int y)
{
int temp=parent[x]+parent[y];
if(parent[x]>=parent[y])
parent[x]=y;parent[y]=temp;
parent[y]=x;parent[x]=temp;
}






template<class D,class K>
struct E
{
operator K()const{return key;} //重载类型转换符
K key;
D data;
};
template<class T> //规范集合的格式,作为以后集合的各种表示的基类
class DynamicSet
{
public:
virtual ResultCode Search(T& x)const=0;
virtual ResultCode Insert(T& x)=0;
virtual ResultCode Remove(T& x)=0;
virtual bool IsEmpty()const=0;
virtual bool IsFull()const=0;
};


template<class T>
class ListSet:public DynamicSet<T>
{
public:
ListSet(int mSize);
~ListSet(){delete []l;}
bool IsEmpty()const{return n==0;}
bool IsFull()const{return n==maxSize;}
ResultCode Search(T& x)const;
ResultCode Insert(T& x);
ResultCode Remove(T& x);
private:
int maxSize;
int n;
T* l;
};
template<class T>
ResuleCode ListSet<T>::Search(T& x)const
{
for(int i=0;i<n;i++)
if(l[i]==x)
x=l[i]; return Success;
return NotPresent;
}
template<class T>
ResultCode ListSet<T>::Search(T& x)const
{
for(int i=0;l[i]<x;i++);
if(l[i]==x) x=l[i];return Success;
return NotPresent;
}
template<class T>
ResutlCode ListSet<T>::Search(T& x)const
{
int i=BSearch(x,0,n-1);
if(i==-1) return NotPresent;
x=l[i]; return Success;
}
template<class T>
ResultCode ListSet<T>::BSearch(T& x,int low,int high)const
{
if(low<=high)
{
m=(low+high)/2;
if(x>l[m]) return BSearch(x,m+1;high);
else if(x<l[m]) return BSearch(x,low,m-1);
return m;
}
return -1;
}
template<class T>
ResultCode ListSet<T>::BSearch(T& x)const
{
int m,low=0,high=n-1;
while(low<=high)
{
m=(low+high)/2;
if(x<l[m]) high=m-1;
else if(x>l[m]) low=m+1;
else
x=l[m];return Success;
}
return NotPresent;
}




template<class T> //二叉搜索树类
class BSTree:public DynamicSet<T>
{
public:
BSTree(){root=NULL;}
ResultCode Search(T& x)const;
ResultCode Insert(T& x);
ResultCode Remove(T& x);
protected:
BTNode<T>* root;
private:
ResultCode Search(BTNode<T>* p,T& x)const;
};




template<class T>
ResultCode BSTree<T>::Search(T& x)const
{
return Search(root,x);
}
template<class T>
ResultCode BSTree<T>::Search(BTNode<T>* p,T& x)const
{
if(!p) return NotPresent;
else if(x<p->element) return Search(root->lChild,x);
else if(x>p->element) return Search(root->rChild,x);
else
x=p->element; return Success;
}




template<class T>
ResultCode BSTree<T>::Search(BTNode<T>* p,T& x)const
{
BTNode<T>* p=root;
while(p)
{
if(x<p->element) p=p->lChild;
else if(x>p->element) p=p->rChild;
else x=p->element; return Success;
}
return NotPresent;
}  


template<class T>
ResultCode BSTree<T>::Insert(T& x)
{
BTNode<T>* p=root,*q=NULL;
while(p)
{
q=p;
if(x<p->element) p=p->lChild;
else if(x>p->element) p=p->rChild;
else x=p->element; return Duplicate;
}
p=new BTNode<T>(x);
if(!root) root=p;
else if(x<q->element) q->lChild=p;
else q->rChild=p;
return Success;
}




template<class T>
ResultCode BSTree<T>::Remove(T& x)
{
BTNode<T>*p=root,*q=NULL,*r,*c,*s;
while(p && x!=p->element)
{
q=p;
if(x<p->element) p=p->lChild;
else p=p->rChild;
}
if(!p) return NotPresent;
x=p->element;
if(p->lChild&&p->rChild)
{
s=p->rChild;
while(s->lChild)
{
r=s;
s=s->lChild;
}
p->element=s->element;
p=s;q=r;
}
if(p->lChild) c=p->lChild;
c=p->rChild;
if(p==root) root=c;
else if(p==q->lChild) q->lChild=c;
else q->rChild=c;
delete p;
return Success;
}




template<class T>
struct AVLNode
{
AVLNode(T& x)
{
element=x;
bF=0;
lChild=rChild=NULL;
}
T element;
int bF;
AVLNode<T>* lChild,* rChild;
};
template<class T>
class AVLTree:public DynamicSet<T>
{
public:
AVLTree(){root=NULL;}
ResultCode Search(T& x)const;
ResultCode Insert(T& x);
ResultCode Remove(T& x);
private:
AVLNode<T>* root;
ResultCode Insert(AVLNode<T>* &p,T& x,bool &unBalanced);
void LRotation(AVLNode<T>* s,bool &unBalanced);
void RRotation(AVLNode<T>* s,bool &unBalanced);
};








r=s->lChild;
if(r->bF==1)
{
s->lChild=r->rChild;r=rChild=s;
s->bF=0;s=r;
}




u=r->rChild; r->rChild=u->lChild;
u->lChild=r;u->rChild=s;s->lChild=u->rChild;
switch(u->bF)
{
case 1:s->bF=-1;r->bF=0;break;
case 0:s->bF=0;r->bF=0;break;
case -1:s->bF=0;r->bF=1;
}
s=u;


template<class T>
void AVLTree<T>::LRotation(AVLNode<T>* &s,bool &unBalanced)
{
AVLNode<T>*u,*r=s->lChild;
if(r->bF==1)
{
s->lChild=r->rChild;r->rChild=s;
s->bF=0;s=r;
}
else
{
u=r->rChild;r->rChild=u->lChild;
u->lCHild=r;u->rChild=s;
s->lChild=u->rChild;
switch(u->bF)
{
case 1:s->bF=-1;r->bF=0;break;
case 0:s->bF=r->bF=0;break;
case -1:s->bF=0;r->bF=1;
}
s=u;
}
s->bF=0;
unBalanced=false;
}










template<class T>
ResultCode AVLTree<T>::Insert(AVLNode<T>* &p,T& x,bool &unBalanced)
{
ResultCode result=Success;
if(p==NULL)
p=new AVLNode<T>(x);unBalanced=true;
else if(x<p->element)
{
result=Insert(p->lChild,x,unBalanced);
if(unBalanced)
{
switch(p->bF)
{
case -1:p->bF=0;unBalanced=false;break;
case 0:p->bF=1;break;
case 1:LRotation(p,unBalanced);
}
}
}
else if(x==p->element)
unBalanced=false;x=p->element;result=Duplicate;
else{
result=Insert(p->rChild,x,unBalanced);
if(unBalanced)
{
switch(p->bF)
{
case 1:p->bF=0;unBalanced=false;break;
case 0:p->bF=-1;break;
case -1:RRotation(p,unBalanced);
}
}
}
return rusult;
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值