第一阶段锻炼基础编程能力

从9.19到9.27,一个星期左右的编程使得基础编程能力有所提升,整个大一大约编了2000行代码,而大二这将近一星期就写了2500行代码。人生就得对自己有所要求,自己就该有所担当和追求,让自己充实起来。从第三周到第四周的两个星期基本学完了数据结构,接下来一星期争取学完离散结构。加油!

template //栈的顺序实现

class SeqStack:public Stack
{
private:
int top;
int maxTop;
T *s;
};










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












#include "linearlist.h" //结点类和单链表类
template class SingleList;
template
class Node
{
private:
T elements;
Node *link;
friend class SingleList;
};
template
class SingleList:public LinearList
{
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* first;
};
template
SingleList::~SingleList()
{
Node *p;
while(first)
{
p = first->link;
delete first;
first = p;
}
}
template
bool SeqList::Insert(int i,T x)
{
if(i<0 ||i>n-1)
cout<<"Out of Bounds"< Node * q=new Node;
q->element=x;
Node *p=first;
for(int j=0;j p=p->link;
if(i>-1)
{
q->link=p->link;
p->link=q;
}
else{
q->link=first;
first=q;
}
n++;
return true;
}
template
bool SeqList::Delete(int i)
{
if(!n)
cout<<"UnderFlow"< if(i<0 ||i>n-1)
cout<<"Out of Bounds"< Node *p=first, *q=first;
for(int j=0;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->expexp)
{
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 SeqStack:public Stack
{
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
SeqStack::SeqStack(int mSize)
{
maxTop=mSize-1;
s=new T[mSize];
top=-1;
}
template
bool SeqStack::Top(T &x) const
{
if(IsEmpty())
{
cout<<"Empty"< return false;
}
x=s[top];
return true;
}
template
bool SeqStack::Push(T x)
{
if(IsFull())
cout<<"Overflow"< s[++top]=x;
return true;
}
template
bool SeqStack::Pop()
{
if(IsEmpty())
cout<<"Underflow"< top--;
return true;
}














#include"queue.h" //循环队列类
template
class SeqQueue:public Queue
{
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
SeqQueue::SeqQueue(int mSize)
{
maxSize=mSize;
q=new T[maxSize];
front=rear=0;
}
template
bool SeqQueue::Front(T& x) const
{
if(IsEmpty())
cout<<"empty"< x= q[(front+1)%maxSize];
return true;
}
template
bool SeqQueue::EnQueue(T x)
{
if(IsFull())
cout<<"Full"< q[(rear+1)%maxSize]=x;
return true;
}
template
bool SeqQueue::DeQueue()
{
if(IsEmpty())
cout<<"Underflow"< front=(front+1)%maxSize;
return true;













void String::Fail() //失败函数
{
int j=0, k=-1,;f[0]=-1;
while(j {
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 //向下调整
void AdjustDown( T heap[],int r, int j)
{
int child=2*r+1;
T temp=heap[r];
while(child<=j)
{
if((childheap[child+1])) child++;
if((temp<=heap[child]) break;
heap[(child-1)/2]=heap[child];
child=child*2+1;
}
heap[(child-1)/2]=temp;
}
template
void CreatHeap(T heap[],int n)
{
for(int i=(n-2)/2;i>-1;i--)
AdjustDown(heap,i,n-1);
}








template
HfmTree CreatHfmTree (T w[],int n)
{
PrioQueue> pq(n);
HfmTree x,y,z,zero;
for(int i=0;i {
z.MakeTree(w[i],x,y);z.putW(W[i]);
pq.Append(z);
z.SetNull();
}
for (i=1;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 //二叉搜索树的删除运算
ResultCode BSTree::Remove(T& x)
{
BTNode *c,*s,*r,*p=root,*q=NULL;
while(p && p->element!=x)
{
q=p;
if(xelement) 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 //二叉平衡树左旋转函数
void AVLTree::LRotation(AVLNode* &s bool &unBalanced)
{
AVLNode*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 //跳表的搜索
ResultCode SkipList::Search(T& x)const
{
if(x>=tail->element) return RangeError;
SNode*p=head;
for(int i=levels;i>=0;i--)
while(p->link[i]->elementlink[i];
if(x==p->link[0]->element) return Success;
else return NotPresent;
}
















template //跳表的插入
int SkipList::Level()
{
int lev=0;
while(rand()<= RAND_MAX/2) level++;
return (lev<=maxLevel)?lev:maxLevel;
}
template
SNode* SkipList::SaveSearch(const T& x)
{
SNode* p=head;
for(int i=levels;i>=0;i--)
{
while(p->link[i]->elementlink[i];
last[i]=p;
}
return (p->link[i]);
}
template
ResultCode SkipList::Insert(T& x)
{
if(x>=tail->element) return RangeError;
SNode* p=SaveSearch(x);
if(p->element==x) return Duplicate;
int lev=Level();
if(lev>levels)
{
lev=++levels;last[lev]=head;
}
SNode* y=new SNode(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 //跳表的删除
ResultCode SkipList::Remove(T& x)
{
if(x>=tail->element) return RangeError;
SNode*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 //线性探查散列表的搜索
ResultCode HashTable::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
ResultCode HashTable:: Search(T& x)const
{
int pos;
if(Find(x,pos)==Success) return Success;
return NotPresent;
}


















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
















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




















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














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


















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
















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
















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


















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












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










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










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
















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












template //两路合并
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 delete []Temp;
}








template //合并排序
void MergeSort(T A[],int n)
{
int i1,j1,i2,j2;
int size=1;
while(size {
i1=0;
while(i1+size {
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 //堆排序
void AdjustDown(T A[],int r,int j)
{
int child=2*r+1; T temp=A[r];
while(child<=j)
{
if((child if(temp>=A[child]) break;
A[(child-1)/2]=A[child];
child=2*child+1;
}
A[(child-1)/2]=temp;
}
template
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 //基数排序
void RadixSort(Term L[])
{
int tail;
int f[radix],r[radix]);
for(int i=0;i {
for(int j=0;j 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 {
for(j++;(j if(f[j])
{
L[tail].next=f[j];
tail=r[j];
}
}
L[tail].next=0;
}
}




















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
















#include //规范线性表类格式,作为顺序表类和链表类的基类
template
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 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
SeqList::SeqList(int mSize) //使用时:template LA(SIZE);
{
maxLength=mSize;
s=new T[maxLength];
n=0;
}
template
bool SeqList::IsEmpty()
{
/*if(!n)
return true;
return false;*/
return n==0;
}
template
int SeqList::Length()
{
return n;
}
template
bool SeqList::IsFull()
{
return n==maxLength;
}
template
bool SeqList::Insert(int i,T &x)
{
if(IsFull())
cout<<"OverFlow"< if(i<-1 || i>n-1)
cout<<"Out of Bounds"< for(int j=n;j>i;j--)
s[j]=s[j-1];
s[i]=x;
n++;
return true;
}
template
bool SeqList::Delete(int i)
{
if(i<0 || i>n-1)
cout<<"Out of Bounds"< if(IsEmpty())
cout<<"UnderFlow"< for(int j=i;j s[j]=s[j+1];
n--;
return true;
}
template
bool SeqList::Find(int i,T &x)
{
if(i<0 ||i>n-1)
cout<<"Out of Bounds"< x=s[i];
return true;
}
template
int SeqList::Search(T &x)
{
for(int i=0;i if(x==s[i]) return i;
return -1;
}
template
bool SeqList::Update(int i,T &x)
{
if(i<0 || i>n-1)
cout<<"Out of Bounds"< s[i]=x;
return true;
}
template
void SeqList::Output(ostream& out) //使用时:LA.Output(cout); 
{
for(int i=0;i out< out< }














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






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










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






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










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


DNode*q=new DNode, 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< switch(val.exp){
case 0:break;
case 1:out<<"X"; break;
default: out<<"X^"< }
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):"< 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< }
void Polynominal::PolyAdd(Polynominal &r)
{
Term* q,*q1=theList,*p;
p=r.theList->link;
q=q1->link;
while(p->exp>=0)
{
while(p->expexp)
{
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< cin>>q; cout< q=q+p; cout< }






#include //堆栈类 规范堆栈格式
using namespace std;
template
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 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
SeqStack::SeqStack(int mSize)
{
top=-1;
maxTop=mSize-1;
s=new T[mSize];
}
template
bool SeqStack::Top(T &x) const
{
if(IsEmpty())
cout<<"Empty"< x=s[top]; return true;
}
template
bool SeqStack::Pop()
{
if(IsEmpty())
cout<<"Empty"< topp--; return true;
}
template
bool SeqStack::Push(T x)
{
if(IsFull())
cout<<"Full"< s[++top]=x;
return true;
}






#include
template
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 SeqQueue:public Queue
{
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
SeqStack::SeqStack(int mSize)
{
maxSize=mSize;
front=rear=0;
q=new T[maxSize];
}
template
bool SeqStack::Front(T &x)
{
if(IsEmpty())
cout<<"Empty"< T x=q[(front+1)%maxSize];
return true;
}
template
bool SeqStack::EnQueue(T x)
{
if(IsFull())
cout<<"Full"< q[(rear+1)%maxSize]=x;
return true;
}
template
bool SeqStack::DeQueue()
{
if(IsEmpty())
cout<<"Empty"< front=(front+1)%maxSize;
return true;
}
#include"stack.h"
#include
class Calculator
{
public:
Calculator(int mSize):S(mSize);
bool Clear(s.Clear());
void Run();
private:
Stack s;
bool GetOperands(double &,double &);
void DoOperator(char);
};
bool Calculator::GetOperands(double &op1,double &op2)
{
if(s.Top(op1))
cout<<"Missing Operands"< s.Pop();
if(s.Top(op2))
cout<<"Missing Operands"< 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"< }
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< }
void InfixToPostfix()
{
Stack s(size);
char c,y;
s.push('#');
while(cin>>c,c!='#')
{
if(isdigit(c) || isalpha(c)) cout< else
{
if(c==')')
for(s.Top(y),s.Pop();y!='(';s.Top(y),s.Pop()) cout< else
{
for(s.Top(y),s.Pop();icp(c)<=isp(y);s.Top(y),s.Pop())
cout< s.push(y);
s.push(c);
}
}
}
while(!s.Empty())
{
s.Top(y);
s.Pop();
if(y!='#')
cout< }
cout< }




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




template
void SeqTriple::Transpose(SeqTriple& 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 for(i=0;i k[0]=0;
for(i=1;i for(i=0;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"< 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!"< int j=0,m=P.n;
while(i {
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 {
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 //二叉树结点类
struct BTNode
{
BTNode() {lChild=rChild=NULL;}
BTNode(const T& x)
{
element=x;lChild=rChild=NULL;
}
BTNode(const T& x,BTNode *l,BTNode *r)
{
element=x;lChild=l;rCHild=r;
}
T element;
BTNode* rChild, *lChild;
};
template
bool BinaryTree::Root(T &x)const
{
if(root)
x=root->element; return true;
return false;
}
template
void BinaryTree::MakeTree(const T&x, BinaryTree& left,BinaryTree& right)
{
if(root ||&left==&right) return;
root=new BTNode(x,left.root,right.root);
letf.root=right.root=NULL;
}
template
void BInaryTree::BreakTree(T &x,BinaryTree& left,BinaryTree& right)
{
if(!root || &right==&left || right.root || left.root) return;
x=root.element;
left.root=root->lChild;right.root=root->rChild;
delete root;
}
template
void Visit(T& x)
{
cout< }
template
void BinaryTree::PreOrder(void (*Visit) (T& x))
{
PreOrder(Visit,root);
}
void BinaryTree::PreOrder(void (*Visit) (T& x),BTNode* t)
{
if(t)
{
Visit(t->element);
PreOrder(Visit,t->lChild);
PreOrder(Visit,t->rChild);
}
}




int BinartTree::Size(BTNode* t) //递归思想
{
if(!t) return 0;
return Size(t->lChild)+Size(t->rChild)+1;
}
template
BTNode* BinartTree::Copy(BTNode *t)
{
if(!t) return NULL;
BTNode* q=new BTNode(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!"< char* p=P.str;
char* q=str+i;
while(i {
if(*p==*q)
p++;q++; 
else
q=str+(++i);p=P.str;
}
if(*p=='\x0') return i;
return -1;
}


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






template
class IInOrder:public BIterator
{
public:
IInOrder(BinaryTree& bt,int mSize)
{
r=bt.root;current=NULL;
s=new SeqStack* >(mSize);
}
T* GoFirst(const BinartTree& bt);
T* Next(void);
private:
SeqStack*> *s;
}
template
T* IInOrder::GoFirst(const BinaryTree& bt)
{
current=bt.root;
if(!current) return NULL;
while(current->lChild){
s->Push(current); current=current->lChild;
}
return ¤t->element;
}
template
T* IInOrder::Next(void)
{
BTNode* 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 ¤t->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 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
PrioQueue::PrioQueue(int mSize)
{
maxSize=mSize;n=0;
q=new T[maxSize+1];
}
template
void Prioqueue::Append(T x)
{
if(IsFull()) throw Overflow;
q[++n]=x;
AdjustUp(n);
}
template
void PrioQueue::Serve(T &x)
{
if(IsEmpty()) throw Underflow;
x=q[1];q[1]=q[n--];
AdjustDown(1,n);
}
template
void PrioQueue::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!"< case Underflow:cout<<"Underflow!"< }
}
}




template //哈夫曼树
HfmTree::CreatHfmTree(T w[],int n)
{
PrioQueue> pq(n);
HfmTree x,y,z,zero;
for(int i=0;i {
z.MakeTree(w[i],x,y);z.putW(w[i]);
pq.Append(z);
z.SetNull();
}
for(int i=0;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
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 }
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
struct E
{
operator K()const{return key;} //重载类型转换符
K key;
D data;
};
template //规范集合的格式,作为以后集合的各种表示的基类
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 ListSet:public DynamicSet
{
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
ResuleCode ListSet::Search(T& x)const
{
for(int i=0;i if(l[i]==x)
x=l[i]; return Success;
return NotPresent;
}
template
ResultCode ListSet::Search(T& x)const
{
for(int i=0;l[i] if(l[i]==x) x=l[i];return Success;
return NotPresent;
}
template
ResutlCode ListSet::Search(T& x)const
{
int i=BSearch(x,0,n-1);
if(i==-1) return NotPresent;
x=l[i]; return Success;
}
template
ResultCode ListSet::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 return m;
}
return -1;
}
template
ResultCode ListSet::BSearch(T& x)const
{
int m,low=0,high=n-1;
while(low<=high)
{
m=(low+high)/2;
if(x else if(x>l[m]) low=m+1;
else
x=l[m];return Success;
}
return NotPresent;
}




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




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




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


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




template
ResultCode BSTree::Remove(T& x)
{
BTNode*p=root,*q=NULL,*r,*c,*s;
while(p && x!=p->element)
{
q=p;
if(xelement) 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
struct AVLNode
{
AVLNode(T& x)
{
element=x;
bF=0;
lChild=rChild=NULL;
}
T element;
int bF;
AVLNode* lChild,* rChild;
};
template
class AVLTree:public DynamicSet
{
public:
AVLTree(){root=NULL;}
ResultCode Search(T& x)const;
ResultCode Insert(T& x);
ResultCode Remove(T& x);
private:
AVLNode* root;
ResultCode Insert(AVLNode* &p,T& x,bool &unBalanced);
void LRotation(AVLNode* s,bool &unBalanced);
void RRotation(AVLNode* 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
void AVLTree::LRotation(AVLNode* &s,bool &unBalanced)
{
AVLNode*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
ResultCode AVLTree::Insert(AVLNode* &p,T& x,bool &unBalanced)
{
ResultCode result=Success;
if(p==NULL)
p=new AVLNode(x);unBalanced=true;
else if(xelement)
{
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;
}














你可能感兴趣的:(第一阶段锻炼基础编程能力)