沈陽計算機技術所復試試題及答案_第1頁
沈陽計算機技術所復試試題及答案_第2頁
沈陽計算機技術所復試試題及答案_第3頁
沈陽計算機技術所復試試題及答案_第4頁
沈陽計算機技術所復試試題及答案_第5頁
已閱讀5頁,還剩30頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

目錄復試試題 12006 12007 32008 52009 8C語言課本 11棧 14鏈表 15樹 18樹的遞歸遍歷 18樹的非遞歸遍歷 19圖 24圖的遍歷 24圖的拓撲排序 28//stack.h頭文件 28//stack.h頭文件 29//main.cpp文件 31復試試題2006//2006-1輸入一行字符,統(tǒng)計各種字符的個數(shù)#include<stdio.h>intmain(){ intletter=0,blank=0,number=0,other=0; charstr[40],*p; puts("請輸入一行字符\n"); gets(str); p=str; for(;*p!='\0';p++) { if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')) { letter++; } elseif(*p>='0'&&*p<='9') { number++; } elseif(*p=='') { blank++; } else{ other++; } } printf("字母個數(shù):%d\n",letter); printf("數(shù)字個數(shù):%d\n",number); printf("空格個數(shù):%d\n",blank); printf("其它字符個數(shù):%d\n",other); return0;}/*請輸入一行字符minifocusflowhaha***0)))__++++字母個數(shù):數(shù)字個數(shù):空格個數(shù):其它字符個數(shù):請按任意鍵繼續(xù)...*///2006-2輸入N個數(shù),使前面各個數(shù)順序后移M個位置,最后M個數(shù)變成最前面的M個數(shù)字#include<stdio.h>intmain(){ intn,m=3,*p,j,i; inta[9]; for(i=0;i<9;i++) { a[i]=i+1; printf("%d\t",a[i]); } printf("\n"); for(i=0;i<m;i++) { n=a[0]; for(j=1;j<9;j++) { a[j-1]=a[j]; } a[8]=n; } for(i=0;i<9;i++) { printf("%d\t",a[i]); } printf("\n"); return0;}/*123456789456789123請按任意鍵繼續(xù)...*/2007//2007-1輸入數(shù)組,最大的一個與第一個元素交換,最小的與最后一個元素交換#include<stdio.h>intmain(){ voidswap(int*p,int*q); inta[10],min=0,max=0,i,*p; puts("請輸入十個整數(shù):\n"); for(i=0;i<10;i++) { scanf("%d",&a[i]); } printf("\n"); for(i=0;i<10;i++) { printf("%d\t",a[i]); } for(i=1;i<10;i++) { if(a[i]>a[max]) { max=i; }elseif(a[i]<a[min]) { min=i; } } if(max!=0)swap(&a[max],a); if(min!=9)swap(&a[min],&a[9]); puts("修改后的字符串為:"); for(i=0;i<10;i++) { printf("%d\t",a[i]); } return0;}voidswap(int*p,int*q){ inttemp; temp=*p; *p=*q; *q=temp;}/*請輸入十個整數(shù):10202521525201020252152520修改后的字符串為:2010252205251請按任意鍵繼續(xù)...*///2007-2實數(shù)轉化為小數(shù)和整數(shù)#include<stdio.h>intmain(){ floata=0; intb=0; puts("請輸入一個實數(shù)\n"); scanf("%f",&a); b=(int)a; a=a-b; puts("轉換后整數(shù)與小數(shù)分別為:\n"); printf("整數(shù):%d,小數(shù):%f",b,a); return0;}/*請輸入一個實數(shù)20.389轉換后整數(shù)與小數(shù)分別為:整數(shù):,小數(shù):.389000請按任意鍵繼續(xù)...*/2008//2008-1五個學生,每人三門成績,鍵盤輸入學號,姓名,成績,///計算出平均成績,并將學生信息儲存在磁盤文件“stud”中#include<stdio.h>structStudent{ charsno[10]; charsname[6]; floatscore[3]; floataver;};intmain(){ voidinInfo(structStudent*p); voidoutInfo(structStudent*p); inti; structStudents[2]; inInfo(s); outInfo(s); return0;}/*請輸入學號:2009270032請輸入姓名:zhangsan請輸入三門課程成績:809085請輸入學號:2009270025請輸入姓名:lisi請輸入三門課程成績:208596學號:2009270032zhangs姓名:zhangs三門課程成績?yōu)?80.000000,90.000000,85.000000三門課程平均成績?yōu)?85.000000學號:2009270025lisi姓名:lisi三門課程成績?yōu)?20.000000,85.000000,96.000000三門課程平均成績?yōu)?67.000000請按任意鍵繼續(xù)...*/voidinInfo(structStudent*p){ inti; for(i=0;i<2;i++) { puts("請輸入學號:"); scanf("%s",(*p).sno); puts("請輸入姓名:"); scanf("%s",(*p).sname); puts("請輸入三門課程成績:"); scanf("%f%f%f",&((*p).score[0]),&((*p).score[1]),&((*p).score[2])); (*p).aver=((*p).score[0]+(*p).score[1]+(*p).score[2])/3.0; p++; }}voidoutInfo(structStudent*p){ inti; for(i=0;i<2;i++) { puts("學號:"); printf("%s",(*p).sno); printf("\n"); puts("姓名:"); printf("%s",(*p).sname); printf("\n"); puts("三門課程成績?yōu)?"); printf("%f,%f,%f",((*p).score[0]),((*p).score[1]),((*p).score[2])); printf("\n"); puts("三門課程平均成績?yōu)?"); printf("%f",(*p).aver); printf("\n"); p++; }}//2008-2復制字符串#include<stdio.h>intmain(){ charstr[20],newStr[20]; intstartNum=0; voidcopyStr(char*p,char*q,intnum); puts("請輸入一串少于個字符的字符串\n"); gets(str); puts("請輸入從第幾個字符開始復制:"); scanf("%d",&startNum); copyStr(str,newStr,startNum); puts("復制后新的字符串為:"); char*p=newStr; while(*p!='\0'){ printf("%c",*p++); } return0;}/*請輸入一串少于個字符的字符串hndadfkjjlk2(*)請輸入從第幾個字符開始復制:5復制后新的字符串為:fkjjlk2(*)請按任意鍵繼續(xù)...*/voidcopyStr(char*p,char*q,intstartNum){ p+=startNum; while(*p!='\0') { *q++=*p++; } *q='\0';}2009//10.1向磁盤存取信息#include<stdio.h>#include<stdlib.h>intmain(){ FILE*fp; charch,filename[10]; printf("請先行輸入文件名:"); scanf("%s",filename); if((fp=fopen(filename,"w"))==NULL) { printf("文件無法打開\n"); exit(0); } ch=getchar(); printf("請輸入一個準備存儲到磁盤的字符串(以#結束):"); while(ch!='#') { fputc(ch,fp); putchar(ch); ch=getchar(); } fclose(fp); putchar(10); return0;}/*請先行輸入文件名:mini.txt請輸入一個準備存儲到磁盤的字符串(以#結束):woaini,jiuxianglaoshuaidami!#woaini,jiuxianglaoshuaidami!請按任意鍵繼續(xù)...*///10.2由一個已知文件向一個新建文件復制信息#include<stdio.h>#include<stdlib.h>intmain(){ FILE*fp1,*fp2; charch,filename1[10],filename2[10]; puts("請輸入一個文件名:"); scanf("%s",filename1); puts("請輸入另一個文件名:"); scanf("%s",filename2); fp1=fopen(filename1,"r"); fp2=fopen(filename2,"w"); if(fp1==NULL) { printf("打開文件失敗\n"); exit(0); } if(fp2==NULL) { printf("打開文件失敗\n"); exit(0); } while(!feof(fp1)) { ch=fgetc(fp1); fputc(ch,fp2); putchar(ch); } fclose(fp1); fclose(fp2); return0; }/*請輸入一個文件名:leinuo.txt請輸入另一個文件名:afei.txthndxztf13hndxztfkdjfls6545646請按任意鍵繼續(xù)...*///10.3向一個文件中寫三個字符串請輸入三個字符串#include<stdio.h>#include<stdlib.h>intmain(){ charstr[3][20],filename[10],*p; FILE*fp; inti; puts("請輸入三個字符串\n"); for(i=0;i<3;i++) { gets(str[i]); } puts("請輸入一個文件名:"); gets(filename); fp=fopen(filename,"w"); if(fp==NULL) { printf("文件打開出錯。"); exit(0); } for(i=0;i<3;i++) { fputs(str[i],fp); fputs("\n",fp); } fclose(fp); return0;}/*iloveyouyouloveme?yes?請輸入一個文件名:flow.txt請按任意鍵繼續(xù)...*/C語言課本//5.9輸出-200之間的素數(shù)#include<stdio.h>#include<math.h>intmain(){ boolifSuNum(inta); boolisSu; intn;for(n=101;n<=200;n+=2) { isSu=ifSuNum(n); if(isSu) { printf("%d\t",n); } elsecontinue; } return0;}/*101103107109113127131137139149151157163167173179181191193197199請按任意鍵繼續(xù)...*/boolifSuNum(inta){ intk=(int)sqrt(a*1.0); inti; for(i=2;i<=k;i++){ if(a%i==0) break; } if(i>=k+1) returntrue; elsereturnfalse;}#include<stdio.h>//5.5要求輸出-200之間不能被整除的數(shù)字intmain(){ intn; for(n=1;n<100;n++){ if(n%3==0) printf("%d\t",n); elsecontinue; } printf("\n"); return0;}/*369121518212427303336394245485154576063666972757881848790939699請按任意鍵繼續(xù)...*///例.2#include<stdio.h>intmain(){ voidexchange(int*p1,int*p2,int*p3); inta,b,c; scanf("%d%d%d",&a,&b,&c); int*p1,*p2,*p3; p1=&a; p2=&b; p3=&c; exchange(p1,p2,p3); printf("%d,%d,%d",*p1,*p2,*p3); printf("\n"); printf("%d,%d,%d",a,b,c); printf("\n"); return0;}/*1020220,10,220,10,2請按任意鍵繼續(xù)...*/voidexchange(int*p1,int*p2,int*p3){ voidswap(int*p1,int*p2); if(*p1<*p2)swap(p1,p2); if(*p1<*p3)swap(p1,p3); if(*p2<*p3)swap(p2,p3);}voidswap(int*p1,int*p2){ inttemp; if(*p1<*p2){ temp=*p1; *p1=*p2; *p2=temp; }}棧//棧的各種操作#include<stdio.h>#include<stdlib.h>//包含exit();typedefcharDataType;//假定數(shù)據類型為字符typedefstructstacknode{DataTypedata;structstacknode*next;}StackNode;typedefstruct{StackNode*top;//棧頂指針}LinkStack;//置??誺oidInitial(LinkStack*s){s->top=0;}//判??読ntIsEmpty(LinkStack*s){returns->top==0;}//壓棧voidPush(LinkStack*s,DataTypex){//將元素x插入鏈棧頭部StackNode*p=(StackNode*)malloc(sizeof(StackNode));p->data=x;p->next=s->top;s->top=p;}//出棧DataTypePop(LinkStack*s){DataTypex;StackNode*p=s->top;if(IsEmpty(s)){printf("棧為空\n");exit(1);}x=p->data;s->top=p->next;free(p);returnx;}//取棧頂元素DataTypeTop(LinkStack*s){if(IsEmpty(s)){printf("棧為空\n");exit(1);}returns->top->data;}voidmain(){LinkStacks;DataTypefirst,sec;Initial(&s);Push(&s,'a');Push(&s,'b');first=Top(&s);Pop(&s);sec=Top(&s);printf("%c,%c\n",first,sec);}/*b,a請按任意鍵繼續(xù)...*/鏈表//鏈表的操作,各種操作啊有木有o(╯□╰)o#include<stdio.h>#include<stdlib.h>//#defineLENsizeof(structStudent);structStudent{ intnum; charname[10]; structStudent*next;};intmain(){ voidprintInfo(structStudent*); voidinsertInfo(structStudent*); voiddeleteInfo(structStudent*,intn); structStudent*L,*p; inti,m,n=2; L=(structStudent*)malloc(sizeof(structStudent)); L->next=NULL; for(i=0;i<n;i++) { p=(structStudent*)malloc(sizeof(structStudent)); puts("請分別輸入學生的學號與姓名"); scanf("%d%s",&p->num,p->name); p->next=L->next; L->next=p; } insertInfo(L); puts("請輸入要刪除第幾個節(jié)點\n"); scanf("%d",&m); deleteInfo(L,m); printInfo(L); return0;}/*請分別輸入學生的學號與姓名2008zhangfei請分別輸入學生的學號與姓名2013lisi請輸入要插入鏈表的學生的學號和姓名2015wangwu請輸入要刪除第幾個節(jié)點2wangwu的詳細信息為學號:,姓名:wangwulisi的詳細信息為學號:,姓名:lisi請按任意鍵繼續(xù)...*/voidinsertInfo(structStudent*L){ structStudent*p=(structStudent*)malloc(sizeof(structStudent)); puts("請輸入要插入鏈表的學生的學號和姓名\n"); scanf("%d%s",&p->num,p->name); p->next=L->next; L->next=p;}voiddeleteInfo(structStudent*L,intn){ structStudent*p,*q; p=L->next; intj=0; if(j<(n-1)&&p) { p=p->next; } q=p->next; p->next=q->next; free(q);}voidprintInfo(structStudent*L){ structStudent*p=L->next; while(p!=NULL) { printf("%s的詳細信息為\n",p->name); printf("學號:%d,姓名:%s\n",p->num,p->name); p=p->next; }}樹樹的遞歸遍歷#include"stdio.h"#include"string.h"#include<stdlib.h>#defineNULL0typedefstructBiTNode{chardata;structBiTNode*lchild,*rchild;}BiTNode,*BiTree;BiTreeCreate(BiTreeT){charch;ch=getchar();if(ch=='0')T=NULL;else{if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))printf("Error!");T->data=ch;T->lchild=Create(T->lchild);T->rchild=Create(T->rchild);}returnT;}voidPreorder(BiTreeT){if(T){printf("%c",T->data);Preorder(T->lchild);Preorder(T->rchild);}}//先序遍歷voidInorder(BiTreeT){if(T){Inorder(T->lchild);printf("%c",T->data);Inorder(T->rchild);}}//中序遍歷voidPostorder(BiTreeT){if(T){Postorder(T->lchild);Postorder(T->rchild);printf("%c",T->data);}}//后序遍歷/*前序輸入二叉樹的結點序列,以表示結點為空,按回車結束:abd00e00c00選擇遍歷算法:先序遍歷-1,中序遍歷-2,后序遍歷-31abdec請按任意鍵繼續(xù)...*/voidmain(){BiTreeT=NULL;intchoice;printf("前序輸入二叉樹的結點序列,以表示結點為空,按回車結束:\n");T=Create(T);printf("選擇遍歷算法:先序遍歷-1,中序遍歷-2,后序遍歷-3\n");scanf("%d",&choice);if(choice==1){Preorder(T);printf("\n");}elseif(choice==2){Inorder(T);printf("\n");}else{Postorder(T);printf("\n");}}樹的非遞歸遍歷#include<stdio.h>#include<stdlib.h>#defineMAXSIZE200/*定義二叉樹節(jié)點類型*/typedefstructnode{chardata;structnode*lchild,*rchild;}BTNode;/*函數(shù)聲明*/BTNode*CreatBitTree();voidPreOrder(BTNode*);voidInOrder(BTNode*);voidPostOrder(BTNode*);/*主函數(shù)*/intmain(){BTNode*root=NULL;root=CreatBitTree();PreOrder(root);InOrder(root);PostOrder(root);system("pause");return0;}/*abd##e##c##abdecdbeacdebca請按任意鍵繼續(xù)...*//*遞歸前序建立二叉樹*/BTNode*CreatBitTree(){charch;BTNode*b;scanf("%c",&ch);/*遇到空節(jié)點停止遞歸*/if(ch=='#'){b=NULL;}else{b=(BTNode*)malloc(sizeof(BTNode));/*建立根節(jié)點*/b->data=ch;/*遞歸先序建立左子樹*/b->lchild=CreatBitTree();/*遞歸先序建立右子樹*/b->rchild=CreatBitTree();}returnb;}/*非遞歸前序遍歷二叉樹*/voidPreOrder(BTNode*b){BTNode*stack[MAXSIZE],*p;inttop=-1;if(b!=NULL){/*根節(jié)點入棧*/top++;stack[top]=b;/*棧不空時循環(huán)*/while(top>-1){/*出棧并訪問該節(jié)點*/p=stack[top];top--;printf("%c",p->data);/*右孩子入棧*/if(p->rchild!=NULL){top++;stack[top]=p->rchild;}/*左孩子入棧*/if(p->lchild!=NULL){top++;stack[top]=p->lchild;}}printf("\n");}}/*非遞歸中序遍歷二叉樹*/voidInOrder(BTNode*b){BTNode*stack[MAXSIZE],*p;inttop=-1;if(b!=NULL){p=b;while(top>-1||p!=NULL){/*掃描p的所有左節(jié)點并入棧*/while(p!=NULL){top++;stack[top]=p;p=p->lchild;}if(top>-1){/*出棧并訪問該節(jié)點*/p=stack[top];top--;printf("%c",p->data);/*掃描p的右孩子*/p=p->rchild;}}printf("\n");}}/*非遞歸后序遍歷二叉樹*/voidPostOrder(BTNode*b){BTNode*stack[MAXSIZE],*p;intsign,top=-1;if(b!=NULL){do{/*b所有左節(jié)點入棧*/while(b!=NULL){top++;stack[top]=b;b=b->lchild;}/*p指向棧頂前一個已訪問節(jié)點*/p=NULL;/*置b為已訪問*/sign=1;while(top!=-1&&sign){/*取出棧頂節(jié)點*/b=stack[top];/*右孩子不存在或右孩子已訪問則訪問b*/if(b->rchild==p){printf("%c",b->data);top--;/*p指向被訪問的節(jié)點*/p=b;}else{/*b指向右孩子節(jié)點*/b=b->rchild;/*置未訪問標記*/sign=0;}}}while(top!=-1);printf("\n");}}圖圖的遍歷#include<stdio.h>#include<malloc.h>#defineMAX31intvisited[MAX];typedefstructArcNode{intadjvex;//該弧指向的頂點的位置structArcNode*nextarc;//指向下一條弧的指針}ArcNode;typedefstructVNode{chardata;//頂點信息structArcNode*firstarc;//指向第一條依附該頂點的弧的指針}VNode,AdjList[MAX];typedefstruct{AdjListadjlist;intvexnum,arcnum;//圖的當前頂點數(shù)和弧數(shù)}ALGraph;voidCreateDG(ALGraph*G);//創(chuàng)建鄰接表voidDispAdj(ALGraph*G);//輸出鄰接表voidDFS(ALGraph*G,intv);//深度優(yōu)先遍歷voidBFS(ALGraph*G,intv);//廣度優(yōu)先遍歷voidCreateDG(ALGraph*G){inti,k,j,v1,v2;ArcNode*p;printf("輸入圖的頂點數(shù)和弧數(shù):");scanf("%d,%d",&G->vexnum,&G->arcnum);printf("\n");for(i=1;i<=G->vexnum;i++){printf("輸入第%d個頂點信息:",i);scanf("%s",&G->adjlist[i].data);}printf("\n");for(i=1;i<=G->vexnum;++i)G->adjlist[i].firstarc=NULL;for(k=1;k<=G->arcnum;++k){printf("輸入第%d條弧的弧尾和弧頭:",k);scanf("%d,%d",&v1,&v2);i=v1;j=v2;p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=j;p->nextarc=G->adjlist[i].firstarc;G->adjlist[i].firstarc=p;}printf("\n");}voidDispAdj(ALGraph*G){inti;ArcNode*q;for(i=1;i<=G->vexnum;++i){q=G->adjlist[i].firstarc;printf("%d",i);while(q!=NULL){printf("->%d",q->adjvex);q=q->nextarc;}printf("\n");}}voidDFS(ALGraph*G,intv){ArcNode*p;visited[v]=1;printf("%3d",v);for(p=G->adjlist[v].firstarc;p!=NULL;){if(!visited[p->adjvex])DFS(G,p->adjvex);p=p->nextarc;}}voidBFS(ALGraph*G,intv){ArcNode*p;intqueue[MAX],w,i;intfront=0,rear=0;for(i=0;i<MAX;i++)visited[i]=0;printf("%3d",v);visited[v]=1;rear=(rear+1)%MAX;queue[rear]=v;while(front!=rear){front=(front+1)%MAX;w=queue[front];p=G->adjlist[w].firstarc;while(p!=NULL){if(visited[p->adjvex]==0){printf("%3d",p->adjvex);visited[p->adjvex]=1;rear=(rear+1)%MAX;queue[rear]=p->adjvex;}p=p->nextarc;}}printf("%\n");}intmain(){//inti,j,k;ALGraph*G;printf("\n");G=(ALGraph*)malloc(sizeof(ALGraph));CreateDG(G);printf("圖的鄰接表為:\n");DispAdj(G);printf("\n");printf("從頂點開始的深度優(yōu)先搜索:\n");DFS(G,1);printf("\n");printf("從頂點開始的廣度優(yōu)先搜索:\n");BFS(G,1);printf("\n");return0;}/*輸入圖的頂點數(shù)和弧數(shù):8,9輸入第個頂點信息:v1輸入第個頂點信息:v2輸入第個頂點信息:v3輸入第個頂點信息:v4輸入第個頂點信息:v5輸入第個頂點信息:v6輸入第個頂點信息:v7輸入第個頂點信息:v8輸入第條弧的弧尾和弧頭:1,3輸入第條弧的弧尾和弧頭:1,2輸入第條弧的弧尾和弧頭:2,5輸入第條弧的弧尾和弧頭:2,4輸入第條弧的弧尾和弧頭:3,7輸入第條弧的弧尾和弧頭:3,6輸入第條弧的弧尾和弧頭:4,8輸入第條弧的弧尾和弧頭:5,8輸入第條弧的弧尾和弧頭:6,7圖的鄰接表為:1->2->32->4->53->6->74->85->86->778從頂點開始的深度優(yōu)先搜索:12485367從頂點開始的廣度優(yōu)先搜索:12345678請按任意鍵繼續(xù)...*/圖的拓撲排序//stack.h頭文件//stack.h頭文件#include<stdio.h>#include<malloc.h>#include<stdlib.h>#defineSTACKSIZE50#defineSTACKINCREMENT20#defineOVERFLOW-1#defineOK1#defineERROR-1typedefstruct{int*base;int*top;intstacksize;}Stack;intInitStack(Stack&s)//創(chuàng)建一個空棧{s.base=(int*)malloc(STACKSIZE*sizeof(int));if(!s.base)return(OVERFLOW);s.top=s.base;s.stacksize=STACKSIZE;return(OK);}intPush(Stack&s,inte){if((s.top-s.base)>s.stacksize){s.base=(int*)realloc(s.base,(STACKSIZE+STACKINCREMENT)*sizeof(int));if(!s.base)return(OVERFLOW);s.top=s.base+s.stacksize;s.stacksize+=STACKINCREMENT;}*s.top++=e;return(OK);}boolEmpty(Stacks){if(s.base==s.top)returntrue;elsereturnfalse;}intPop(Stack&s){inte;e=*--s.top;returne;}//graph.h文件//有向無環(huán)圖的拓撲排序#include<stdio.h>#include<malloc.h>#include<stdlib.h>#defineMAX20#defineNULL0typedefstructArcNode//頭節(jié)點{intadjvex;//該邊所指向的頂點的位置structArcNode*nextarc;//指向下一條邊}ArcNode;typedefstructVNode//表節(jié)點{intdata;//頂點信息intindegree;//節(jié)點的入度ArcNode*firstarc;//指向第一條依附該節(jié)點的邊的指針}VNode,AdjList[MAX];typedefstruct{AdjListvertices;//表節(jié)點intvexnum;//節(jié)點的個數(shù)intarcnum;//邊的條數(shù)}Graph;intLocateVex(GraphG,intv)//返回節(jié)點v在圖中的位置{inti;for(i=0;i<G.vexnum;++i)if(G.vertices[i].data==v)break;elsecontinue;if(i<G.vexnum)returni;elsereturn-1;}voidCreateGraph(Graph&G){intm,n;printf("請輸入圖的節(jié)點數(shù):");scanf("%d",&m);while(m<0){printf("Error!\n頂點數(shù)不能小于.\n");printf("請重新輸入圖的頂點數(shù):");scanf("%d",&m);}printf("請輸入圖的邊數(shù):");scanf("%d",&n);while(n<0){printf("Error!\n圖的邊數(shù)不能小于.\n");printf("請重新輸入圖的邊數(shù):");scanf("%d",&n);}G.vexnum=m;//頂點數(shù)目G.arcnum=n;//邊的數(shù)目inti,j,k;for(i=0;i<G.vexnum;++i)//初始化圖的信息{G.vertices[i].data=i+1;//頂點信息G.vertices[i].firstarc=NULL;G.vertices[i].indegree=0;//開始時入度都為}//頂點信息printf("輸出頂點信息:\n");for(i=0;i<G.vexnum;++i)printf("v%d\n",G.vertices[i].data);intv1,v2,flag=0;for(k=0;k<G.arcnum;++k){printf("請輸入第%d邊的起點和終點:",k+1);scanf("%d%d",&v1,&v2);i=LocateVex(G,v1);//頂點v1在圖中的位置j=LocateVex(G,v2);//頂點v2在圖中的位置if(i>=0&&j>=0){++flag;(G.vertices[j].indegree)++;ArcNode*p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=j;p->nextarc=NULL;ArcNode*p1;if(!G.vertices[i].firstarc)G.vertices[i].firstarc=p;else{for(p1=G.vertices[i].firstarc;p1->nextarc;p1=p1->nextarc);//求該頂點的最后一個鄰接頂點p1->nextarc=p;//將p插入到最后一個鄰接頂點的后面}}else//沒有該弧,刪除掉{printf("沒有該邊!\n");k=flag;}}//輸出鄰接表printf("構造的鄰接表為:\n");printf("位置頂點弧\n");for(i=0;i<G.vexnum;++i){printf("%dv%d",i,G.vertices[i].data);ArcNode*p=G.vertices[i].firstarc;if(p){while(p->nextarc){printf("->v%d",p->adjvex+1);p=p->nextarc;}printf("->v%d\

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論