课程设计终极版.docx
《课程设计终极版.docx》由会员分享,可在线阅读,更多相关《课程设计终极版.docx(20页珍藏版)》请在冰豆网上搜索。
课程设计终极版
软件基础设计报告
学院:
电气信息工程学院
班级:
电科1102班
姓名:
许波益
学号:
3110504052
实验一
在交互方式下完成下列任务:
1、建立单向链表,表长任意;
2、可交互输出单链表中的内容;
3、编写算法计算出自己所建单链表的长度并输出;
4、删除自己所建单链表中的第K个结点,并将剩余结点输出;
5、将单链表倒排,输出结果。
源代码:
#include
#include
structLinkList
{intdata;
structLinkList*next;
};
structLinkList*CreatLList()
{structLinkList*H=NULL,*p,*q;
intx;
q=NULL;
printf("输入一个数(-1结束):
");
scanf("%d",&x);
while(x!
=-1){p=(structLinkList*)malloc(sizeof(structLinkList));
p->data=x;
if(H==NULL)H=p;
elseq->next=p;
q=p;
printf("继续输入:
");
scanf("%d",&x);
}
if(q!
=NULL)q->next=NULL;
return(H);
}
voidOutputlist(structLinkList*Head)
{structLinkList*H;
H=Head;
printf("链表是:
\n");
while(H!
=NULL)
{printf("%d",H->data);
H=H->next;
}
printf("\n");
}
LengthLList(structLinkList*L)
{intLength=0;
structLinkList*p=L;
while(p!
=NULL)
{p=p->next;
Length++;
}
returnLength;
}
voidDeleteLList(structLinkList*L)
{inta;
structLinkList*p,*s;
printf("输入要删除的数:
");
scanf("%d",&a);
if(L->next==NULL)return;
p=L;s=p;
while((p->next!
=NULL)&&(p->data!
=a))
{s=p;
p=p->next;
}
if(p==NULL){printf("链表中无此数\n");return;}
s->next=p->next;
free(p);
printf("已删除\n");
return;
}
structLinkList*NIXU(structLinkList*h)
{structLinkList*p,*r,*s;
r=h;
p=r->next;
s=p->next;
if(h==NULL)printf("空链表\n");
while(s!
=NULL&&h!
=NULL)
{p->next=r;
r=p;
p=s;
s=s->next;
}
h->next=NULL;
p->next=r;
returnp;
}
voidmain()
{inta;
structLinkList*head;
while
(1)
{printf("功能:
\n");
printf("1:
建立链表\n");
printf("2:
输出链表\n");
printf("3:
计算链表长度\n");
printf("4:
删除链表结点\n");
printf("5:
链表逆序\n");
printf("6:
退出\n");
printf("输入功能:
");
scanf("%d",&a);
if(a<=6&&a>=1)
{switch(a)
{case1:
head=CreatLList();break;
case2:
Outputlist(head);break;
case3:
printf("链表长度是:
%d\n",LengthLList(head));break;
case4:
DeleteLList(head);break;
case5:
head=NIXU(head);break;
case6:
exit(0);break;
default:
break;
}
}
else{printf("错误的功能号码\n");}
}
}
实验二
在交互方式下完成下列任务:
1、动态交互建立二叉树,结点个数任意;
2、分别用DLR、LDR、LRD三种方式对二叉树进行便利并输出结果;
3、计算二叉树中的结点个数并输出;
4、计算二叉树的深度并输出;
源代码:
#include"stdio.h"
#include"malloc.h"
structBTNode
{
intdata;
structBTNode*Lchild,*Rchild;
};
structBTNode*build(structBTNode*p);
structBTNode*creatrent(structBTNode*p);
voidDLR(structBTNode*T);
structBTNode*creatrent(structBTNode*p)
{
intx;
printf("输入根:
rent=");
scanf("%d",&x);
if(x==1000){p=NULL;}
else
{
p->data=x;
p->Lchild=(structBTNode*)malloc(sizeof(structBTNode));
p->Rchild=(structBTNode*)malloc(sizeof(structBTNode));
if(p==NULL)returnp;
p->Lchild=build(p->Lchild);
p->Rchild=build(p->Rchild);
returnp;
}
}
structBTNode*build(structBTNode*p)
{
intx;
printf("输入数据(输入值为时999,表示该结点为空):
value=");
scanf("%d",&x);
if(x==999){p=NULL;}
else
{
p->data=x;
p->Lchild=(structBTNode*)malloc(sizeof(structBTNode));
p->Rchild=(structBTNode*)malloc(sizeof(structBTNode));
}
if(p==NULL)returnp;
p->Lchild=build(p->Lchild);
p->Rchild=build(p->Rchild);
returnp;
}
voidDLR(structBTNode*T)
{
if(T==NULL)return;
printf("%d",T->data);
DLR(T->Lchild);
DLR(T->Rchild);
}
voidLDR(structBTNode*T)
{
if(T==NULL)return;
LDR(T->Lchild);
printf("%d",T->data);
LDR(T->Rchild);
}
voidLRD(structBTNode*T)
{
if(T==NULL)return;
LRD(T->Lchild);
LRD(T->Rchild);
printf("%d",T->data);
}
voidmain()
{
structBTNode*rent=NULL;
intflag;
while
(1)
{
printf("选择输入的操作:
\n1:
创建;\n2:
先序;\n3:
中序;\n4:
后序\n");
scanf("%d",&flag);
switch(flag)
{
case1:
rent=(structBTNode*)malloc(sizeof(structBTNode));
rent=creatrent(rent);
break;
case2:
DLR(rent);printf("\n");break;
case3:
LDR(rent);printf("\n");break;
case4:
LRD(rent);printf("\n");break;
}
}
}
实验三
在交互方式下完成下列任务:
1、根据教材上算法,完成图的深度和广度优先遍历,要求任意给定起始点,输出结果;
2、根据教材上算法,完成图的单源最短路径的算法,要求任意给定源点,输出结果;
源代码:
#include
#include
#defineK1000
#defineVNum6
structGLink
{intNo;
intRight;
structGLink*Relat;
};
intG[VNum][VNum]=//初始化//
{
0,50,10,K,45,K,
K,0,15,50,10,K,
20,K,0,15,K,K,
K,20,K,0,35,K,
K,K,K,30,0,K,
K,K,K,3,K,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]=K;
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=K;
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;//a为功能号,i用作循环,j,b为函数参数
CreateGLink(G);
printf("1.深度查找\n");//功能选择//
printf("2.广度查找\n");
printf("3.最短路径\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、任意给定无序系列,用快速排序法对其进行排序,并统计交换次数。
2、任意给定的无序序列,用对半检索法交互检索任意给定的关键字KEY
3、任意给定无序系列,用冒泡排序法对其进行排序,并统计交换次数和排序的趟数
源代码:
#include
intji;
voidhalfhalf(inta[8],intn,intkey)//对半检索
{intlow,high,mid,flag;
low=0;high=n-1;flag=0;
while(low<=high)
{mid=(low+high)/2;
if(a[mid]==key){flag=1;break;}
elseif(key>a[mid])low=mid+1;
elsehigh=mid-1;
}
if(flag==1)printf("是第%d个元素",mid+1);
elseprintf("没有此数?
!
");
}
voidpop(inta[8],intn)//冒泡检索
{inti,j,temp,flag;
intjiao,tang;
jiao=tang=0;
for(i=0;i{flag=1;
for(j=0;jif(a[j+1]tang++;
if(flag==1)break;
}
printf("\n交换次数和排序的趟数分别是%d和%d",jiao,tang);
}
voidquick(inta[8],intlow,inthigh)//快速检索
{inti,j;
inttemp;
if(low>=high)return(0);
i=low;j=high;
temp=a[i];
while(i!
=j)
{while((a[j]>=temp)&&(j>i))j--;
if(j>i){a[i++]=a[j];ji++;}
while((a[i]<=temp)&&(j>i))i++;
if(j>i){a[j--]=a[i];ji++;}
}
a[i]=temp;
quick(a,low,i-1);
quick(a,i+1,high);
}
main()
{
inti,a[8],n,q,p,t;
do{printf("\n1.对半检索检索关键字KEY");
printf("\n2.快速排序法排序,统计交换次数");
printf("\n3.冒泡排序法排序,统计交换次数");
printf("\n4.退出");
printf("\n请输入功能号:
");
scanf("%d",&i);
while(i<1||i>4)
{printf("\nTheorderyouprintiswrong!
pleaseprintagain!
");
scanf("%d",&i);
}
switch(i)
{case1:
printf("输入数据\n");
for(n=0;n<8;n++)scanf("%d",&a[n]);
do{printf("输入检索关键字\n");
scanf("%d",&q);
halfhalf(a,8,q);
printf("\n输入1则继续检索:
\n");
scanf("%d",&p);
}while(p==1);break;
case2:
i=0;
printf("输入数据\n");
for(n=0;n<8;n++)scanf("%d",&a[n]);
quick(a,0,7);
printf("\快速排序交换次数是",i);
printf("\n排序结果是");
for(n=0;n<8;n++)printf("%d",a[n]);break;
case3:
printf("输入数据\n");
for(n=0;n<8;n++)scanf("%d",&a[n]);
pop(a,8);
printf("\n结果是");
for(n=0;n<8;n++)printf("%d",a[n]);
break;
case4:
printf("再见");
}
}while(i!
=4);
}