
作业社区
探索学习新天地,共享知识资源!
SamstagBaron 的学生作业:
练习1 #include"common.h" struct tree{ data_t value; struct tree* left; struct tree* right; }; typedef struct tree Treenode; int getHeight( struct tree* head){ if(head==NULL){ return 0; } int l = getHeight(head->left); int r = getHeight(head->right); return 1+(l>r?l:r); } struct tree* buildTree(int n,int maxn){ if(n>maxn){ return NULL; } struct tree* node = ( struct tree*)malloc(sizeof( struct tree)); node->value = n; node->left = buildTree(2*n+1,maxn); node->right = buildTree(2*n+2,maxn); return node; } #include"common.h" int main() { int n = 22; struct tree* root = buildTree(0,n); int height = getHeight(root); printf("deep %d \n",height); return 0; } 设maxn=22,输出5【图片】 练习2 typedef struct linknode{ data_t value; struct linknode* next; }linklist; linklist* create_linklist(data_t value){ linklist* res = (linklist*)malloc(sizeof(linklist)); res->next = NULL; res->value = value; } linklist* creatlistbylist(data_t* datas,int len){ if(len==0){ return NULL; } int idx = 1; linklist** curr = NULL; linklist* res = create_linklist(datas[0]); curr = &res; while(idxnext = create_linklist(datas[idx]); curr = &((*curr)->next); idx+=1; } return res; } void printList(linklist* list){ while(list){ printf("%d ",list->value); list = list->next; } printf("\n"); } void sortedBubble(linklist* list){ linklist** curr = &list; linklist* end = NULL; while((*curr)!=end){ while((*curr)->next!=end){ if((*curr)->value>(*curr)->next->value){ ((*curr)->value)^=(*curr)->next->value; (*curr)->next->value^=((*curr)->value); ((*curr)->value)^=(*curr)->next->value; } curr = &((*curr)->next); } end = *curr; curr = &list; } } int main() { int data[] = {2,5,7,10,22,578,321,9,3,5}; linklist* list = creatlistbylist(data,sizeof(data)/sizeof(int)); sortedBubble(list); printList(list); return 0; } 【图片】 练习3 #include"common.h" typedef struct linknode{ data_t value; struct linknode* next; }linklist; linklist* create_linklist(data_t value){ linklist* res = (linklist*)malloc(sizeof(linklist)); res->next = NULL; res->value = value; } linklist* creatlistbylist(data_t* datas,int len){ if(len==0){ return NULL; } int idx = 1; linklist** curr = NULL; linklist* res = create_linklist(datas[0]); curr = &res; while(idxnext = create_linklist(datas[idx]); curr = &((*curr)->next); idx+=1; } return res; } linklist* merge(linklist* list1,linklist* list2){ linklist* head = create_linklist(0); linklist* res = head; while(list1&&list2){ if(list1->valuevalue){ head->next = list1; list1 = list1->next; } else{ head->next = list2; list2 = list2->next; } head = head->next; } if(list1){ head->next = list1; } if(list2){ head->next = list2; } head = res; res = res->next; free(head); return res; } void printList(linklist* list){ while(list){ printf("%d ",list->value); list = list->next; } printf("\n"); } int main() { int data[] = {2,5,7,10,22,578,321,9,3,5}; int data1[] = {1,3,5,7,9}; int data2[] = {2,4,6,8,10}; linklist* list1 = creatlistbylist(data1,5); linklist* list2 = creatlistbylist(data2,5); linklist* newlist = merge(list1,list2); printList(newlist); return 0; } 【图片】 练习4【图片】




