2.[問題描述] 編寫遞歸算法,在二叉樹中求位于先序序列中第K個位置的結(jié)點 [輸入] 按照先序序列的順序輸入該結(jié)點的內(nèi)容。其輸入abd eh cf i g 。輸入要求的位置 [輸出] 若二叉樹不空,按先序序列輸出,求出所求位置的結(jié)點 [存儲結(jié)構(gòu)] 采用二叉表存儲 [算法的基本思想] 采用遞歸方法建立和遍歷二叉樹。首先建立二叉樹的根結(jié)點,然后建立其左右子樹,直到空子樹為止,先序遍歷二叉樹時,先遍厲左子樹,后遍厲右子樹,最后訪問根結(jié)點并計算出二叉樹中葉子結(jié)點的數(shù)目和第K個位置的結(jié)點 #include<stdio.h> #include<MALLOC.h> struct node{ char info struct node*llink,*rlink } typedef struct node NODE
上傳時間: 2014-01-13
上傳用戶:zm7516678
C語言內(nèi)存管理源碼,一個介紹MALLOC實現(xiàn)的源碼。嵌入式系統(tǒng)里面可以參考,以了解如何減少內(nèi)存碎片的問題!
上傳時間: 2016-01-26
上傳用戶:miaochun888
一個類STL的多平臺可移植的算法容器庫,主要用于嵌入式系統(tǒng)編程時的內(nèi)存管理等方面,尤其是缺少支持MALLOC 和free的情況下。
上傳時間: 2016-04-17
上傳用戶:tfyt
編寫一個C程序,用char *MALLOC(unsigned size)函數(shù)向系統(tǒng)申請一次內(nèi)存空間(如size=1000,單位為字節(jié)),用循環(huán)首次適應(yīng)法 addr = (char *)lMALLOC(unsigned size) 和 lfree(unsigned size,char * addr)模擬可變分區(qū)內(nèi)存管理,實現(xiàn)對該內(nèi)存區(qū)的分配和釋放管理。
上傳時間: 2016-06-18
上傳用戶:fredguo
內(nèi)存越界操作檢測工具,既可以在windows下使用,也可以在linux下使用,作為動態(tài)庫鏈入可執(zhí)行檔即可使用里面的MALLOC
上傳時間: 2013-12-23
上傳用戶:litianchu
在程序中輸入數(shù)組大小n,通過動態(tài)分配內(nèi)存函數(shù)MALLOC產(chǎn)生一個整型數(shù)組,接著輸入該數(shù)組的n個元數(shù),并使用一個函數(shù)對組數(shù)進(jìn)行排序(從小到大,要求用選擇法實現(xiàn))
上傳時間: 2013-12-21
上傳用戶:戀天使569
小型內(nèi)存分配模塊,用通用循環(huán)雙向鏈表實現(xiàn),改程序模擬系統(tǒng)MALLOC
標(biāo)簽: 內(nèi)存分配 模塊 循環(huán)
上傳時間: 2014-01-11
上傳用戶:libinxny
兩個鏈表的交集 #include<stdio.h> #include<stdlib.h> typedef struct Node{ int data; struct Node *next; }Node; void initpointer(struct Node *p){ p=NULL; } int printlist(struct Node* head){ int flag=1; head=head->next; /* 因為標(biāo)記1的地方你用了頭結(jié)點,所以第一個數(shù)據(jù)域無效,應(yīng)該從下一個頭元結(jié)點開始 */ if(head==NULL) printf("NULL\n"); else { while(head!=NULL) { if(flag==1) { printf("%d",head->data); flag=0; } else { printf(" %d",head->data); } head=head->next; } printf("\n"); } return 0; } struct Node *creatlist(struct Node *head) { int n; struct Node *p1=(struct Node *)MALLOC(sizeof(struct Node)); p1->next=NULL; while(scanf("%d",&n),n!=-1) { struct Node *pnode=(struct Node *)MALLOC(sizeof(struct Node)); pnode->next=NULL; pnode->data=n; if(head==NULL) head=pnode; p1->next=pnode; p1=pnode; } return head; } struct Node *Intersect(struct Node *head1, struct Node *head2) { struct Node *p1=head1,*p2=head2;/*我這里沒有用頭指針和頭結(jié)點,這里是首元結(jié)點head1里面就是第一個數(shù)據(jù),一定要理解什么事頭指針, 頭結(jié)點,和首元結(jié)點 具體你一定要看這個博客:http://blog.sina.com.cn/s/blog_71e7e6fb0101lipz.html*/ struct Node *head,*p,*q; head = (struct Node *)MALLOC(sizeof(struct Node)); head->next = NULL; p = head; while( (p1!=NULL)&&(p2!=NULL) ) { if (p1->data == p2->data) { q = (struct Node *)MALLOC(sizeof(struct Node)); q->data = p1->data; q->next = NULL; p->next = q;//我可以認(rèn)為你這里用了頭結(jié)點,也就是說第一個數(shù)據(jù)域無效 **標(biāo)記1** p = q; p1 = p1->next; p2 = p2->next; } else if (p1->data < p2->data) { p1 = p1->next; } else { p2 = p2->next; } } return head; } int main() { struct Node *head=NULL,*headt=NULL,*t; //initpointer(head);//這里的函數(shù)相當(dāng)于head=NULL; // initpointer(headt);//上面已經(jīng)寫了headt=NULL那么這里可以不用調(diào)用這個函數(shù) head=creatlist(head); headt=creatlist(headt); t=Intersect(head,headt); printlist(t); }
標(biāo)簽: c語言編程
上傳時間: 2015-04-27
上傳用戶:coco2017co
#include <stdlib.h> #include<stdio.h> #include <MALLOC.h> #define stack_init_size 100 #define stackincrement 10 typedef struct sqstack { int *base; int *top; int stacksize; } sqstack; int StackInit(sqstack *s) { s->base=(int *)MALLOC(stack_init_size *sizeof(int)); if(!s->base) return 0; s->top=s->base; s->stacksize=stack_init_size; return 1; } int Push(sqstack *s,int e) { if(s->top-s->base>=s->stacksize) { s->base=(int *)realloc(s->base,(s->stacksize+stackincrement)*sizeof(int)); if(!s->base) return 0; s->top=s->base+s->stacksize; s->stacksize+=stackincrement; } *(s->top++)=e; return e; } int Pop(sqstack *s,int e) { if(s->top==s->base) return 0; e=*--s->top; return e; } int stackempty(sqstack *s) { if(s->top==s->base) { return 1; } else { return 0; } } int conversion(sqstack *s) { int n,e=0,flag=0; printf("輸入要轉(zhuǎn)化的十進(jìn)制數(shù):\n"); scanf("%d",&n); printf("要轉(zhuǎn)化為多少進(jìn)制:\n"); scanf("%d",&flag); printf("將十進(jìn)制數(shù)%d 轉(zhuǎn)化為%d 進(jìn)制是:\n",n,flag); while(n) { Push(s,n%flag); n=n/flag; } while(!stackempty(s)) { e=Pop(s,e); switch(e) { case 10: printf("A"); break; case 11: printf("B"); break; case 12: printf("C"); break; case 13: printf("D"); break; case 14: printf("E"); break; case 15: printf("F"); break; default: printf("%d",e); } } printf("\n"); return 0; } int main() { sqstack s; StackInit(&s); conversion(&s); return 0; }
標(biāo)簽: 整數(shù) 棧 基本操作 十進(jìn)制 轉(zhuǎn)化 進(jìn)制
上傳時間: 2016-12-08
上傳用戶:愛你198
#include <MALLOC.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define NULL 0 #define MaxSize 30 typedef struct athletestruct /*運動員*/ { char name[20]; int score; /*分?jǐn)?shù)*/ int range; /**/ int item; /*項目*/ }ATH; typedef struct schoolstruct /*學(xué)校*/ { int count; /*編號*/ int serial; /**/ int menscore; /*男選手分?jǐn)?shù)*/ int womenscore; /*女選手分?jǐn)?shù)*/ int totalscore; /*總分*/ ATH athlete[MaxSize]; /**/ struct schoolstruct *next; }SCH; int nsc,msp,wsp; int ntsp; int i,j; int overgame; int serial,range; int n; SCH *head,*pfirst,*psecond; int *phead=NULL,*pafirst=NULL,*pasecond=NULL; void create(); void input () { char answer; head = (SCH *)MALLOC(sizeof(SCH)); /**/ head->next = NULL; pfirst = head; answer = 'y'; while ( answer == 'y' ) { Is_Game_DoMain: printf("\nGET Top 5 when odd\nGET Top 3 when even"); printf("\n輸入運動項目序號 (x<=%d):",ntsp); scanf("%d",pafirst); overgame = *pafirst; if ( pafirst != phead ) { for ( pasecond = phead ; pasecond < pafirst ; pasecond ++ ) { if ( overgame == *pasecond ) { printf("\n這個項目已經(jīng)存在請選擇其他的數(shù)字\n"); goto Is_Game_DoMain; } } } pafirst = pafirst + 1; if ( overgame > ntsp ) { printf("\n項目不存在"); printf("\n請重新輸入"); goto Is_Game_DoMain; } switch ( overgame%2 ) { case 0: n = 3;break; case 1: n = 5;break; } for ( i = 1 ; i <= n ; i++ ) { Is_Serial_DoMain: printf("\n輸入序號 of the NO.%d (0<x<=%d): ",i,nsc); scanf("%d",&serial); if ( serial > nsc ) { printf("\n超過學(xué)校數(shù)目,請重新輸入"); goto Is_Serial_DoMain; } if ( head->next == NULL ) { create(); } psecond = head->next ; while ( psecond != NULL ) { if ( psecond->serial == serial ) { pfirst = psecond; pfirst->count = pfirst->count + 1; goto Store_Data; } else { psecond = psecond->next; } } create(); Store_Data: pfirst->athlete[pfirst->count].item = overgame; pfirst->athlete[pfirst->count].range = i; pfirst->serial = serial; printf("Input name:) : "); scanf("%s",pfirst->athlete[pfirst->count].name); } printf("\n繼續(xù)輸入運動項目(y&n)?"); answer = getchar(); printf("\n"); } } void calculate() /**/ { pfirst = head->next; while ( pfirst->next != NULL ) { for (i=1;i<=pfirst->count;i++) { if ( pfirst->athlete[i].item % 2 == 0 ) { switch (pfirst->athlete[i].range) { case 1:pfirst->athlete[i].score = 5;break; case 2:pfirst->athlete[i].score = 3;break; case 3:pfirst->athlete[i].score = 2;break; } } else { switch (pfirst->athlete[i].range) { case 1:pfirst->athlete[i].score = 7;break; case 2:pfirst->athlete[i].score = 5;break; case 3:pfirst->athlete[i].score = 3;break; case 4:pfirst->athlete[i].score = 2;break; case 5:pfirst->athlete[i].score = 1;break; } } if ( pfirst->athlete[i].item <=msp ) { pfirst->menscore = pfirst->menscore + pfirst->athlete[i].score; } else { pfirst->womenscore = pfirst->womenscore + pfirst->athlete[i].score; } } pfirst->totalscore = pfirst->menscore + pfirst->womenscore; pfirst = pfirst->next; } } void output() { pfirst = head->next; psecond = head->next; while ( pfirst->next != NULL ) { // clrscr(); printf("\n第%d號學(xué)校的結(jié)果成績:",pfirst->serial); printf("\n\n項目的數(shù)目\t學(xué)校的名字\t分?jǐn)?shù)"); for (i=1;i<=ntsp;i++) { for (j=1;j<=pfirst->count;j++) { if ( pfirst->athlete[j].item == i ) { printf("\n %d\t\t\t\t\t\t%s\n %d",i,pfirst->athlete[j].name,pfirst->athlete[j].score);break; } } } printf("\n\n\n\t\t\t\t\t\t按任意建 進(jìn)入下一頁"); getchar(); pfirst = pfirst->next; } // clrscr(); printf("\n運動會結(jié)果:\n\n學(xué)校編號\t男運動員成績\t女運動員成績\t總分"); pfirst = head->next; while ( pfirst->next != NULL ) { printf("\n %d\t\t %d\t\t %d\t\t %d",pfirst->serial,pfirst->menscore,pfirst->womenscore,pfirst->totalscore); pfirst = pfirst->next; } printf("\n\n\n\t\t\t\t\t\t\t按任意建結(jié)束"); getchar(); } void create() { pfirst = (struct schoolstruct *)MALLOC(sizeof(struct schoolstruct)); pfirst->next = head->next ; head->next = pfirst ; pfirst->count = 1; pfirst->menscore = 0; pfirst->womenscore = 0; pfirst->totalscore = 0; } void Save() {FILE *fp; if((fp = fopen("school.dat","wb"))==NULL) {printf("can't open school.dat\n"); fclose(fp); return; } fwrite(pfirst,sizeof(SCH),10,fp); fclose(fp); printf("文件已經(jīng)成功保存\n"); } void main() { system("cls"); printf("\n\t\t\t 運動會分?jǐn)?shù)統(tǒng)計\n"); printf("輸入學(xué)校數(shù)目 (x>= 5):"); scanf("%d",&nsc); printf("輸入男選手的項目(x<=20):"); scanf("%d",&msp); printf("輸入女選手項目(<=20):"); scanf("%d",&wsp); ntsp = msp + wsp; phead = (int *)calloc(ntsp,sizeof(int)); pafirst = phead; pasecond = phead; input(); calculate(); output(); Save(); }
標(biāo)簽: 源代碼
上傳時間: 2016-12-28
上傳用戶:150501
蟲蟲下載站版權(quán)所有 京ICP備2021023401號-1