江苏大学计算机软件技术基础上机编程.docx
《江苏大学计算机软件技术基础上机编程.docx》由会员分享,可在线阅读,更多相关《江苏大学计算机软件技术基础上机编程.docx(18页珍藏版)》请在冰豆网上搜索。
![江苏大学计算机软件技术基础上机编程.docx](https://file1.bdocx.com/fileroot1/2023-1/29/35977a3f-cfe9-4589-9880-1161a902d2d1/35977a3f-cfe9-4589-9880-1161a902d2d11.gif)
江苏大学计算机软件技术基础上机编程
计算机软件技术基础上机编程
上机题一:
线性表
1.建立单向链表;表长任意;
2.可交互输出单链表中的内容;
3.编写算法计算出自己所建单链表的长度并输出;
4.输出自己所建立单链表中的第K个结点,并将剩余结点输出;
5.将单链表倒排并输出结果
源程序:
#include
#include
typedefintdatatype;
typedefstructnode
{datatypedata;
structnode*next;
}linklist;
linklist*Creatlist()
{intx;
linklist*h,*s;
h=NULL;
printf("\npleaseinputthedateendwith0:
\n");
printf("\nInputdata:
");
scanf("%d",&x);
while(x!
=0)
{s=malloc(sizeof(linklist));
s->data=x;
s->next=h;
h=s;
printf("\nInputdata:
");
scanf("%d",&x);
}
returnh;
}
voidPutlist(linklist*h)
{linklist*s;
s=h;
while(s!
=NULL)
{printf("%4d",s->data);
s=s->next;
}
}
intLong(linklist*h)
{inti=0;
linklist*s;
s=h;
while(s!
=NULL)
{i++;
s=s->next;
}
return(i);
}
voidDelete(linklist*h,intk)
{inti=0;
linklist*p1,*p2;
p1=h;
if(k==1){h=h->next;free(p1);}
else
{
while(i=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)
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;
}
}
上机题三图
在交互方式下完成下列任务:
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)
{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)
{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("\n\nTheQukSortLoop[%d]is:
",i);
for(j=0;jprintf("%d",L[j].key);
return(t+QuickSort(L,low,i-1)+QuickSort(L,i+1,high));
}
voidbubsort(structRedTypeL[],intn)
{inti,j=0,m,fag=1,t=0;
structRedTypex;
while((j0))
{fag=0;
for(i=0;iif(L[i+1].key{fag++;
x=L[i];
L[i]=L[i+1];
L[i+1]=x;
t++;
}
if(fag)
{j++;
for(m=0;mprintf("%5d",L[m].key);
printf("\n\n");
}
}
printf("thesortedarrayis:
");
for(m=0;mprintf("%5d",L[m].key);
printf("\n");
printf("\nthetimesofsortis:
%d",j);
printf("\nthetotaltimesofexchangeis:
%d\n",t);
}
main()
{intb,m,n,i,j;
structRedTypeS[M],T[M];
printf("inputthelengthofthedata:
");
scanf("%d",&a);
printf("pleaseinputthedata\n");
for(i=0;iscanf("%d",&S[i].key);
printf("\n1.quicksort\n");
printf("2.bubsort\n");
printf("3.searchthedatayouwanttosee\n");
printf("4.exit\n");
while
(1)
{printf("\npleaseinputanumberfrom1to4:
");
scanf("%d",&b);
if(b==1){for(i=0;iT[i].key=S[i].key;
j=QuickSort(T,0,a-1);
printf("\nthetotaltimesofexchangeis:
%d\n",j);
}
if(b==2){for(i=0;iT[i].key=S[i].key;
bubsort(T,a);
}
if(b==3){printf("pleaseinputthethekeyvalue:
");
scanf("%d",&m);
n=Search(T,a,m);
if(n==0)printf("can'tfindthekeyvalue\n");
elseprintf("thelocationofthekeyvalueis:
%d\n",n);
}
if(b==4)break;
}
}