为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

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; }

得分 100
学习任务

Felixxx 的学生作业:

#include #include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 10 #define MSG_TYPE_A 100 #define MSG_TYPE_B 200 #define MSG_SZ 64 struct msgbuf { long mtype; char mtext[MSG_SZ]; }; void main() { pid_t cpid; key_t key; int msgid,retA,retB; char buffer[MSG_SZ]; ssize_t rbytes; struct msgbuf msgA; struct msgbuf msgB; struct msgbuf rcv_msg; key = ftok(PATHNAME,PRO_ID); if(key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } msgid = msgget(key,IPC_CREAT | 0666); if(msgid == -1) { perror("[ERROR] msgget(): "); exit(EXIT_FAILURE); } printf("msgid is %d \n",msgid); cpid = fork(); if(cpid == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if(cpid == 0) { while(1) { rbytes = msgrcv(msgid,(void *)&rcv_msg,MSG_SZ,MSG_TYPE_A,0); if(rbytes == -1) { perror("[ERROR] msgcv():"); exit(EXIT_FAILURE); } printf("进程A mtype: %ld\n",rcv_msg.mtype); printf("进程A mtext: %s\n",rcv_msg.mtext); } } else if(cpid > 0) { cpid = fork(); if(cpid == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if(cpid == 0) { while(1) { rbytes = msgrcv(msgid,(void *)&rcv_msg,MSG_SZ,MSG_TYPE_B,0); if(rbytes == -1) { perror("[ERROR] msgcv():"); exit(EXIT_FAILURE); } printf("进程B mtype: %ld\n",rcv_msg.mtype); printf("进程B mtext: %s\n",rcv_msg.mtext); } } else if(cpid > 0) { while(1) { fgets(buffer,sizeof(buffer),stdin); buffer[strcspn(buffer,"\n")] = '\0'; if(strcmp(buffer,"quit") == 0) exit(EXIT_SUCCESS); msgA.mtype = MSG_TYPE_A; msgB.mtype = MSG_TYPE_B; strcpy(msgA.mtext,buffer); strcpy(msgB.mtext,buffer); retA = msgsnd(msgid,(const void *)&msgA,strlen(msgA.mtext)+1,0); retB = msgsnd(msgid,(const void *)&msgB,strlen(msgB.mtext)+1,0); if(retA == -1 || retB == -1) { perror("[ERROR] msgsnd(): "); exit(EXIT_FAILURE); } } } } }

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号