第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

作業(yè)社區(qū)

探索學(xué)習(xí)新天地,共享知識資源!

0 提交作業(yè)
0 布置作業(yè)
0 滿分作業(yè)
得分 100
學(xué)習(xí)任務(wù)

cjozGV 的學(xué)生作業(yè):

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

得分 100
學(xué)習(xí)任務(wù)

Felixxx 的學(xué)生作業(yè):

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("文件已讀取到共享內(nèi)存:%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; } 【圖片】

首頁上一頁1234567下一頁尾頁
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號