从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;