亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

作業社區

探索學習新天地,共享知識資源!

0 提交作業
0 布置作業
0 滿分作業
得分 100
學習任務

cjozGV 的學生作業:

練習1: #include "stdio.h" #include "stdlib.h" typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }TreeNode; //計算二叉樹深度(遞歸方法) int maxDepth(TreeNode *root){ if (NULL == root){ return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1; } // 創建新節點 TreeNode* createNode(int val){ TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->val = val; newNode->left = NULL; newNode->right = NULL; return newNode; } // 遞歸釋放二叉樹內存 void freeTree(TreeNode* node) { if (node == NULL) return; freeTree(node->left); //釋放左子樹 freeTree(node->right); //釋放右子樹 free(node); //釋放當前節點 } int main(){ // 構建示例二叉樹: // 1 // / \ // 2 3 // / \ // 4 5 TreeNode* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); int depht = maxDepth(root); printf("二叉樹深度: %d\n",depht); freeTree(root); return 0; } 練習2: #include #include typedef struct ListNode { int val; struct ListNode *next; } ListNode; ListNode* sort_linklist(ListNode *head) { if (!head || !head->next) return head; ListNode *dummy = (ListNode*)malloc(sizeof(ListNode)); dummy->next = head; ListNode *prev = dummy; ListNode *current = head; int swapped; do { swapped = 0; prev = dummy; current = dummy->next; while (current && current->next) { if (current->val > current->next->val) { ListNode *temp = current->next; // temp 指向 B current->next = temp->next; // A 的 next 指向 C(跳過 B) temp->next = current; // B 的 next 指向 A(B 現在 A 前面) prev->next = temp; // 前驅節點的 next 指向 B(新的當前節點) prev = temp; // 前驅節點指向 B swapped = 1; } else { prev = current; current = current->next; } } } while (swapped); ListNode *new_head = dummy->next; free(dummy); return new_head; } ListNode* create_node(int val) { ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->val = val; node->next = NULL; return node; } void print_list(ListNode *head) { ListNode *cur = head; while (cur) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } int main() { ListNode *head = create_node(4); head->next = create_node(2); head->next->next = create_node(1); head->next->next->next = create_node(3); printf("排序前: "); print_list(head); head = sort_linklist(head); printf("排序后: "); print_list(head); ListNode *cur = head; while (cur) { ListNode *temp = cur; cur = cur->next; free(temp); } return 0; } 練習3: #include "stdlib.h" #include "stdio.h" typedef struct linknode{ int val; //數據域 struct linknode *next; //指向下一個節點的指針next } linknode_t; linknode_t* merge_lists(linknode_t* head1,linknode_t* head2){ //1. 創建虛擬頭節點(dummy) linknode_t dummy; //虛擬頭節點 linknode_t* tail = &dummy; //尾指針,初始指向虛擬頭節點 //2.新鏈表初始值為空 dummy.next = NULL; //虛擬頭節點的next初始化為NULL; //3.遍歷兩個鏈表,直到其中一個為空 while (head1 != NULL && head2 != NULL){ //4.比較兩個鏈表當前節點的值 if (head1->val < head2->val){ //5.將head1 當前節點追加到新鏈表末尾 tail->next = head1; //6.head1 移動到下一個節點 head1 = head1->next; } else { //7.將head2 當前節點追加到新鏈表末尾 tail->next = head2; //8.head2 移動到下一個節點 head2 = head2->next; } //9.tail 移動到新追加的節點(及新鏈表的末尾) tail = tail->next; } //10. 處理剩余節點(如果一個鏈表還未遍歷完) tail->next = (head1 != NULL) ? head1 : head2; //11. 返回合并后的鏈表頭節點(跳過虛擬頭節點) return dummy.next; } //2.創建鏈表 linknode_t* create_list(int* arr,int size){ if (size == 0) return 0; //如果數組為空直接返回 linknode_t* head =(linknode_t*)malloc(sizeof(linknode_t)); head->val = arr[0]; //2.給頭節點賦值數組的第一個元素 head->next = NULL; //3.頭節點的next指針初始化為NULL linknode_t* current = head; //4.創建一個指針current,初始指向頭節點 for (int i = 1;i < size;i++){ current->next = (linknode_t*)malloc(sizeof(linknode_t)); //6.為當前節點的next分配新節點 current = current->next; //7.current移動到新節點(準備給新節點賦值) current->val = arr[i]; //8.給新節點賦值為數組的當前元素 current->next = NULL; //9.新節點的next指針設為NULL(表示這是最后一個節點) } return head; } //打印鏈表 void print_list(linknode_t* head){ linknode_t* current = head; while (current != NULL){ printf("%d ",current->val); current = current->next; } printf("\n"); } //釋放鏈表內存 void free_list(linknode_t* head){ linknode_t* current = head; while (current != NULL){ linknode_t* temp = current; current = current->next; free(temp); } } int main() { int arr1[] = {1, 3, 5, 7, 9}; int arr2[] = {2, 4, 6, 8, 10}; linknode_t* head1 = create_list(arr1, sizeof(arr1) / sizeof(arr1[0])); linknode_t* head2 = create_list(arr2, sizeof(arr2) / sizeof(arr2[0])); printf("List 1: "); print_list(head1); printf("List 2: "); print_list(head2); linknode_t* merged_head = merge_lists(head1, head2); printf("Merged List: "); print_list(merged_head); free_list(merged_head); return 0; } 練習4: 1.確定根節點: 先序遍歷的第一個節點是根節點,即 A。 2.在中序遍歷中找到根節點: 中序遍歷中,根節點 A 將序列分為左子樹和右子樹: 左子樹部分:G D H B 右子樹部分:E I C F 3.遞歸構建左子樹: 左子樹的先序遍歷:B D G H(先序中根節點后的部分,長度與中序左子樹相同) 左子樹的中序遍歷:G D H B 根節點:B 中序中 B 分割為左:G D H,右:空 繼續遞歸: 左子樹的先序:D G H 左子樹的中序:G D H 根節點:D 中序中 D 分割為左:G,右:H 因此,D 的左子是 G,右子是 H。 所以 B 的左子是 D,右子為空(因為中序中 B 右側無節點)。 4.遞歸構建右子樹: 右子樹的先序遍歷:C E I F 右子樹的中序遍歷:E I C F 根節點:C 中序中 C 分割為左:E I,右:F 繼續遞歸: 左子樹的先序:E I 左子樹的中序:E I 根節點:E 中序中 E 分割為左:空,右:I 因此,E 的右子是 I。 右子樹的先序:F 右子樹的中序:F 根節點:F 所以 C 的左子是 E,右子是 F。 最終二叉樹結構 A / \ B C / / \ D E F / \ \ G H I

得分 100
學習任務

Felixxx 的學生作業:

write: #include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 100 int main(int argc,char *argv[]) { key_t key; int shmid,ret; void *addr = NULL; int fd; if(argc !=2) { perror("[ERROR] argc"); exit(EXIT_FAILURE); } key = ftok(PATHNAME,PRO_ID); if(key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } fd = open(argv[1],O_RDONLY); if(fd == -1) { perror("[ERROR] open():"); exit(EXIT_FAILURE); } struct stat st; if(fstat(fd,&st) == -1) { perror("[ERROR] fstat(): "); close(fd); exit(EXIT_FAILURE); } shmid = shmget(key,st.st_size,IPC_CREAT | 0666); if(shmid == -1) { perror("[ERROR] shmget(): "); close(fd); exit(EXIT_FAILURE); } char *shm = shmat(shmid,NULL,0); if(shm == (void *)-1) { perror("[ERROR] shmat(): "); close(fd); exit(EXIT_FAILURE); } ssize_t n = read(fd,shm,st.st_size); if(n == -1) { perror("[ERROR] read(): "); shmdt(shm); close(fd); exit(EXIT_FAILURE); } if(n != st.st_size) { fprintf(stderr,"讀取不完全"); } close(fd); printf("文件已讀取到共享內存:%d\n",shmid); shmdt(shm); return 0; } read: #include #include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 100 int main(int argc,char *argv[]) { key_t key; int shmid; if(argc != 2) { perror("[ERROR] argc:"); exit(EXIT_FAILURE); } key = ftok(PATHNAME,PRO_ID); if(key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } shmid = shmget(key,0,0666); if(shmid == -1) { perror("[ERROR] shmget(): "); exit(EXIT_FAILURE); } struct shmid_ds shm_info; if(shmctl(shmid,IPC_STAT,&shm_info) == -1) { perror("[ERROR] shmctl(): "); exit(EXIT_FAILURE); } char *shm = shmat(shmid,NULL,0); if(shm == (void *)-1) { perror("[ERROR] shmat(): "); exit(EXIT_FAILURE); } FILE *fp = fopen(argv[1],"wb"); if(fp == NULL) { perror("[ERROR] fopen(): "); shmdt(shm); exit(EXIT_FAILURE); } size_t written = fwrite(shm,1,shm_info.shm_segsz,fp); if(written != shm_info.shm_segsz) fprintf(stderr,"未完全寫入"); fclose(fp); printf("文件保存成功\n"); shmdt(shm); return 0; } 【圖片】

得分 100
學習任務

qq_康斯坦丁_0 的學生作業:

#include #include #include #include // 1. 定義 Person 結構體 struct Person { char name[50]; int age; }; // 線程函數:創建并返回一個 Person 結構體指針 void* create_person_in_thread(void* arg) { printf(“子線程: 開始執行…\n”); // 2. 在堆上動態分配內存給 Person 結構體 // 這是關鍵!如果在這里使用局部變量 (struct Person p;), // 那么當線程退出時,這塊內存會被銷毀,主線程將收到一個無效的指針。 struct Person* person_ptr = (struct Person*)malloc(sizeof(struct Person)); if (person_ptr == NULL) { perror("子線程: malloc 失敗"); return NULL; // 返回 NULL 表示失敗 } printf("子線程: 已在堆上為 Person 分配內存,地址: %p\n", person_ptr); // 3. 填充結構體數據 strcpy(person_ptr->name, "Alice"); person_ptr->age = 30; printf("子線程: 已填充數據 (Name: %s, Age: %d)\n", person_ptr->name, person_ptr->age); printf("子線程: 執行完畢,即將退出并返回結構體指針。\n"); // 4. 返回指向新創建的結構體的指針 // pthread_join 將會捕獲這個返回值 return (void*)person_ptr; } int main() { pthread_t tid; void* thread_return_value = NULL; int ret; printf("主線程: 準備創建子線程...\n"); // 5. 創建一個可結合的 (joinable) 線程 // 我們必須使用可結合的線程,因為我們需要調用 pthread_join 來獲取其返回值。 ret = pthread_create(&tid, NULL, create_person_in_thread, NULL); if (ret != 0) { perror("主線程: 創建線程失敗"); exit(EXIT_FAILURE); } printf("主線程: 線程已創建,現在等待它完成...\n"); // 6. 等待子線程結束,并接收它返回的指針 // 第二個參數是 void** 類型,它會接收線程函數返回的 void* 指針。 ret = pthread_join(tid, &thread_return_value); if (ret != 0) { perror("主線程: pthread_join 失敗"); exit(EXIT_FAILURE); } printf("主線程: 子線程已結束。\n"); // 7. 檢查返回值并進行處理 if (thread_return_value != NULL) { // 將 void* 指針強制轉換回正確的類型 struct Person* received_person = (struct Person*)thread_return_value; printf("\n--- 主線程打印接收到的數據 ---\n"); printf("接收到的 Person 地址: %p\n", received_person); printf("姓名: %s\n", received_person->name); printf("年齡: %d\n", received_person->age); printf("------------------------------\n\n"); // 8. 釋放內存 // 由于內存是在子線程中通過 malloc 分配的, // 主線程在使用完畢后有責任釋放它,以防止內存泄漏。 printf("主線程: 打印完畢,釋放子線程分配的內存。\n"); free(received_person); received_person = NULL; // 好習慣:防止懸掛指針 } else { printf("主線程: 從子線程接收到 NULL,可能發生了錯誤。\n"); } printf("主線程: 任務完成,退出。\n"); return 0; }

微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號