=NULL)
{
i++;
p2=p1;
p1=p1->next;
}
p2->next=p1->next;
free(p1);
}
}
linklist*Nixu(linklist*h)//逆序输出链表//
{linklist*r,*q,*p;
r=h;
p=r->next;
q=p->next;
if(h==NULL)
printf("thelinklistisempty\n");//空表//
while(q!
=NULL&&h!
=NULL)
{p->next=r;
r=p;
p=q;
q=q->next;
}
h->next=NULL;
p->next=r;
return(p);//返回根结点//
}
main()
{intk,x;
linklist*h;
do//输出菜单//
{printf("\nqingshurumingling:
\n");
printf("1.jianlilianbiao;\n");
printf("2.shuchulianbiaozhongdeneirong;\n");
printf("3.shuchulianbiaodechangdu;\n");
printf("4.shanchudiKgejiedian;\n");
printf("5.jianglianbiaodaoxubingshuchu;\n");
printf("6.tuichuchengxu;\n");
printf("qingshuru1--6deshuzi:
\n");
scanf("%d",&x);
if(x<1||x>6)printf("error!
\n");
else
switch(x)
{case1:
h=Creatlist();break;
case2:
Putlist(h);break;
case3:
printf("lianbiaodechangdushi%d",Long(h));break;
case4:
printf("Inputthenodeyouwanttodelete:
\n");
scanf("%d",&k);
Delete(h,k);Putlist(h);break;
case5:
h=Nixu(h);Putlist(h);break;
case6:
exit(0);break;//退出程序//
}
}
while
(1);
}
退出程序;
上机题二:
二叉树
1.动态交互建立二叉树,结点个数任意;
2.分别用DLR,LDR,LRD三种方式对二叉树进行遍历并输出结果;
3.计算二叉树中结点个数并输出;
4.计算二叉树深度并输出
源程序:
#include"stdio.h"
#include"malloc.h"
structTreeNode
{intdata;
structTreeNode*Lchild;
structTreeNode*Rchild;
};
structTreeNode*create()//用于建立二叉树的子函数//
{structTreeNode*T;
inta;
scanf("%d",&a);
if(a==0)returnNULL;
else//动态建立二叉树//
{T=(structTreeNode*)malloc(sizeof(structTreeNode));
T->data=a;//建立根结点//
T->Lchild=create();//递归建立左子树//
T->Rchild=create();//递归建立右子树//
}
return(T);
}
voidPre(structTreeNode*T)//用于进行先序遍历的子函数//
{if(T!
=NULL)
{printf("%5d",T->data);//访问根结点//
Pre(T->Lchild);//递归访问左子树//
Pre(T->Rchild);//递归访问右子树//
}
}
voidMid(structTreeNode*T)//用于进行中序遍历的子函数//
{
if(T!
=NULL)
{Mid(T->Lchild);//递归访问左子树//
printf("%5d",T->data);//访问根结点//
Mid(T->Rchild);//递归访问右子树//
}
}
voidPost(structTreeNode*T)//用于进行后序遍历的子函数//
{if(T!
=NULL)
{Post(T->Lchild);//递归访问左子树//
Post(T->Rchild);//递归访问右子树//
printf("%5d",T->data);
}
}
voidvisit(structTreeNode*T)//用于访问二叉树的子函数//
{printf("theresultofDLR:
");Pre(T);
printf("\n");
printf("theresultofLDR:
");Mid(T);
printf("\n");
printf("theresultofLRD:
");Post(T);
printf("\n");
}
intleaf(structTreeNode*T)//用于计算叶子结点的子函数//
{inta,b;
if(T==NULL)return(0);
elseif(T->Lchild==NULL&&T->Rchild==NULL)//判断该结点是叶子结点//
return
(1);
else
{a=leaf(T->Lchild);//递归计算左子树上叶子数//
b=leaf(T->Rchild);//递归计算右子树上叶子数//
return(a+b+1);
}
}
intmax(intx,inty)//用于取两数的较大值的子函数//
{if(x>y)return(x);
elsereturn(y);
}
intdeep(structTreeNode*T)//用于计算二叉树深度的子函数//
{intk=0;
if(T==NULL)return(0);
elseif(T->Lchild==NULL&&T->Rchild==NULL)//该结点有孩子,深度增加1//
return
(1);
elsereturn(1+max(deep(T->Lchild),deep(T->Rchild)));//递归求取树的深度//
}
main()
{intm,n,p;
structTreeNode*T;
printf("createatree\n");
T=create();//建立二叉树//
printf("1.visitthetree\n");//打印菜单//
printf("2.putoutthetotalnumberofnode\n");
printf("3.putoutthedepthofthetree\n");
printf("4.exit\n");
while
(1)
{printf("\npleaseinput1-4:
");//完成菜单的相应功能//
scanf("%d",&m);
if(m==1)visit(T);
if(m==2){n=leaf(T);printf("thetotalnumberofleavesinthetreeis%d\n",n);}
if(m==3)if(m==3){p=deep(T);printf("thedepthofthetreeis%d\n",p);}
if(m==4)break;
}
}
调试结果:
createatree
102504068960005685006900025000
1.visitthetree
2.putoutthetotalnumberofnode
3.putoutthedepthofthetree
4.exit
pleaseinput1-4:
thetotalnumberofleavesinthetreeis10
pleaseinput1-4:
pleaseinput1-4:
pleaseinput1-4:
pleaseinput1-4:
pleaseinput1-4:
1
theresultofDLR:
12546896568569
theresultofLDR:
15496868556692
theresultofLRD:
96885695664521
pleaseinput1-4:
2
thetotalnumberofleavesinthetreeis10
pleaseinput1-4:
3
thedepthofthetreeis7
pleaseinput1-4:
4
Pressanykeytocontinue
上机题三图
在交互方式下完成下列任务:
1、根据教材上算法,完成图的深度和广度优先遍历,要求任意给定起始点,输出结果;
2、根据教材上算法,完成图的单源最短路径的算法,要求任意给定源点,输出结果
程序:
#include
#include
#defineM1000
#defineVNum6
structGLink
{intNo;
intRight;
structGLink*Relat;
};
intG[VNum][VNum]=1//对图进行初始化//
{1,31,11,M,41,M,
M,0,15,50,10,M,
13,M,0,15,M,M,
M,13,M,0,17,M,
M,M,M,26,0,M,
M,M,M,3,M,0};
structGLink*GL[VNum];
intVisited[VNum];
voidCreateGLink(intG[VNum][VNum])//建立邻接表//
{inti,j;
structGLink*p,*q;
for(i=0;i{GL[i]=q=NULL;
for(j=0;j{if(i!
=j)
if((G[i][j]>0)&&(G[i][j]{p=(structGLink*)malloc(sizeof(structGLink));
p->No=j;//将该点加入邻接表//
p->Right=G[i][j];
if(GL[i]==NULL)
GL[i]=p;
else
q->Relat=p;
q=p;
}
}
}
}
voidDFS(intA[VNum][VNum],intV)//用于进行深度优先遍历的子函数,V是起点//
{inti;
printf("[%d]",V);
Visited[V]=1;//将其标记为已访问//
for(i=0;iif((A[V][i]>0)&&(A[V][i]=1))//该结点未被访问过//
DFS(A,i);//访问该点//
for(i=0;iif(Visited[i]!
=1)DFS(A,i);//仍有未必访问过的点,访问该点//
}
voidBFS(intA[VNum][VNum],intV)//用于广度优先遍历的子函数//
{intCQ[VNum];
inta=0,b,c;
inti,k=1;
for(i=0;iCQ[i]=M;
Visited[V]=1;//标志为访问过//
CQ[0]=V;
printf("[%d]",V);//将该结点放入队列//
while(k<6&&a{b=CQ[a];
for(c=0;cif(Visited[c]==0&&A[b][c]=0)
{printf("[%d]",c);
CQ[++k]=c;//未被访问过,将其插入到队列中//
Visited[c]=1;//标志为访问过//
}
a++;
}
for(i=0;iif(Visited[i]==0)
BFS(A,i);
}
voidShort(intA[VNum][VNum],intV)//用于计算最短路径的子函数,V是起点//
{intDist[VNum],Path[VNum];
intS=0;
inti,k;
intwm,u;
for(i=0;i{Dist[i]=A[V][i];//默认这两点之间即为最短路径//
if(Dist[i]}
S=S|(1<for(k=0;k{wm=M;
u=V;
for(i=0;iif(((S&(1<
{u=i;
wm=Dist[i];
}
S=S|(1<
for(i=0;iif(((S&(1<
{Dist[i]=Dist[u]+A[u][i];//找到新的最短路径//
Path[i]=u;//更新路径长度//
}
}
for(i=0;iif((S&(1<
=0)
{k=i;
while(k!
=V)
{printf("%d<-",k);
k=Path[k];
}
printf("%d",V);
printf("=%d\n",Dist[i]);
}
elseprintf("NoPath:
%d",i);
}
main()
{inti,j,a,b;
CreateGLink(G);
printf("1.searchthegraphdeepfirst\n");//打印菜单//
printf("2.searchthegraphbroadfirst\n");
printf("3.findtheshortestpath\n");
printf("4.exit\n");
while
(1)//完成菜单功能//
{printf("\npleaseinputanumfrom1to4:
");
scanf("%d",&a);
if(a==1)
{for(i=0;iVisited[i]=0;
printf("pleaseinputthefirstnode:
");
scanf("%d",&j);
printf("\nTheResultofDFSis:
");
DFS(G,j);
printf("\n");
}
if(a==2)
{for(i=0;iVisited[i]=0;
printf("pleaseinputthefirstnode:
");
scanf("%d",&j);
printf("\nTheResultofBFSis:
");
BFS(G,j);
printf("\n");
}
if(a==3)
{printf("pleaseinputthesourcenode:
");
scanf("%d",&b);
printf("\nTheResultofShortestpathis:
\n");
Short(G,b);
}
if(a==4)break;
}
}
结果调试截图:
上机题四
检索和排序
在交互方式下完成下列任务:
1、任意给定无序序列,用对半检索法,交互检索任意给定的关键字KEY;
2、任意给定无序序列,用快速排序法对进行排序,并统计交换次数;
3、任意给定无序序列,用冒泡排序法对进行排序,并统计交换次数和排序的趟数;
程序:
#include
#defineM100
structRedType
{intkey;
intother;
};
inta;
intSearch(structRedTypeST[],intn,intkey)//用于进行对半查找的子函数//
{intlow,high,mid;
low=0;high=n-1;
while(low<=high)//未检索完毕//
{mid=(low+high)/2;//指向中间值//
if(ST[mid].key==key)return(mid+1);//检索成功//
elseif(keyelselow=mid+1;//待查找值大于中间值,到后一半查找//
}
return(0);
}
intQuickSort(structRedTypeL[],intlow,inthigh)//用于快速排序的子函数//
{inti,j,t=0;
structRedTypetemp;
if(low>=high)//待排序队列空,结束//
return(0);
i=low;
j=high;
temp=L[i];
while(i{while((L[j].key>=temp.key)&&(j>i))//从后向前找出第一个小于关键值的数据//
j--;
if(iwhile((L[i].key<=temp.key)&&(j>i))//从前向后找出第一个大于关键值的数据//
i++;
if(i}
L[i]=temp;
printf("