
作业社区
探索学习新天地,共享知识资源!
jelasin 的学生作业:
练习 1 int depth_of_binary_tree(BTNode_t* root) { if (NULL == root) { return 0; } int left = depth_of_binary_tree(root->left); int right = depth_of_binary_tree(root->right); return left > right ? left + 1 : right + 1; } 练习 2 slist_node* find_by_idx_slist(const slist_head* head, int idx) { if (NULL == head->next || idx head->n) { return NULL; } slist_node* current = head->next; for (size_t i = 1; i next; } } return current; } int sort_linklist(slist_head* head) { slist_node* p = NULL; slist_node* q = NULL; if (NULL == head->next) { return -1; } #define SWAP(x, y) ({ \ typeof(*x) _x = *x; \ typeof(*y) _y = *y; \ (void) (&_x == &_y); \ *x = _y; *y = _x; \ }) printf("Sorting...\n"); bool flag; int last_idx = head->n; int flag_idx = 0; while (true) { flag = true; for (size_t j = 1; j data > q->data) { SWAP(&(p->data), &(q->data)); print_slist(head); flag_idx = j; flag = false; } } if (flag) { break; } last_idx = flag_idx; } return 0; } // main.c #include "SList.h" int test() { slist_head * l = create_empyt_slist(); if (NULL == l) { perror("Failed to create list"); return -1; } insert_head_slist(l, 1); insert_head_slist(l, 5); insert_head_slist(l, 3); insert_head_slist(l, 7); insert_head_slist(l, 9); insert_head_slist(l, 8); insert_head_slist(l, 2); insert_head_slist(l, 4); insert_head_slist(l, 6); print_slist(l); sort_linklist(l); print_slist(l); destyroy_slist(l); return 0; } int main(int argc, char const *argv[]) { return test(); } ➜ sList clang *.c -o slist ➜ sList ./slist 6 -> 4 -> 2 -> 8 -> 9 -> 7 -> 3 -> 5 -> 1 -> NULL Sorting... 4 -> 6 -> 2 -> 8 -> 9 -> 7 -> 3 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 9 -> 7 -> 3 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 9 -> 3 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 3 -> 9 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 3 -> 5 -> 9 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 3 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 8 -> 7 -> 3 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 8 -> 3 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 3 -> 8 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 3 -> 5 -> 8 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 3 -> 5 -> 1 -> 8 -> 9 -> NULL 2 -> 4 -> 6 -> 3 -> 7 -> 5 -> 1 -> 8 -> 9 -> NULL 2 -> 4 -> 6 -> 3 -> 5 -> 7 -> 1 -> 8 -> 9 -> NULL 2 -> 4 -> 6 -> 3 -> 5 -> 1 -> 7 -> 8 -> 9 -> NULL 2 -> 4 -> 3 -> 6 -> 5 -> 1 -> 7 -> 8 -> 9 -> NULL 2 -> 4 -> 3 -> 5 -> 6 -> 1 -> 7 -> 8 -> 9 -> NULL 2 -> 4 -> 3 -> 5 -> 1 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 3 -> 4 -> 5 -> 1 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 3 -> 4 -> 1 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 3 -> 1 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 1 2 3 4 5 6 7 8 9 练习3 int merge_sorted_linklist(slist_head* head_1, slist_head* head_2) { if (NULL == head_1 || NULL == head_2) { return -1; } slist_node* cmp_node = NULL; slist_node* insert_node = NULL; for (size_t i = 1; i n; i++) { insert_node = find_by_idx_slist(head_2, i); printf("Inserting %d\n", insert_node->data); for (size_t j = 1; j n; j++) { cmp_node = find_by_idx_slist(head_1, j); printf("Comparing %d and %d\n", cmp_node->data, insert_node->data); if (insert_node->data > cmp_node->data && j != head_1->n) { continue; } else if (unlikely(j == head_1->n)) { insert_tail_slist(head_1, insert_node->data); break; } else if (insert_node->data data) { if (unlikely(1 == j)) { insert_head_slist(head_1, insert_node->data); } else { slist_node* temp = cmp_node->next; slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = cmp_node->data; cmp_node->data = insert_node->data; cmp_node->next = new_node; new_node->next = temp; head_1->n++; print_slist(head_1); } break; } } } return 0; } ➜ sList ./slist 1 -> 3 -> 5 -> 7 -> 9 -> NULL 2 -> 4 -> 6 -> 8 -> 10 -> NULL Inserting 2 Comparing 1 and 2 Comparing 3 and 2 1 -> 2 -> 3 -> 5 -> 7 -> 9 -> NULL Inserting 4 Comparing 1 and 4 Comparing 2 and 4 Comparing 3 and 4 Comparing 5 and 4 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> 9 -> NULL Inserting 6 Comparing 1 and 6 Comparing 2 and 6 Comparing 3 and 6 Comparing 4 and 6 Comparing 5 and 6 Comparing 7 and 6 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> NULL Inserting 8 Comparing 1 and 8 Comparing 2 and 8 Comparing 3 and 8 Comparing 4 and 8 Comparing 5 and 8 Comparing 6 and 8 Comparing 7 and 8 Comparing 9 and 8 Inserting 10 Comparing 1 and 10 Comparing 2 and 10 Comparing 3 and 10 Comparing 4 and 10 Comparing 5 and 10 Comparing 6 and 10 Comparing 7 and 10 Comparing 9 and 10 Comparing 8 and 10 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> 8 -> 10 -> NULL 1 2 3 4 5 6 7 9 8 10 2 4 6 8 10 练习4【图片】




