1、江苏大学计算机软件技术基础上机编程计算机软件技术基础上机编程上机题一:线性表1. 建立单向链表;表长任意;2. 可交互输出单链表中的内容;3. 编写算法计算出自己所建单链表的长度并输出;4. 输出自己所建立单链表中的第K个结点,并将剩余结点输出;5. 将单链表倒排并输出结果源程序:#include#includetypedef int datatype;typedef struct node datatype data; struct node *next;linklist; linklist *Creatlist() int x; linklist *h, *s; h=NULL; print
2、f(n please input the date end with 0:n); printf(n Input data:); scanf(%d,&x); while(x!=0) s=malloc(sizeof(linklist); s-data=x; s-next=h; h=s; printf(n Input data:); scanf(%d,&x); return h; void Putlist(linklist *h) linklist *s; s=h; while(s!=NULL) printf(%4d,s-data); s=s-next; int Long(linklist *h)
3、int i=0; linklist *s; s=h; while(s!=NULL) i+; s=s-next; return(i); void Delete(linklist *h,int k) int i=0; linklist *p1,*p2; p1=h; if(k=1) h=h-next;free(p1); else while(inext; 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(the lin
4、klist is emptyn); while(q!=NULL&h!=NULL) p-next=r; r=p; p=q; q=q-next; h-next=NULL; p-next=r; return(p); main() int k,x; linklist *h; do printf(nqing shu ru ming ling:n); printf(1.jian li lian biao;n); printf(2.shu chu lian biao zhong de nei rong;n); printf(3.shu chu lian biao de chang du;n); printf
5、(4.shan chu di K ge jie dian;n); printf(5.jiang lian biao dao xu bing shu chu;n); printf(6.tui chu cheng xu;n); printf(qing shu ru 1-6 de shu zi:n); scanf(%d,&x); if(x6) printf(error!n); else switch(x) case 1:h=Creatlist();break; case 2:Putlist(h);break; case 3:printf(lian biao de chang du shi %d,Lo
6、ng(h);break; case 4:printf(Input the node you want to delete:n); scanf(%d,&k); Delete(h,k);Putlist(h);break; case 5:h=Nixu(h);Putlist(h);break; case 6:exit(0);break; while(1);上机题二:二叉树1. 动态交互建立二叉树,结点个数任意;2. 分别用DLR,LDR,LRD三种方式对二叉树进行遍历并输出结果;3. 计算二叉树中结点个数并输出;4. 计算二叉树深度并输出源程序:#include stdio.h#include mal
7、loc.hstruct TreeNode int data; struct TreeNode *Lchild ; struct TreeNode *Rchild;struct TreeNode *create( ) struct TreeNode *T; int a; scanf(%d,&a); if (a=0) return NULL; else T=(struct TreeNode *)malloc(sizeof(struct TreeNode); T-data=a; T-Lchild=create( ); T-Rchild=create( ); return (T);void Pre (
8、struct TreeNode *T) if (T!= NULL) printf (%5d,T-data); Pre (T-Lchild); Pre (T-Rchild); void Mid(struct TreeNode *T) if ( T != NULL) Mid (T-Lchild); printf(%5d,T-data); Mid (T-Rchild); void Post (struct TreeNode *T) if ( T != NULL) Post (T-Lchild); Post (T-Rchild); printf(%5d,T-data); void visit(stru
9、ct TreeNode *T) printf(the result of DLR:); Pre(T); printf(n); printf(the result of LDR:); Mid(T); printf(n); printf(the result of LRD:); Post(T); printf(n);int leaf(struct TreeNode *T) int a,b; if(T=NULL) return (0); else if(T-Lchild=NULL&T-Rchild=NULL) return (1); else a=leaf(T-Lchild); b=leaf(T-R
10、child); return (a+b+1); int max(int x,int y) if(xy) return (x); else return (y);int deep(struct TreeNode *T) int k=0; if(T=NULL) return (0); else if(T-Lchild=NULL&T-Rchild=NULL) return (1); else return (1+max(deep(T-Lchild),deep(T-Rchild); main() int m,n,p; struct TreeNode *T; printf(create a treen)
11、; T=create(); printf(1.visit the treen); printf(2.putout the total number of noden); printf(3.putout the depth of the treen); printf(4.exitn); while(1) printf(nplease input 1-4: ); scanf(%d,&m); if(m=1) visit(T); if(m=2) n=leaf(T); printf(the total number of leaves in the tree is %dn,n); if(m=3) if(
12、m=3) p=deep(T); printf(the depth of the tree is %dn,p); if(m=4) break; 上机题三 图在交互方式下完成下列任务:1、根据教材上算法,完成图的深度和广度优先遍历,要求任意给定起始点,输出结果;2、根据教材上算法,完成图的单源最短路径的算法,要求任意给定源点,输出结果程序:#include #include #define M 1000#define VNum 6struct GLink int No; int Right; struct GLink *Relat; ;int GVNumVNum = 1 1 , 31, 11, M
13、, 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 ;struct GLink *GLVNum;int VisitedVNum;void CreateGLink(int GVNumVNum) int i,j; struct GLink *p,*q; for (i=0; iVNum; i+) GLi = q = NULL; for (j=0; j 0) & (Gij No = j; p-Right = Gij; if (GLi = NUL
14、L) GLi = p; else q-Relat = p; q = p; void DFS(int AVNumVNum, int V) int i; printf( %d , V); VisitedV = 1; for (i = 0; i 0) & (AVi M) & (Visitedi != 1) DFS(A,i); for (i = 0; i VNum; i+) if (Visitedi != 1) DFS(A,i); void BFS(int AVNumVNum, int V) int CQVNum; int a=0,b,c; int i,k=1; for (i=0;iVNum;i+)
15、CQi=M; VisitedV = 1; CQ0=V; printf(%d ,V); while(k6&ak) b=CQa; for(c=0;cVNum;c+) if(Visitedc=0&AbcM&Abc!=0) printf(%d , c); CQ+k=c; Visitedc=1; a+; for(i=0;iVNum;i+) if(Visitedi=0) BFS(A,i);void Short(int AVNumVNum,int V) int DistVNum, PathVNum; int S = 0; int i, k; int wm, u; for (i=0; iVNum; i+) D
16、isti = AVi; if (Disti M) Pathi = V; S = S | (1 V); for (k=0; kVNum; k+) wm = M; u = V; for (i=0; iVNum; i+) if (S & (1 i)=0) & (Disti wm) u = i; wm = Disti; S = S | (1 u); for (i=0; iVNum; i+) if (S & (1 i)=0) & (Distu + Aui) Disti) Disti = Distu + Aui; Pathi = u; for (i=0; iVNum; i+) if (S & (1 i)
17、!= 0) k = i; while ( k != V) printf( %d - , k); k = Pathk; printf( %d , V); printf( = %d n, Disti); else printf( No Path : %d,i);main() int i,j,a,b; CreateGLink(G); printf(1.search the graph deep firstn); printf(2.search the graph broad firstn); printf(3.find the shortest pathn); printf(4.exitn); wh
18、ile(1) printf(n please input a num from 1 to 4 : ); scanf(%d,&a); if(a=1) for (i=0; iVNum; i+) Visitedi = 0; printf(please input the first node: ); scanf(%d,&j); printf(n The Result of DFS is:); DFS(G,j); printf(n); if(a=2) for (i=0; iVNum; i+) Visitedi = 0; printf(please input the first node: ); sc
19、anf(%d,&j); printf(n The Result of BFS is:); BFS(G,j); printf(n); if(a=3) printf(please input the source node : ); scanf(%d,&b); printf(n The Result of Shortest path is:n); Short(G,b); if(a=4) break; 上机题四 检索和排序在交互方式下完成下列任务:1、任意给定无序序列,用对半检索法,交互检索任意给定的关键字KEY;2、任意给定无序序列,用快速排序法对进行排序,并统计交换次数;3、任意给定无序序列,用
20、冒泡排序法对进行排序,并统计交换次数和排序的趟数; 源程序:#include #define M 100struct RedType int key; int other; ;int a;int Search ( struct RedType ST, int n, int key) int low, high, mid; low=0; high=n-1; while( low = high ) mid=(low+high)/2; if ( STmid.key = key) return(mid+1); else if( key = high) return (0); i = low; j =
21、high; temp = Li; while(i = temp.key) & (j i) j -; if (i j) Li = Lj; t+; while(Li.key i) i +; if (i j) Lj = Li; t+; Li = temp; printf(nnThe QukSort Loop%d is : ,i); for (j = 0; j a; j+) printf(%d , Lj.key); return(t+QuickSort(L, low, i-1)+QuickSort(L, i+1, high); void bubsort(struct RedType L , int n
22、) int i,j=0,m, fag=1,t=0; struct RedType x; while ( (j 0) ) fag=0; for ( i=0; i n-1; i+) if ( Li+1.key Li.key ) fag+; x=Li; Li=Li+1; Li+1=x; t+; if(fag) j+; for(m=0;mn;m+) printf(%5d,Lm.key); printf(nn); printf(the sorted array is: ); for(m=0;mn;m+) printf(%5d,Lm.key); printf(n); printf(nthe times o
23、f sort is: %d,j); printf(nthe total times of exchange is : %dn,t); main() int b,m,n,i,j; struct RedType SM,TM; printf(input the length of the data:); scanf(%d,&a); printf(please input the data n); for(i=0;ia;i+) scanf(%d,&Si.key); printf(n1.quick sortn); printf(2.bub sortn); printf(3.search the data
24、 you want to seen); printf(4.exitn); while(1) printf(nplease input a number from 1 to 4: ); scanf(%d,&b); if(b=1) for(i=0;ia;i+) Ti.key=Si.key; j=QuickSort( T, 0, a-1); printf(nthe total times of exchange is :%dn,j); if(b=2) for(i=0;ia;i+) Ti.key=Si.key; bubsort(T,a); if(b=3) printf(please input the the key value: ); scanf(%d,&m); n=Search(T,a,m); if(n=0) printf(cant find the key valuen); else printf(the location of the key value is: %dn, n); if(b=4) break;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1