提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
继续数据结构
一、相关问题
1.链接矩阵和邻接表
2.图的最优路径
3.出入链接表
4.3的进阶版
5.排序
6.快排
7.排序
8.霍夫曼树编码
9.最大堆
10.KMP算法
二、代码
1-5
代码如下:
1.#include<iostream>
#include<stack>
#include<cstdlib>
#include<ctime>
using namespace std;
struct Arcnode
{
int j;
Arcnode*next;
int i;
int weight;
};
struct edge
{
Arcnode*adj;
int lowest;
};
struct Vnode
{
int elem;
char data;
Arcnode*first;
};
struct Graph
{
Vnode*v;
int length;
};
void adjlist(char*p,int a[][20],int n,Vnode*v)
{
int i,j;
Arcnode*r,*c;
bool flag=true;
for(i=0;i<n;i++)
{
v[i].elem=i;
v[i].data=p[i];
v[i].first=NULL;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]&&flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
v[i].first=r;
flag=false;
}
else
{
if(a[i][j]&&!flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
c=v[i].first;
while(c->next!=NULL)
c=c->next;
c->next=r;
}
}
}
flag=true;
}
}
void produce(int a[][20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
cout<<"请您输入第"<<i<<"个点与第"<<j<<"之间的权值:";
cin>>a[i][j];
}
}
for(i=0;i<n;i++)
for(j=0;j<i;j++)
a[i][j]=a[j][i];
for(i=0;i<n;i++)
a[i][i]=0;
}
void searchlowest(int&ej,int*U,Graph g,bool*end)
{
int n=U[0];
int i,k,cost,ei;
edge*close=new edge[n+1];
edge lin;
Vnode v;
Arcnode*r;
bool flag;
for(i=1;i<=n;i++)
{
k=U[i];
v=g.v[k];
r=v.first;
flag=false;
for(;r!=NULL;r=r->next)
{
if(!end[r->j])
{
close[i].adj=r;
close[i].lowest=r->weight;
flag=true;
break;
}
}
if(flag)
{
for(;r!=NULL;r=r->next)
{
if(!end[r->j])
{
if((r->weight)<(close[i].lowest))
{
close[i].adj=r;
close[i].lowest=r->weight;
}
}
}
}
else
close[i].adj=NULL;
}
flag=false;
for(i=1;i<=n;i++)
{
if(close[i].adj)
{
lin.adj=close[i].adj;
lin.lowest=close[i].lowest;
flag=true;
break;
}
}
if(flag)
{
for(;i<=n;i++)
{
if(close[i].adj)
{
if(close[i].lowest<lin.lowest)
{
lin.adj=close[i].adj;
lin.lowest=close[i].lowest;
}
}
}
r=lin.adj;
ei=r->i;
ej=r->j;
cost=lin.lowest;
cout<<"这条边:"<<ei<<"->"<<ej<<"权值为:"<<cost<<endl;
}
else
cout<<"出现问题了!"<<endl;
delete []close;
}
void Prim(Graph g)
{
int i,j,k,n;
n=g.length;
int*U=new int [n+1];//约定第0位存储的是数组中的元素个数;
bool*end=new bool[n];//end数组来表示其已经输出过;
for(i=0;i<n;i++)
end[i]=false;
U[0]=1;
U[1]=0;
end[0]=true;
for(k=2;k<=n;k++)
{
searchlowest(j,U,g,end);
end[j]=true;
U[k]=j;
U[0]=k;
}
delete []U;
delete []end;
}
int main()
{
srand(time(0));
int n;
cout<<"请您输入数据个数:";
cin>>n;
char *p=new char[n];
int i,j;
for(i=0;i<n;i++)
p[i]='A'+i;
cout<<"该图的元素为:";
for(i=0;i<n;i++)
cout<<p[i]<<'\t';
cout<<endl;
int a[20][20];
produce(a,n);
cout<<"该图的邻接矩阵为:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<'\t';
cout<<endl;
}
Vnode*v=new Vnode[n];
adjlist(p,a,n,v);
cout<<"链接表为:"<<endl;
Arcnode*r,*c;
for(i=0;i<n;i++)
{
cout<<i<<":"<<v[i].data<<'\t';
r=v[i].first;
if(r)
{
while(r->next!=NULL)
{
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<"->";
r=r->next;
}
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<endl;
}
else
cout<<endl;
}
Graph g;
g.v=v;
g.length=n;
Prim(g);
delete []p;
for(i=0;i<n;i++)
{
r=v[i].first;
while(r)
{
c=r;
r=r->next;
delete c;
}
}
delete []v;
return 0;
}
2.#include<iostream>
#include<stack>
#include<cstdlib>
using namespace std;
struct Arcnode
{
int j;
Arcnode*next;
int i;
int weight;
};
struct Vnode
{
int elem;
int data;
int ru_degree;
int chu_degree;
Arcnode*first;
Arcnode*last;
};
struct Graph
{
Vnode*v;
int length;
};
void chu_adjlist(int*p,int a[][20],int n,Vnode*v)
{
int i,j;
Arcnode*r,*c;
bool flag=true;
for(i=0;i<n;i++)
{
v[i].elem=i;
v[i].data=p[i];
v[i].first=NULL;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]&&flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
v[i].first=r;
flag=false;
}
else
{
if(a[i][j]&&!flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
c=v[i].first;
while(c->next!=NULL)
c=c->next;
c->next=r;
}
}
}
flag=true;
}
}
void ru_adjlist(int a[][20],int n,Vnode*v)
{
int i,j;
Arcnode*r,*c;
bool flag=true;
for(i=0;i<n;i++)
{
v[i].last=NULL;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j][i]&&flag)
{
r=new Arcnode;
r->j=i;
r->i=j;
r->weight=a[j][i];
r->next=NULL;
v[i].last=r;
flag=false;
}
else
{
if(a[j][i]&&!flag)
{
r=new Arcnode;
r->j=i;
r->i=j;
r->weight=a[j][i];
r->next=NULL;
c=v[i].last;
while(c->next!=NULL)
c=c->next;
c->next=r;
}
}
}
flag=true;
}
}
void matrix(int*p,int a[][20],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout<<"请输入"<<i<<"指向"<<j<<"的权值:";
cin>>a[i][j];
cout<<endl;
}
for(i=0;i<n;i++)
a[i][i]=0;
}
void do_degree(Graph g)
{
int n=g.length;
Vnode*v=g.v;
int i,j,k;
Arcnode*r;
for(i=0;i<n;i++)
v[i].ru_degree=0;
for(i=1;i<n;i++)
{
r=v[i].last;
while(r)
{
j=r->i;
k=v[j].ru_degree+r->weight;
if(k>v[i].ru_degree)
v[i].ru_degree=k;
r=r->next;
}
}
for(i=0;i<n;i++)
v[i].chu_degree=v[n-1].ru_degree;
for(i=n-1;i>=0;i--)
{
r=v[i].last;
while(r)
{
j=r->i;
k=(v[i].chu_degree)-(r->weight);
if(k<v[j].chu_degree)
v[j].chu_degree=k;
r=r->next;
}
}
}
void find_import(Graph g,int*import)
{
int n=g.length;
Vnode*v=g.v;
int i,j=0;
import[0]=j;
for(i=0;i<n;i++)
{
if((v[i].chu_degree)==(v[i].ru_degree))
{
import[j+1]=i;
import[0]=j+1;
j++;
cout<<"成功了!"<<endl;
}
}
}
void findpath(Graph g,int*import)
{
int n=g.length;
Vnode*v=g.v;
Arcnode*r;
stack<Arcnode> s;
stack<Arcnode> sr;
int k=import[0],i,j,ei,ej;
for(i=1;i<=k;i++)
{
j=import[i];
r=v[j].first;
while(r)
{
ei=r->i;
ej=r->j;
if((v[ei].ru_degree)==(v[ej].chu_degree-r->weight))
{
s.push(*r);
}
r=r->next;
}
}
Arcnode d;
while(!s.empty())
{
d=s.top();
s.pop();
sr.push(d);
}
while(!sr.empty())
{
d=sr.top();
sr.pop();
cout<<d.i<<"->(权值为:"<<d.weight<<")"<<d.j<<endl;
}
}
int main()
{
int n;
cout<<"请输入图的元素数目:";
cin>>n;
int *p=new int[n];
int i,j;
cout<<"请输入该图的元素:";
for(i=0;i<n;i++)
cin>>p[i];
int a[20][20];
matrix(p,a,n);
cout<<"该图的邻接矩阵为:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<'\t';
cout<<endl;
}
Vnode*v=new Vnode[n];
chu_adjlist(p,a,n,v);
ru_adjlist(a,n,v);
cout<<"出链接表为:"<<endl;
Arcnode*r,*c;
for(i=0;i<n;i++)
{
cout<<i<<":"<<v[i].data<<'\t';
r=v[i].first;
if(r)
{
while(r->next!=NULL)
{
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<"->";
r=r->next;
}
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<endl;
}
else
cout<<endl;
}
cout<<"入链接表为:"<<endl;
for(i=0;i<n;i++)
{
cout<<i<<":"<<v[i].data<<'\t';
r=v[i].last;
if(r)
{
while(r->next!=NULL)
{
cout<<r->i<<"(边的权值为:"<<r->weight<<")"<<"->";
r=r->next;
}
cout<<r->i<<"(边的权值为:"<<r->weight<<")"<<endl;
}
else
cout<<endl;
}
Graph g;
g.v=v;
g.length=n;
do_degree(g);
cout<<"该图各节点的入权值为:";
for(i=0;i<n;i++)
cout<<"第"<<i<<"个节点入权:"<<g.v[i].ru_degree<<endl;
cout<<"该图各节点的出权值为:";
for(i=0;i<n;i++)
cout<<"第"<<i<<"个节点出权:"<<g.v[i].chu_degree<<endl;
int*import=new int[n];
find_import(g,import);
cout<<"关键活动点为:";
for(i=1;i<=import[0];i++)
cout<<import[i]<<'\t';
cout<<endl;
cout<<"关键路径为:"<<endl;
findpath(g,import);
delete []p;
for(i=0;i<n;i++)
{
r=v[i].first;
while(r)
{
c=r;
r=r->next;
delete c;
}
}
delete []v;
delete []import;
return 0;
}
3.#include<iostream>
#include<stack>
#include<cstdlib>
using namespace std;
struct Arcnode
{
int j;
Arcnode*next;
int i;
int weight;
};
struct Vnode
{
int elem;
int data;
Arcnode*first;
Arcnode*last;
};
struct Graph
{
Vnode*v;
int length;
};
void matrix(int*p,int a[][20],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout<<"请输入"<<i<<"指向"<<j<<"的权值:";
cin>>a[i][j];
cout<<endl;
}
for(i=0;i<n;i++)
a[i][i]=0;
}
void chu_adjlist(int*p,int a[][20],int n,Vnode*v)
{
int i,j;
Arcnode*r,*c;
bool flag=true;
for(i=0;i<n;i++)
{
v[i].elem=i;
v[i].data=p[i];
v[i].first=NULL;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]&&flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
v[i].first=r;
flag=false;
}
else
{
if(a[i][j]&&!flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
c=v[i].first;
while(c->next!=NULL)
c=c->next;
c->next=r;
}
}
}
flag=true;
}
}
void ru_adjlist(int a[][20],int n,Vnode*v)
{
int i,j;
Arcnode*r,*c;
bool flag=true;
for(i=0;i<n;i++)
{
v[i].last=NULL;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j][i]&&flag)
{
r=new Arcnode;
r->j=i;
r->i=j;
r->weight=a[j][i];
r->next=NULL;
v[i].last=r;
flag=false;
}
else
{
if(a[j][i]&&!flag)
{
r=new Arcnode;
r->j=i;
r->i=j;
r->weight=a[j][i];
r->next=NULL;
c=v[i].last;
while(c->next!=NULL)
c=c->next;
c->next=r;
}
}
}
flag=true;
}
}
void shortest(Graph g,int k)
{
int n=g.length;
Vnode*v=g.v;
int i,j;
int*D=new int[n];
Arcnode*r;
bool flag=true;
for(i=0;i<n;i++)
D[i]=0;
for(i=0;i<n;i++)
{
r=v[k].first;
while(r)
{
if(r->j==i)
{
D[i]=r->weight;
r=r->next;
break;
}
else
r=r->next;
}
}
int*p=new int[n];
p[0]=k;
j=n-1;
int t,m,ep=0;
int min;
i=0;
while(!D[i])
{
i++;
}
min=i;
for(;i<n;i++)
{
if(D[i]&&D[i]<D[min])
{
min=i;
}
}
ep++;
p[ep]=min;
j--;
for(;j>0;j--)
{
for(i=0;i<n;i++)
{
for(t=0;t<(n-j);t++)
{
m=p[t];
r=v[m].first;
if(!D[i])
{
while(r)
{
if((r->j)==i)
{
D[i]=D[m]+(r->weight);
r=r->next;
}
else
r=r->next;
}
}
else
{
while(r)
{
if((r->j)==i)
{
if((D[m]+(r->weight))<D[i])
D[i]=D[m]+(r->weight);
r=r->next;
break;
}
else
r=r->next;
}
}
}
}
for(i=0;i<n;i++)
{
if(D[i])
{
for(t=0;t<=ep;t++)
{
if(i==p[t])
{
break;
}
}
if(t>ep)
{
break;
}
}
}
min=i;
if(min==n)
{
break;
}
else
{
for(;i<n;i++)
{
if(D[i]&&D[i]<D[min])
{
flag=true;
for(t=0;t<=ep;t++)
{
if(i==p[t])
{
flag=false;
break;
}
}
if(flag)
{
min=i;
}
}
}
ep++;
p[ep]=min;
}
}
cout<<k<<"点到每个顶点的最短路径为:"<<endl;
for(i=0;i<n;i++)
{
if(i==k)
{
continue;
}
else
{
if(!D[i])
{
cout<<"到第"<<i<<"个点的路径为无穷大!"<<endl;
}
else
{
cout<<"到第"<<i<<"个点的最短路径为:"<<D[i]<<endl;
}
}
}
delete []D;
delete []p;
}
int main()
{
int n;
cout<<"请输入图的元素数目:";
cin>>n;
int *p=new int[n];
int i,j;
cout<<"请输入该图的元素:";
for(i=0;i<n;i++)
cin>>p[i];
int a[20][20];
matrix(p,a,n);
cout<<"该图的邻接矩阵为:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<'\t';
cout<<endl;
}
Vnode*v=new Vnode[n];
chu_adjlist(p,a,n,v);
ru_adjlist(a,n,v);
cout<<"出链接表为:"<<endl;
Arcnode*r;
for(i=0;i<n;i++)
{
cout<<i<<":"<<v[i].data<<'\t';
r=v[i].first;
if(r)
{
while(r->next!=NULL)
{
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<"->";
r=r->next;
}
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<endl;
}
else
cout<<endl;
}
cout<<"入链接表为:"<<endl;
for(i=0;i<n;i++)
{
cout<<i<<":"<<v[i].data<<'\t';
r=v[i].last;
if(r)
{
while(r->next!=NULL)
{
cout<<r->i<<"(边的权值为:"<<r->weight<<")"<<"->";
r=r->next;
}
cout<<r->i<<"(边的权值为:"<<r->weight<<")"<<endl;
}
else
cout<<endl;
}
Graph g;
g.v=v;
g.length=n;
shortest(g,4);
return 0;
}
4.#include<iostream>
#include<stack>
#include<cstdlib>
using namespace std;
struct Arcnode
{
int j;
Arcnode*next;
int i;
int weight;
};
struct Vnode
{
int elem;
int data;
Arcnode*first;
Arcnode*last;
int path[20];
};
struct Graph
{
Vnode*v;
int length;
};
void matrix(int*p,int a[][20],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout<<"请输入"<<i<<"指向"<<j<<"的权值:";
cin>>a[i][j];
cout<<endl;
}
for(i=0;i<n;i++)
a[i][i]=0;
}
void chu_adjlist(int*p,int a[][20],int n,Vnode*v)
{
int i,j;
Arcnode*r,*c;
bool flag=true;
for(i=0;i<n;i++)
{
v[i].elem=i;
v[i].data=p[i];
v[i].first=NULL;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]&&flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
v[i].first=r;
flag=false;
}
else
{
if(a[i][j]&&!flag)
{
r=new Arcnode;
r->i=i;
r->j=j;
r->weight=a[i][j];
r->next=NULL;
c=v[i].first;
while(c->next!=NULL)
c=c->next;
c->next=r;
}
}
}
flag=true;
}
}
void ru_adjlist(int a[][20],int n,Vnode*v)
{
int i,j;
Arcnode*r,*c;
bool flag=true;
for(i=0;i<n;i++)
{
v[i].last=NULL;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j][i]&&flag)
{
r=new Arcnode;
r->j=i;
r->i=j;
r->weight=a[j][i];
r->next=NULL;
v[i].last=r;
flag=false;
}
else
{
if(a[j][i]&&!flag)
{
r=new Arcnode;
r->j=i;
r->i=j;
r->weight=a[j][i];
r->next=NULL;
c=v[i].last;
while(c->next!=NULL)
c=c->next;
c->next=r;
}
}
}
flag=true;
}
}
void shortest(Graph g,int k)
{
int n=g.length;
Vnode*v=g.v;
int i,j;
v[k].path[0]=0;
int*D=new int[n];
Arcnode*r;
bool flag=true;
for(i=0;i<n;i++)
{
D[i]=0;
v[i].path[0]=0;
}
for(i=0;i<n;i++)
{
r=v[k].first;
while(r)
{
if(r->j==i)
{
D[i]=r->weight;
v[i].path[0]=1;
v[i].path[1]=0;
r=r->next;
break;
}
else
r=r->next;
}
}
int*p=new int[n];
p[0]=k;
j=n-1;
int t,m,ep=0,h;
int min;
i=0;
while(!D[i])
{
i++;
}
min=i;
for(;i<n;i++)
{
if(D[i]&&D[i]<D[min])
{
min=i;
}
}
ep++;
p[ep]=min;
j--;
v[min].path[0]=1;
v[min].path[1]=v[k].elem;
for(;j>0;j--)
{
for(i=0;i<n;i++)
{
for(t=0;t<(n-j);t++)
{
m=p[t];
r=v[m].first;
if(!D[i])
{
while(r)
{
if((r->j)==i)
{
D[i]=D[m]+(r->weight);
v[i].path[0]=(v[m].path[0])+1;
for(h=1;h<=(v[m].path[0]);h++)
{
v[i].path[h]=(v[m].path[h]);
}
v[i].path[h]=m;
r=r->next;
}
else
r=r->next;
}
}
else
{
while(r)
{
if((r->j)==i)
{
if((D[m]+(r->weight))<D[i])
{
D[i]=D[m]+(r->weight);
v[i].path[0]=(v[m].path[0])+1;
for(h=1;h<=(v[m].path[0]);h++)
v[i].path[h]=(v[m].path[h]);
v[i].path[h]=m;
}
r=r->next;
break;
}
else
r=r->next;
}
}
}
}
for(i=0;i<n;i++)
{
if(D[i])
{
for(t=0;t<=ep;t++)
{
if(i==p[t])
{
break;
}
}
if(t>ep)
{
break;
}
}
}
min=i;
if(min==n)
{
break;
}
else
{
for(;i<n;i++)
{
if(D[i]&&D[i]<D[min])
{
flag=true;
for(t=0;t<=ep;t++)
{
if(i==p[t])
{
flag=false;
break;
}
}
if(flag)
{
min=i;
}
}
}
ep++;
p[ep]=min;
}
}
cout<<k<<"点到每个顶点的最短路径为:"<<endl;
for(i=0;i<n;i++)
{
if(i==k)
{
continue;
}
else
{
if(!D[i])
{
cout<<"到第"<<i<<"个点的路径为无穷大!"<<endl;
}
else
{
cout<<"到第"<<i<<"个点的最短路径为:";
for(h=1;h<=(v[i].path[0]);h++)
{
cout<<(v[i].path[h])<<"->";
}
cout<<i<<'\t'<<"最短路径长度为:"<<D[i]<<endl;
}
}
}
delete []D;
delete []p;
}
int main()
{
int n;
cout<<"请输入图的元素数目:";
cin>>n;
int *p=new int[n];
int i,j;
cout<<"请输入该图的元素:";
for(i=0;i<n;i++)
cin>>p[i];
int a[20][20];
matrix(p,a,n);
cout<<"该图的邻接矩阵为:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<'\t';
cout<<endl;
}
Vnode*v=new Vnode[n];
chu_adjlist(p,a,n,v);
ru_adjlist(a,n,v);
cout<<"出链接表为:"<<endl;
Arcnode*r,*c;
for(i=0;i<n;i++)
{
cout<<i<<":"<<v[i].data<<'\t';
r=v[i].first;
if(r)
{
while(r->next!=NULL)
{
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<"->";
r=r->next;
}
cout<<r->j<<"(边的权值为:"<<r->weight<<")"<<endl;
}
else
cout<<endl;
}
cout<<"入链接表为:"<<endl;
for(i=0;i<n;i++)
{
cout<<i<<":"<<v[i].data<<'\t';
r=v[i].last;
if(r)
{
while(r->next!=NULL)
{
cout<<r->i<<"(边的权值为:"<<r->weight<<")"<<"->";
r=r->next;
}
cout<<r->i<<"(边的权值为:"<<r->weight<<")"<<endl;
}
else
cout<<endl;
}
Graph g;
g.v=v;
g.length=n;
shortest(g,0);
delete []p;
for(i=0;i<n;i++)
{
r=v[i].first;
while(r)
{
c=r;
r=r->next;
delete c;
}
}
delete []v;
return 0;
}
5.#include<iostream>
using namespace std;
struct node
{
int data;
node*next;
};
bool produce(node*maxint,int*a)
{
node*p,*r,*q;
int i;
int n=a[0];
if(n>=1)
{
p=new node;
p->data=a[1];
p->next=maxint;
maxint->next=p;
}
else
{
return false;
}
if(n==1)
{
return true;
}
for(i=2;i<=n;i++)
{
r=new node;
r->data=a[i];
q=maxint;
p=maxint->next;
while((r->data)>(p->data))
{
q=p;
p=p->next;
}
cout<<r->data<<endl;
q->next=r;
r->next=p;
}
return true;
}
void output(node*maxint)
{
node*p=maxint->next;
while((p->data)<(maxint->data))
{
cout<<p->data<<"->";
p=p->next;
}
cout<<maxint->data<<"(这是预先设计的阈值)。"<<endl;
}
void deletenode(node*maxint)
{
node*p=maxint->next,*q;
while((p->data)<(maxint->data))
{
q=p;
p=p->next;
delete q;
}
delete p;
}
int main()
{
int n,i;
cout<<"请您输入数据个数:";
cin>>n;
int*a=new int[n+1];
a[0]=n;
cout<<"请您输入数据(注意:输入数据应小于100):";
for(i=1;i<=n;i++)
cin>>a[i];
node*maxint=new node;
maxint->data=100;
maxint->next=NULL;
if(!produce(maxint,a))
cout<<"请您至少输入一个数据:"<<endl;
else
{
cout<<"排好的数据为:";
output(maxint);
deletenode(maxint);
}
delete []a;
return 0;
}
6-10
代码如下:
6.#include<iostream>
using namespace std;
int partition(int*p,int low,int high)
{
int value=p[low];
while(low<high)
{
while(low<high&&p[high]>=value)
{
high--;
}
p[low]=p[high];
while(low<high&&p[low]<=value)
{
low++;
}
p[high]=p[low];
}
p[low]=value;
return low;
}
void quicksort(int*p,int low,int high)
{
int k;
if(low<high)
{
k=partition(p,low,high);
quicksort(p,low,k-1);
quicksort(p,k+1,high);
}
}
int main()
{
int n;
cout<<"请输入元素个数:";
cin>>n;
int*p=new int[n+1];
int i;
p[0]=n;
cout<<"请您输入数据:";
for(i=1;i<=n;i++)
cin>>p[i];
quicksort(p,1,n);
cout<<"快排之后的序列为:";
for(i=1;i<=n;i++)
cout<<p[i]<<'\t';
cout<<endl;
p[0]=n;
delete []p;
return 0;
}
7.#include<iostream>
using namespace std;
void max(int*a)
{
int n=a[0];
int i=n/2;
int j=i;
int t,k;
while(i>0)
{
while(j<=n/2)
{
if((2*j+1)<=n)
{
k=(a[2*j+1]>a[2*j])?(2*j+1):(
2*j);
if(a[j]<a[k])
{
t=a[j];
a[j]=a[k];
a[k]=t;
}
else break;
}
else
{
k=2*j;
if(a[j]<a[k])
{
t=a[j];
a[j]=a[k];
a[k]=t;
}
else break;
}
j=k;
}
i--;
j=i;
}
}
int main()
{
int i,n,t;
cout<<"请输入元素个数:";
cin>>n;
int*p=new int[n+1];
cout<<"请您输入数据:";
for(i=1;i<=n;i++)
cin>>p[i];
p[0]=n;
for(i=p[0];i>0;i--)
{
p[0]=i;
max(p);
t=p[1];
p[1]=p[i];
p[i]=t;
}
cout<<"最终的排序为:";
for(i=1;i<=n;i++)
cout<<p[i]<<'\t';
cout<<endl;
delete []p;
return 0;
}
8.#include<iostream>
#include<cstdlib>
#include<ctime>
#define N 40
using namespace std;
struct tree
{
int weight,parent,lchild,rchild;
};
void Select(tree*HT,int m,int&s1,int&s2)
{
int j=1,t,flag=1;
for(;j<=m;j++)
{
if(HT[j].parent==0)
{
if(flag==1)
{
s1=j;
flag++;
continue;
}
if(flag==2)
{
s2=j;
if(HT[s1].weight>HT[s2].weight)
{
t=s1;
s1=s2;
s2=t;
}
flag=0;
continue;
}
if(flag==0)
{
if(HT[j].weight<HT[s1].weight)
{
t=s1;
s1=j;
s2=t;
}
else
{
if(HT[j].weight<HT[s2].weight)
s2=j;
}
}
}
}
}
void produce(int&a,int&b,int&c,int&d,int&e,int&f)
{
srand(time(0));
int m[N];
int i;
for(i=0;i<N;i++)
m[i]=rand()%6;
for(i=0;i<N;i++)
{
switch(m[i])
{
case 0:a++;break;
case 1:b++;break;
case 2:c++;break;
case 3:d++;break;
case 4:e++;break;
case 5:f++;break;
}
}
}
void coding(tree*&HT,int w[],int n,char**&HC)
{
int m=2*n-1;
int s1,s2;
HT=new tree[m+1];
tree*p=HT;
int i=1;
for(;i<=n;i++)
{
p[i].weight=w[i-1];
p[i].parent=0;
p[i].lchild=0;
p[i].rchild=0;
}
for(i=n+1;i<=m;i++)
{
p[i].weight=0;
p[i].parent=0;
p[i].lchild=0;
p[i].rchild=0;
}
for(i=n+1;i<=m;i++)
{
Select(HT,i-1,s1,s2);
cout<<s1<<'\t'<<s2<<endl;
HT[s1].parent=i;
HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
char*cd=new char[n];
cd[n-1]='\0';
for(i=1;i<=n;i++)
{
int start=n-1;
int c=i,f=HT[i].parent;
for(;f!=0;c=f,f=HT[f].parent)
{
if(HT[f].lchild==c)
cd[--start]='0';
else
cd[--start]='1';
}
HC[i]=new char[n-start];
strcpy(HC[i],&cd[start]);
}
delete[] cd;
}
int main()
{
char p[6]={'a','b','c','d','e','f'};
tree*HT=NULL;
typedef char** HUFM;
HUFM HC=new char*[N];
int a=0,b=0,c=0,d=0,e=0,f=0;
produce(a,b,c,d,e,f);
cout<<a<<'\t'<<b<<'\t'<<c<<'\t'<<d<<'\t'<<e<<'\t'<<f<<endl;
int*w=new int[6];
int i;
for(i=0;i<6;i++)
{
switch(p[i])
{
case 'a':w[i]=a;break;
case 'b':w[i]=b;break;
case 'c':w[i]=c;break;
case 'd':w[i]=d;break;
case 'e':w[i]=e;break;
case 'f':w[i]=f;break;
}
}
coding(HT,w,6,HC);
for(i=1;i<=6;i++)
{
switch(i)
{
case 1:cout<<"a的编码为:";break;
case 2:cout<<"b的编码为:";break;
case 3:cout<<"c的编码为:";break;
case 4:cout<<"d的编码为:";break;
case 5:cout<<"e的编码为:";break;
case 6:cout<<"f的编码为:";break;
}
cout<<HC[i]<<endl;
}
delete []HT;
for(i=1;i<=6;i++)
delete HC[i];
delete []HC;
delete []w;
return 0;
}
9.#include<iostream>
#include<cstdlib>
#include<ctime>
#define N 26
using namespace std;
void max(int a[])
{
int i=N/2;
int j=i;
int t,k;
while(i>0)
{
while(j<=N/2)
{
if((2*j+1)<=N)
{
k=(a[2*j+1]>a[2*j])?(2*j+1):(2*j);
if(a[j]<a[k])
{
t=a[j];
a[j]=a[k];
a[k]=t;
}
else break;
}
else
{
k=2*j;
if(a[j]<a[k])
{
t=a[j];
a[j]=a[k];
a[k]=t;
}
else break;
}
j=k;
}
i--;
j=i;
}
}
void output(int a[])
{
int n=a[0];
int j=1;
for(int i=1;i<=n;i++)
{
int s=1;
cout<<a[i]<<'\t';
for(int k=1;k<=j;k++)
s=2*s;
if(i+1==s)
{
cout<<endl;
j++;
}
}
}
void insert(int a[])
{
int n=a[0];
int j=n;
int i,t;
while(j>1)
{
i=j/2;
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
j=i;
}
}
int main()
{
srand(time(0));
int i;
int p[28];
for(i=1;i<27;i++)
p[i]=rand()%50;
p[0]=26;
max(p);
cout<<"生成的最大堆为:"<<endl;
output(p);
cout<<endl;
p[0]=27;
p[27]=33;
insert(p);
cout<<"插入节点后的堆为:"<<endl;
output(p);
cout<<endl;
return 0;
}
10.#include<iostream>
using namespace std;
#define N 50
void getnext(char t[],int*next)
{
int i=1,j=0,n=strlen(t);
next[1]=0;
while(i<n)
{
if(j==0||t[i]==t[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
}
int KMP(char s[],char t[],int*next)
{
int i=1,j=1,n=strlen(s),m=strlen(t);
while(i<n&&j<m)
{
if(j==0||s[i]==t[j])
{
i++;
j++;
}
else j=next[j];
}
if(j>(m-1)) return i-m+1;
else return 0;
}
int main()
{
char s[50]={"5uyytcdabaabcac"},t[50]={"1abaabcac"};
int*next=new int[N];
getnext(t,next);
int n=strlen(t);
for(int i=1;i<n;i++)
cout<<next[i]<<'\t';
cout<<endl;
int x=KMP(s,t,next);
cout<<"组号是:"<<x<<endl;
return 0;
}
总结
敬请期待第三篇