數(shù)據(jù)結(jié)構(gòu)上機(jī)考試(含答案_第1頁
數(shù)據(jù)結(jié)構(gòu)上機(jī)考試(含答案_第2頁
數(shù)據(jù)結(jié)構(gòu)上機(jī)考試(含答案_第3頁
數(shù)據(jù)結(jié)構(gòu)上機(jī)考試(含答案_第4頁
數(shù)據(jù)結(jié)構(gòu)上機(jī)考試(含答案_第5頁
已閱讀5頁,還剩27頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、數(shù)據(jù)結(jié)構(gòu)上機(jī)練習(xí)題1、設(shè)有兩個有序序列,利用歸并排序?qū)⑺鼈兣懦捎行虮?,并輸出?、設(shè)有一有序序列,從鍵盤輸入一個數(shù),判別是否在序列中,如果在輸出“YSE”;否則,將它插入到序列中使它仍然有序,并輸出排序后的序列。3、設(shè)有一有序序列,從鍵盤輸入一個數(shù),判別是否在序列中,如果不在,則輸出“NO”,否則,將它從序列中刪除它,并輸出刪除后的序列。4、從鍵盤輸入一組任意數(shù)據(jù),建立一個有序鏈表,并從鏈頭開始輸出該鏈,使輸出結(jié)果是有序的。5、從鍵盤輸入一組任意數(shù)據(jù),建立一個包含所有輸入數(shù)據(jù)的單向循環(huán)鏈表,并從鏈表的任意開始,依次輸出該鏈表中的所有結(jié)點(diǎn)。10、設(shè)有一個鏈表,(自己建立,數(shù)據(jù)從鍵盤輸入),再從鍵

2、盤輸入一個數(shù),判別是否在鏈表中,如果不在,則輸出“NO“,否則,將它從鏈表中刪除,并輸出刪除后的鏈表。11、設(shè)有一個鏈表,(自己建立,數(shù)據(jù)從鍵盤輸入),再從鍵盤輸入一個數(shù),判別是否在鏈表中,如果在輸出“YSE”,否則,將它從插入到鏈頭,并輸出插入后的鏈表。12、設(shè)有一個鏈表,(自己建立,數(shù)據(jù)從鍵盤輸入),再從鍵盤輸入一個數(shù),判別是否在鏈表中,如果在輸出“YSE”,否則,將它從插入到鏈尾,并輸出插入后的鏈表。13、編寫棧的壓棧push、彈棧pop函數(shù),從鍵盤輸入一組數(shù)據(jù),逐個元素壓入堆棧,然后再逐個從棧中彈出它們并輸出。14、編寫棧的壓棧push、彈棧pop函數(shù),用它判別()的匹配問題。15、按

3、類似先序遍歷結(jié)果輸入一序列,建立一棵二叉樹(算法6、4),輸出二叉樹中序遍歷的結(jié)果。16、按類似先序遍歷結(jié)果輸入一序列,建立一棵二叉樹(算法6、4),輸出二叉樹先序遍歷的結(jié)果。17、按類似先序遍歷結(jié)果輸入一序列,建立一棵二叉樹(算法6、4),輸出二叉樹后序遍歷的結(jié)果。18、按類似先序遍歷結(jié)果輸入一序列,建立一棵二叉樹(算法6、4),輸出二叉樹的總結(jié)點(diǎn)數(shù)。19、按類似先序遍歷結(jié)果輸入一序列,建立一棵二叉樹(算法6、4),輸出二叉樹葉子結(jié)點(diǎn)數(shù)。20、按類似先序遍歷結(jié)果輸入一序列,建立一棵二叉樹(算法6、4),輸出此二叉樹的高度。21、給出一個無向圖的鄰接矩陣,輸出各個頂點(diǎn)的度。22、給出一個有向圖

4、的鄰接矩陣,輸出各個頂點(diǎn)的入度與出度。23、輸入一個有序序列,利用折半查找來查找一個數(shù)是否在序列中,如在,則輸出其位置,否則輸出“NO”。24、用插入排序方法對一組數(shù)據(jù)進(jìn)行排序,并輸出每趟排序的結(jié)果。25、用選擇排序方法對一組數(shù)據(jù)進(jìn)行排序,并輸出每趟排序的結(jié)果。26、用希爾(SHELL)排序方法對一組數(shù)據(jù)進(jìn)行排序,并輸出每趟排序的結(jié)果。27、用快速排序方法對一組數(shù)據(jù)進(jìn)行排序,并輸出每趟排序的結(jié)果。答案:1. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0/鏈表的存儲結(jié)構(gòu)typedef stru

5、ct LNodeint data;struct LNode *next;LNode,*list;/順序創(chuàng)建鏈表void creatList(list &l,int n)int i;list p,q;l=(list)malloc(sizeof(LNode); /開辟頭結(jié)點(diǎn)p=l; /指針p指向頭結(jié)點(diǎn)for(i=0;i<n;i+)q=(list)malloc(sizeof(LNode); /新的結(jié)點(diǎn)scanf("%d",&q->data);p->next=q; /p的下一個結(jié)點(diǎn)指向新開辟的結(jié)點(diǎn)qp=q; /將p指針指向qp->next=N

6、ULL;/歸并排序void mergeList(list &la,list &lb,list &lc) /將已經(jīng)排好序的la,lb中的數(shù)重新排列成有序(非遞減)list pa,pb,pc;pa=la->next;pb=lb->next;lc=pc=la; /默認(rèn)將la做為lc的頭結(jié)點(diǎn)(lb亦可)while(pa&&pb) /讓pc接到數(shù)據(jù)小的結(jié)點(diǎn)上,直到pa,pb兩者有一指向空結(jié)點(diǎn)if(pa->data<=pb->data) pc->next=pa;pc=pa;pa=pa->next; else pc->n

7、ext=pb;pc=pb;pb=pb->next; pc->next=pa?pa:pb; /如果最后la有剩余結(jié)點(diǎn),即將其直接加入到lc中,反之將lb的剩余結(jié)點(diǎn)加到lc中free(lb);void printList(list l)list p;p=l->next;while(p) printf("%d ",p->data);p=p->next;void main()list la,lb,lc;printf("創(chuàng)建兩個含%d個元素的鏈表,請輸入:n",N);creatList(la,N);creatList(lb,N);me

8、rgeList(la,lb,lc);printList(lc);2. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0/鏈表的存儲結(jié)構(gòu)typedef struct LNodeint data;struct LNode *next;LNode,*list;/創(chuàng)建鏈表void creatList(list &l,int n)list p,q;l=(list)malloc(sizeof(LNode);p=l;for(int i=0;i<

9、;n;i+)q=(list)malloc(sizeof(LNode);scanf("%d",&q->data);p->next=q;p=q;p->next=NULL;/判斷元素e是否在鏈表中int inList(list l,int e)list p;p=l->next;while(p)if(p->data=e) return OK; /發(fā)現(xiàn)在里面,返回真值p=p->next; /否則指針后移,繼續(xù)找return ERROR; /未找到,返回假值(沒有執(zhí)行return OK;語句)/插入元素void insertList(list

10、 &l,int &e)list p,q,s; /q為新插入的元素開辟一個存儲空間的指針,s為p前一個指針,方便插入p=l->next;s=l;while(p)if(e<=p->data)/發(fā)現(xiàn)要插入的元素e比后面的小,開辟空間,并將e放入空間的數(shù)據(jù)域中q=(list)malloc(sizeof(LNode);q->data=e;while(s->next!=p) s=s->next; /找到p前的一個指針q->next=p; / 畫圖好好理解 ->s->p-> s->next=q; / q->break;p

11、=p->next;/輸出鏈表void printList(list l)list p;p=l->next;while(p) printf("%d ",p->data); p=p->next;void main()list l;int e;printf("創(chuàng)建%d個元素的鏈表,請輸入%d個元素:n",N,N);creatList(l,N);printf("請輸入要判斷的元素:");scanf("%d",&e);if(inList(l,e)printf("YES ")

12、;elseinsertList(l,e);printList(l);3. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0/鏈表的存儲結(jié)構(gòu)typedef struct LNodeint data;struct LNode *next;LNode,*list;/創(chuàng)建鏈表void creatList(list &l,int n)list p,q;l=(list)malloc(sizeof(LNode);p=l;for(int i=0;i&

13、lt;n;i+)q=(list)malloc(sizeof(LNode);scanf("%d",&q->data);p->next=q;p=q;p->next=NULL;/判斷元素e是否在鏈表中int insertDeleteList(list l,int e)list p,q;p=l->next; q=l;while(p)if(p->data=e) while(q->next!=p) q=q->next; /找到p前一個結(jié)點(diǎn),方便刪除操作q->next=p->next; /刪除結(jié)點(diǎn)pfree(p);return

14、 OK; /發(fā)現(xiàn)在里面,返回真值p=p->next; /否則指針后移,繼續(xù)找return ERROR; /未找到,返回假值(沒有執(zhí)行return OK;語句)/輸出鏈表void printList(list l)list p;p=l->next;while(p) printf("%d ",p->data); p=p->next;void main()list l;int e;printf("創(chuàng)建%d個元素的鏈表,請輸入%d個元素:n",N,N);creatList(l,N);printf("請輸入要判斷的元素"

15、);scanf("%d",&e);if(!insertDeleteList(l,e)printf("NO ");elseprintList(l);4. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0/鏈表的存儲結(jié)構(gòu)typedef struct LNodeint data;struct LNode *next;LNode,*list;/創(chuàng)建鏈表void creatList(list &l

16、,int n)list p,q;l=(list)malloc(sizeof(LNode);p=l;for(int i=0;i<n;i+)q=(list)malloc(sizeof(LNode);scanf("%d",&q->data);p->next=q;p=q;p->next=NULL;/鏈表排序void sortList(list &l)list p,q,r; /p標(biāo)記排序的輪數(shù)int chang; /用于交換結(jié)點(diǎn)中的數(shù)據(jù)p=l->next;while(p->next!=NULL)q=l->next; /每次比較

17、從首結(jié)點(diǎn)開始while(q->next!=NULL)r=q->next; if(q->data>r->data) /發(fā)現(xiàn)前一個比后一個大,交換數(shù)據(jù) chang=q->data;q->data=r->data;r->data=chang; q=q->next; /相鄰間下一個比較p=p->next; /下一輪比較/輸出鏈表void printList(list l)list p;p=l->next;while(p) printf("%d ",p->data); p=p->next;void m

18、ain()list l;printf("創(chuàng)建%d個元素的鏈表,請輸入%d個元素:n",N,N);creatList(l,N);sortList(l);printList(l);5. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0/鏈表的存儲結(jié)構(gòu)typedef struct LNodeint data;struct LNode *next;LNode,*list;/創(chuàng)建鏈表void creatList(list &

19、l,int n)list p,q;l=(list)malloc(sizeof(LNode); scanf("%d",&l->data); /頭結(jié)點(diǎn)也添加元素,方便輸出p=l;for(int i=1;i<n;i+)q=(list)malloc(sizeof(LNode);scanf("%d",&q->data);p->next=q;p=q;p->next=l; /讓最后一個p->next指針指向頭結(jié)點(diǎn),構(gòu)成循環(huán)鏈表/輸出鏈表void printList(list l,int pos)list p,q;in

20、t i;p=l;for(i=1;i<pos-1;i+) p=p->next; /找到指定位置的前一個位置q=p->next;do if(pos=1) printf("%d ",p->data); p=p->next; /如果指定位置為1,即按原樣輸出else p=p->next; printf("%d ",p->data); /不然,p先移到指定的位置,輸出其數(shù)據(jù)while(p->next!=q); /結(jié)束條件(p移到的下一個位置不是q,即不是最初的p,完成循環(huán)輸出)void main()list l;in

21、t pos;printf("創(chuàng)建%d個元素的循環(huán)鏈表,請輸入%d個元素:n",N,N);creatList(l,N);printf("請指明從第幾個位置輸出循環(huán)鏈表中的元素:");scanf("%d",&pos);while(pos<=0|pos>N)printf("輸入的位置不存在,請重新輸入. ");scanf("%d",&pos);printList(l,pos);11#include <stdio.h>#include <stdlib.h&g

22、t;#define N 5#define NULL 0#define OK 1#define ERROR 0/鏈表的存儲結(jié)構(gòu)typedef struct LNodeint data;struct LNode *next;LNode,*list;/創(chuàng)建鏈表void creatList(list &l,int n)list p,q;l=(list)malloc(sizeof(LNode); scanf("%d",&l->data); /頭結(jié)點(diǎn)也添加元素,方便輸出p=l;for(int i=1;i<n;i+)q=(list)malloc(sizeof(

23、LNode);scanf("%d",&q->data);p->next=q;p=q;p->next=l; /讓最后一個p->next指針指向頭結(jié)點(diǎn),構(gòu)成循環(huán)鏈表/輸出鏈表void printList(list l,int pos)list p,q;int i;p=l;for(i=1;i<pos-1;i+) p=p->next; /找到指定位置的前一個位置q=p->next;do if(pos=1) printf("%d ",p->data); p=p->next; /如果指定位置為1,即按原樣

24、輸出else p=p->next; printf("%d ",p->data); /不然,p先移到指定的位置,輸出其數(shù)據(jù)while(p->next!=q); /結(jié)束條件(p移到的下一個位置不是q,即不是最初的p,完成循環(huán)輸出)void main()list l;int pos;printf("創(chuàng)建%d個元素的循環(huán)鏈表,請輸入%d個元素:n",N,N);creatList(l,N);printf("請指明從第幾個位置輸出循環(huán)鏈表中的元素:");scanf("%d",&pos);while(p

25、os<=0|pos>N)printf("輸入的位置不存在,請重新輸入. ");scanf("%d",&pos);printList(l,pos);12#include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0/鏈表的存儲結(jié)構(gòu)typedef struct LNodeint data;struct LNode *next;LNode,*list;/創(chuàng)建鏈表void creatList(list &

26、amp;l,int n)list p,q;l=(list)malloc(sizeof(LNode);p=l;for(int i=0;i<n;i+)q=(list)malloc(sizeof(LNode);scanf("%d",&q->data);p->next=q;p=q;p->next=NULL;/判斷元素e是否在鏈表中int inList(list l,int e)list p,q;q=p=l->next;while(p)if(p->data=e) return OK; /發(fā)現(xiàn)在里面,返回真值p=p->next; /否則

27、指針后移,繼續(xù)找/沒有執(zhí)行return OK;語句,說明未找到while(q->next!=p) q=q->next; /找到鏈尾p=(list)malloc(sizeof(LNode); /為鏈尾重新開辟空間p->data=e; /接到鏈尾p->next=q->next;q->next=p;return ERROR; /未找到,返回假值/輸出鏈表void printList(list l)list p;p=l->next;while(p) printf("%d ",p->data); p=p->next;void ma

28、in()list l;int e;printf("創(chuàng)建%d個元素的鏈表,請輸入%d個元素:n",N,N);creatList(l,N);printf("請輸入要判斷的元素:");scanf("%d",&e);if(inList(l,e)printf("YES ");elseprintList(l);13#include <stdio.h>#include <stdlib.h>#define OK 1#define Error 0#define NULL 0#define maxSiz

29、e 100/棧的存儲結(jié)構(gòu)typedef struct int *base;int *top;int size;stack;/棧的初始化(順序存儲)int initStack(stack &s) /開辟maxSize大小的空間,base和top都指向基地址,同時判斷是否開辟成功,不成功返回0if(!(s.base=s.top=(int*)malloc(maxSize*sizeof(int) return Error; s.size=maxSize; /棧的大小為maxSizereturn OK;/進(jìn)棧操作int push(stack &s,int e)*s.top=e; /先將元

30、素e賦值給s.top所指的存儲空間s.top+; /top指針上移return OK;/出棧操作int pop(stack &s,int &e)if(s.base=s.top) return Error; /如果棧為空,返回0s.top-; /top指針先后移e=*s.top; /將其所指的元素值賦給ereturn e; void main()stack s;int n,e;printf("請輸入要創(chuàng)建棧的元素的個數(shù):");scanf("%d",&n);initStack(s);for(int i=0;i<n;i+)scan

31、f("%d",&e);push(s,e);while(s.base!=s.top)printf("%d ",pop(s,e);14#include <stdlib.h>#include <stdio.h>#include <stdio.h>#include <stdlib.h>#define stackincrement 8#define OK 1#define Error 0#define NULL 0#define maxSize 100/棧的存儲結(jié)構(gòu)typedef struct char *b

32、ase; /由于要存放括號,所以為char類型char *top;int size;stack;/棧的初始化(順序存儲)int initStack(stack &s) /注意開辟的空間為char類型if(!(s.base=s.top=(char*)malloc(maxSize*sizeof(char) return Error; s.size=maxSize; /棧的大小為maxSizereturn OK;/進(jìn)棧操作int push(stack &s,int e)*s.top=e; /先將元素e賦值給s.top所指的存儲空間s.top+; /top指針上移return OK;i

33、nt isEmpty(stack s)return s.base=s.top?OK:Error;/出棧操作char pop(stack &s,char &e)if(isEmpty(s) return Error; /如果棧為空,返回0s.top-; /top指針先后移e=*s.top; /將其所指的元素值賦給ereturn e; /括號匹配int match()stack s;initStack(s);char ch100,e;int flag=1,i=0 ,lenth; /flag用于標(biāo)記,如果匹配,值為1,否則為0scanf("%c",&chi)

34、;while(chi!='n') scanf("%c",&ch+i); /先將所有輸入的括號存放在數(shù)組ch中 lenth=i-1; /數(shù)組的長度,不包括'n'i=0;push(s,chi); /先將第一個括號壓棧if(chi=''|chi=')'|chi='') flag=0; /如果第一個壓入的是右括號,則肯定不匹配,flag=0else while(i<lenth)/|!emptystack(s)i+;char t;if(chi=''|chi=')

35、9;|chi='') t=pop(s,e); /彈出先前壓入的元素,將后繼輸入的括號與先前壓入的比較 if(t!=chi-1)&&(t!=chi-2) flag=0;break; /左右小括號與左右大括號的ASCII碼都相差1,左右中括號相差2,如果不滿足,則不匹配,直接退出循環(huán) else push(s,chi); /輸入的是左括號,直接壓入 if(!isEmpty(s) flag=0; /通過不斷的壓棧和彈棧,如果最后棧不為空,則肯定是左括號多于右括號,不匹配return flag;void main()int result;printf("判斷輸入

36、的各種括號是否匹配:n");result=match();if(result) printf("括號匹配正確 _n");else printf("括號匹配錯誤 *.*n");15#include "stdio.h"#include "stdlib.h"#define stackinitsize 100#define OK 1#define ERROR 0/二叉樹的二叉鏈表存儲結(jié)構(gòu)typedef struct BiTNode int data; struct BiTNode *lchild,*rchild;

37、 /左右孩子指針 BiTnode,*BiTree;int CreateBiTree(BiTree &T) /按先序次序輸入二叉樹中結(jié)點(diǎn)的值(一個字符),空格字符表示空樹。 /構(gòu)造二叉鏈表表示的二叉樹T. char ch; scanf("%c",&ch); if(ch=' ') T=NULL; else if(!(T=(BiTNode *)malloc(sizeof(BiTNode) exit(0); T->data=ch; /生成根結(jié)點(diǎn) CreateBiTree(T->lchild);/構(gòu)造左子樹 CreateBiTree(T-&

38、gt;rchild);/構(gòu)造右子樹 return OK; /CreateBiTreeint PrintElement(int e) /輸出元素e的值 printf("%c",e); return OK;int InOrderTraverse(BiTree T,int(*Visit)(int e) /采用二叉鏈表存儲結(jié)構(gòu),visit是對數(shù)據(jù)元素操作的應(yīng)用函數(shù), /中序遍歷二叉樹T的遞歸算法,對每個數(shù)據(jù)元素調(diào)用函數(shù)visit。 /調(diào)用實(shí)例: InOrderTraverse(T,printElement); if(T) if (InOrderTraverse(T->lchi

39、ld,Visit) if (Visit(T->data) if (InOrderTraverse(T->rchild,Visit) return OK; return ERROR;else return OK; void main() BiTree t; printf("請按先序遍歷輸入二叉樹(當(dāng)左右子樹為空時用空格輸入)n"); CreateBiTree(t); printf("該二叉樹的中序遍歷為:n"); InOrderTraverse(t,PrintElement); printf("n");16#include

40、"stdio.h"#include "stdlib.h"#define stackinitsize 100#define OK 1#define ERROR 0/二叉樹的二叉鏈表存儲結(jié)構(gòu)typedef struct BiTNode int data; struct BiTNode *lchild,*rchild; /左右孩子指針 BiTnode,*BiTree;int CreateBiTree(BiTree &T) /按先序次序輸入二叉樹中結(jié)點(diǎn)的值(一個字符),空格字符表示空樹。 /構(gòu)造二叉鏈表表示的二叉樹T. char ch; scanf(&q

41、uot;%c",&ch); if(ch=' ') T=NULL; else if(!(T=(BiTNode *)malloc(sizeof(BiTNode) exit(0); T->data=ch; /生成根結(jié)點(diǎn) CreateBiTree(T->lchild);/構(gòu)造左子樹 CreateBiTree(T->rchild);/構(gòu)造右子樹 return OK; /CreateBiTreeint PrintElement(int e) /輸出元素e的值 printf("%c",e); return OK;int PreOrder

42、Traverse(BiTree T,int(*Visit)(int e) /采用二叉鏈表存儲結(jié)構(gòu),visit是對數(shù)據(jù)元素操作的應(yīng)用函數(shù), /先序遍歷二叉樹T的遞歸算法,對每個數(shù)據(jù)元素調(diào)用函數(shù)visit。 /調(diào)用實(shí)例: PreOrderTraverse(T,printElement); if(T) if (Visit(T->data) if (PreOrderTraverse(T->lchild,Visit) if (PreOrderTraverse(T->rchild,Visit) return OK; return ERROR;else return OK; /preOrd

43、erTraVersevoid main() BiTree t; printf("請按先序遍歷輸入二叉樹(當(dāng)左右子樹為空時用空格輸入)n"); CreateBiTree(t); printf("該二叉樹的先序遍歷為:n"); PreOrderTraverse(t,PrintElement); printf("n");17#include "stdio.h"#include "stdlib.h"#define stackinitsize 100#define OK 1#define ERROR 0/

44、二叉樹的二叉鏈表存儲結(jié)構(gòu)typedef struct BiTNode int data; struct BiTNode *lchild,*rchild; /左右孩子指針 BiTnode,*BiTree;int CreateBiTree(BiTree &T) /按先序次序輸入二叉樹中結(jié)點(diǎn)的值(一個字符),空格字符表示空樹。 /構(gòu)造二叉鏈表表示的二叉樹T. char ch; scanf("%c",&ch); if(ch=' ') T=NULL; else if(!(T=(BiTNode *)malloc(sizeof(BiTNode) exit(

45、0); T->data=ch; /生成根結(jié)點(diǎn) CreateBiTree(T->lchild);/構(gòu)造左子樹 CreateBiTree(T->rchild);/構(gòu)造右子樹 return OK; /CreateBiTreeint PrintElement(int e) /輸出元素e的值 printf("%c",e); return OK;int PostOrderTraverse(BiTree T,int(*Visit)(int e) /采用二叉鏈表存儲結(jié)構(gòu),visit是對數(shù)據(jù)元素操作的應(yīng)用函數(shù), /后序遍歷二叉樹T的遞歸算法,對每個數(shù)據(jù)元素調(diào)用函數(shù)visit

46、。 /調(diào)用實(shí)例: PostOrderTraverse(T,printElement); if(T) if (PostOrderTraverse(T->lchild,Visit) if (PostOrderTraverse(T->rchild,Visit) if (Visit(T->data)return OK; return ERROR;else return OK; void main() BiTree t; printf("請按先序遍歷輸入二叉樹(當(dāng)左右子樹為空時用空格輸入)n"); CreateBiTree(t); printf("該二叉樹

47、的后序遍歷為:n"); PostOrderTraverse(t,PrintElement); printf("n");18#include "stdio.h"#include "stdlib.h"#define stackinitsize 100#define OK 1#define ERROR 0/#define NULL 0static int count=0;/二叉樹的二叉鏈表存儲結(jié)構(gòu)typedef struct BiTNode int data; struct BiTNode *lchild,*rchild; /左右

48、孩子指針 BiTnode,*BiTree;int CreateBiTree(BiTree &T) /按先序次序輸入二叉樹中結(jié)點(diǎn)的值(一個字符),空格字符表示空樹。 /構(gòu)造二叉鏈表表示的二叉樹T. char ch; scanf("%c",&ch); if(ch=' ') T=NULL; else if(!(T=(BiTNode *)malloc(sizeof(BiTNode) return -1; T->data=ch; /生成根結(jié)點(diǎn) CreateBiTree(T->lchild);/構(gòu)造左子樹 CreateBiTree(T->

49、;rchild);/構(gòu)造右子樹 return OK; /CreateBiTreeint PrintElement(int e) /記錄遍歷結(jié)點(diǎn)數(shù) count+; return OK;int PreOrderTraverse(BiTree T,int(*Visit)(int e) /采用二叉鏈表存儲結(jié)構(gòu),visit是對數(shù)據(jù)元素操作的應(yīng)用函數(shù), if(T) if (Visit(T->data) if (PreOrderTraverse(T->lchild,Visit) if (PreOrderTraverse(T->rchild,Visit) return OK; return

50、ERROR;else return OK; /preOrderTraVersevoid main() BiTree t; printf("請按先序遍歷輸入二叉樹(當(dāng)左右子樹為空時用空格輸入)n"); CreateBiTree(t); printf("該二叉樹的總結(jié)點(diǎn)數(shù)為: "); PreOrderTraverse(t,PrintElement); printf("%dn",count);19#include "stdio.h"#include "stdlib.h"#define stackinitsize 100#define OK 1#define ERROR 0static int count=0;/二叉樹的二叉鏈表存儲結(jié)構(gòu)typedef struct BiTNode int data; struct BiTNode *lchild,*r

溫馨提示

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

最新文檔

評論

0/150

提交評論