C語(yǔ)言實(shí)現(xiàn)樹的動(dòng)態(tài)查找實(shí)例代碼
C語(yǔ)言實(shí)現(xiàn)樹的動(dòng)態(tài)查找實(shí)例代碼
本例演示一種樹數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)記錄集合時(shí)的動(dòng)態(tài)查找方法。首先程序通過construct()函數(shù),利用已經(jīng)存在的結(jié)構(gòu)體數(shù)組數(shù)據(jù)建立一個(gè)二叉樹,建立樹的過程中,要保證每個(gè)節(jié)點(diǎn)的值都大于它的左子樹上節(jié)點(diǎn)的值而小于它右子樹所有節(jié)點(diǎn)的值,該函數(shù)返回建立樹的根指針;然后通過函數(shù)Search(root,name)查找,如果找到相應(yīng)的數(shù)據(jù),將其打印出來(lái),如果沒有找到,則用戶可以選擇是否將該數(shù)據(jù)插入到樹中。
具體代碼如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NUM 4 struct tree { char name[20]; char city[20]; char sex[10]; char age[10]; char job[10]; struct tree *left; struct tree *right; }; struct tree Datas[NUM]= { "Willing","Tianjing","Female","21","worker",NULL,NULL, "Tom","Beijing","Male","31","doctor",NULL,NULL, "Sun","Weifang","Male","24","student",NULL,NULL, "Marry","Shanghai","Female","19","techer",NULL,NULL }; struct tree *construct( struct tree *root, struct tree *r, struct tree *Data) { if(!r) { r = (struct tree *)malloc(sizeof(struct tree)); if(!r) { printf("內(nèi)存分配失?。?); exit(0); } r->left = NULL; r->right = NULL; strcpy(r->name,Data->name); strcpy(r->city,Data->city); strcpy(r->sex,Data->sex); strcpy(r->age,Data->age); strcpy(r->job,Data->job); if(!root) return r; if(strcmp(Data->name,root->name)<0) root->left = r; else root->right = r; return r; } if(strcmp(Data->name,r->name)<0) construct(r,r->left,Data); else construct(r,r->right,Data); return root; } struct tree *Search(root,name) struct tree *root; char name[]; { struct tree *p; if(root == NULL) printf("該樹為空\(chéng)n"); p = root; while(strcmp(p->name,name)!=0) { if(strcmp(p->name,name)>0) p = p->left; else p = p->right; if(p == NULL) break; } return(p); } void print(struct tree *r) { if(!r) return; print(r->left); printf("%s\n",r->name); print(r->right); } void print_currentData(struct tree *point) { if(point == NULL) return; printf(" 姓名:%s\n",point->name); printf(" 城市:%s\n",point->city); printf(" 性別:%s\n",point->sex); printf(" 年齡:%s\n",point->age); printf(" 工作:%s\n",point->job); } int main(void) { int i; char c[10]; char swap[20]; char name[20]; struct tree *root,*p; struct tree *temp; p = NULL; temp = NULL; root = NULL; for(i = 0;i<NUM;i++) root =construct(root,root,&Datas[i]); printf("現(xiàn)有人員資料:\n"); print(root); printf("請(qǐng)輸入要查找的人的名字\n"); scanf("%s",name); p = Search(root,name); if(p == NULL) { printf("沒有該人資料\n"); printf("是否要插入該人資料[y/n]\n"); scanf("%s",c); if(strcmp(c,"y")==0) { temp = (struct tree *)malloc(sizeof(struct tree)); if(!temp) { printf("內(nèi)存分配失?。?); exit(0); } printf("請(qǐng)輸入該人姓名:\n"); scanf("%s",swap); strcpy(temp->name,swap); printf("請(qǐng)輸入該人所在城市:\n"); scanf("%s",swap); strcpy(temp->city,swap); printf("請(qǐng)輸入該人性別[Male/Female]:\n"); scanf("%s",swap); strcpy(temp->sex,swap); printf("請(qǐng)輸入該人年齡:\n"); scanf("%s",swap); strcpy(temp->age,swap); printf("請(qǐng)輸入該人工作:\n"); scanf("%s",swap); strcpy(temp->job,swap); temp->left = NULL; temp->right = NULL; root =construct(root,root,temp); print_currentData(temp); printf("現(xiàn)有人員資料:\n"); root = root; print(root); } else return 0; } print_currentData(p); return 1; }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:C語(yǔ)言數(shù)據(jù)輸入與輸出實(shí)例詳解
欄 目:C語(yǔ)言
本文標(biāo)題:C語(yǔ)言實(shí)現(xiàn)樹的動(dòng)態(tài)查找實(shí)例代碼
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1431.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段函數(shù)
- 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
- 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文