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

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

作業社區

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

0 提交作業
0 布置作業
0 滿分作業
得分 100
討論題

RX0_UNICORN 的學生作業:

// joseph.h #ifndef __JOSEPH_H__ #define __JOSEPH_H__ #include #include typedef int datatype_t; typedef struct node { datatype_t data; struct node *next; }loopnode_t; extern loopnode_t *create_circular_linklist(int n); extern void josephus_problem(int n, int k, int m); #endif // joseph.c #include "joseph.h" // 創建循環鏈表 loopnode_t *create_circular_linklist(int n) { if (n data = 1; loopnode_t *current = head; for (int i = 2; i data = i; current->next = newNode; current = newNode; } // 將鏈表首尾相連形成循環 current->next = head; return head; } // 解決約瑟夫問題 void josephus_problem(int n, int k, int m) { // 創建循環鏈表 loopnode_t *head = createCircularLinkedList(n); if (head == NULL) return; // 移動到第k個節點 loopnode_t *current = head; loopnode_t *prev = NULL; // 找到第k個節點 for (int i = 1; i < k; i++) { prev = current; current = current->next; } while (current->next != current) { // 數m-1次,因為當前節點從1開始計數 for (int i = 1; i < m; i++) { prev = current; current = current->next; } // 刪除當前節點 prev->next = current->next; printf("%d ", current->data); // 如果不是最后一個節點,打印逗號分隔 if (current->next != prev->next) { printf(" "); } loopnode_t *temp = current; current = current->next; free(temp); } // 處理最后一個節點 printf("%d\n", current->data); free(current); } // main.c #include "joseph.h" int main(int argc, const char *argv[]) { int n, k, m; printf("please input n k m : "); scanf("%d%d%d", &n, &k, &m); printf("n = %d, k = %d, m = %d 時的出列序列:\n", n, k, m); josephus_problem(n, k, m); return 0; } 【圖片】

得分 100
討論題

RX0_UNICORN 的學生作業:

// linklist.h #ifndef __LINKLIST_H__ #define __LINKLIST_H__ #include #include #include typedef int datatype_t; typedef struct node { datatype_t data; // 數據域 保存有效的數據 struct node *next; // 指針域 保存下一個結點地址 }linklist_t; extern linklist_t *create_empty_linklist(); extern void insert_head_linklist(linklist_t *head, datatype_t data); extern void insert_tail_linklist(linklist_t *head, datatype_t data); extern void insert_order_linklist(linklist_t *head, datatype_t data); extern void print_data_linklist(linklist_t *head); #endif // linlist.c #include "linklist.h" // 創建一個新的鏈表---為頭結點分配堆區空間 linklist_t *create_empty_linklist() { // 為頭結點分配堆區空間 linklist_t *head = NULL; head = (linklist_t *)malloc(sizeof(linklist_t)); if(NULL == head) { printf("malloc is fail!\n"); return NULL; } memset(head, 0, sizeof(linklist_t)); head->next = NULL; // 該步驟可省略 return head; } // 頭插法,每次都在頭結點后插入數據 // [特點:插入順序和輸出順序是相反的] void insert_head_linklist(linklist_t *head, datatype_t data) { // 分配空間 linklist_t *temp = NULL; temp = (linklist_t *)malloc(sizeof(linklist_t)); // 將 data 存入 temp 的數據域 temp->data = data; // 將 temp 插入 head 后面 temp->next = head->next; head->next = temp; return; } // 尾插法 void insert_tail_linklist(linklist_t *head, datatype_t data) { // 分配空間 linklist_t *temp = NULL; temp = (linklist_t *)malloc(sizeof(linklist_t)); // 將 data 存入 temp 的數據域 temp->data = data; linklist_t *p = head; // 遍歷循環找到尾結點 while(p->next != NULL) { p = p->next; } // 將 temp 插入 p 后面 temp->next = p->next; p->next = temp; return; } // 有序法 void insert_order_linklist(linklist_t *head, datatype_t data) { linklist_t *temp = NULL; temp = (linklist_t *)malloc(sizeof(linklist_t)); temp->data = data; linklist_t *p = head; while(p->next != NULL && data < p->next->data) { p = p->next; } temp->next = p->next; p->next = temp; return; } // 輸出鏈表中的數據 void print_data_linklist(linklist_t *head) { linklist_t *p = head; while(p->next != NULL) // next 為 NULL 時停止打印 { printf("%d ", p->next->data); p = p->next; } printf("\n"); return; } // main.c #include "linklist.h" int main(int argc, const char *argv[]) { datatype_t data = 0; int data_num = 0, i = 0; linklist_t *head = create_empty_linklist(); printf("please input you want save data num : "); scanf("%d", &data_num); printf("please input %d data : ", data_num); for(i = 0; i < data_num; i++) { scanf("%d", &data); // insert_head_linklist(head, data); // insert_tail_linklist(head, data); insert_order_linklist(head, data); } print_data_linklist(head); return 0; } 【圖片】

得分 100
討論題
得分 100
學習任務

RX0_UNICORN 的學生作業:

// seqlist.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include #include #include #define MAX 10 struct student { char name[20]; int id; int age; }; typedef struct student datatype_t; typedef struct { datatype_t buf[MAX]; int n; }seqlist_t; seqlist_t *create_empty_seqlist(); int is_full_seqlist(seqlist_t *l); void insert_data_seqlist(seqlist_t *l, datatype_t data); void print_data_seqlist(seqlist_t *l); #endif // seqlist.c #include "seqlist.h" seqlist_t *create_empty_seqlist() { seqlist_t *l = (seqlist_t *)malloc(sizeof(seqlist_t)); if(NULL == l) { printf("malloc is fail!"); return NULL; } memset(l, 0, sizeof(seqlist_t)); l -> n = 0; return l; } int is_full_seqlist(seqlist_t *l) { return l->n < MAX ? 1 : 0; } void insert_data_seqlist(seqlist_t *l, datatype_t data) { l->buf[l->n] = data; l->n++; } void print_data_seqlist(seqlist_t *l) { printf("NAME\tID\tAGE\n"); printf("---------------------------------\n"); for(int i = 0; i < l->n; i++) { printf("%s\t%d\t%d\n", l->buf[i].name, l->buf[i].id, l->buf[i].age); printf("---------------------------------\n"); } } // main.c #include "seqlist.h" int main(int argc, const char *argv[]) { seqlist_t *l = create_empty_seqlist(); while(is_full_seqlist(l)) { printf("Please input no more than %d student info[NAME ID AGE] : ", MAX - l->n); datatype_t std; scanf("%s%d%d", std.name, &(std.id), &(std.age)); insert_data_seqlist(l, std); } print_data_seqlist(l); free(l); l = NULL; return 0; } 【圖片】

得分 100
討論題

cjozGV 的學生作業:

#include "stdio.h" #include "stdlib.h" #include "string.h" typedef int datatype_t; //定義鏈表節點結構 typedef struct Node{ datatype_t value; //節點的值,代表入的編號 struct Node* next; } Node; //創建包含n個節點的循環鏈表 Node* create_Circular_LinkedList(int n) { Node* head = NULL; // 鏈表頭指針 Node* prev = NULL; // 用于跟蹤前一個節點 for (int i = 1; i value = i; // 設置節點值 newNode->next = NULL; if (head == NULL) { head = newNode; // 第一個節點作為頭節點 } else { prev->next = newNode; // 將前一個節點的next指向新節點 } prev = newNode; // 更新前一個節點為當前新節點 } if (prev != NULL) { prev->next = head; // 將最后一個節點的next指向頭節點,形成循環 } return head; // 返回頭節點 } void josephus(int n, int k, int m) { Node* head = create_Circular_LinkedList(n); // 創建循環鏈表 Node* current = head; // 當前節點指針 Node* prev = NULL; // 當前節點的前一個節點指針 // 移動到起始點:第k個節點 for (int i = 1; i < k; i++) { prev = current; current = current->next; } printf("出列順序: "); // 當鏈表中不止一個節點時循環 while (current->next != current) { // 計數m-1次,找到要刪除節點的前一個節點 for (int i = 1; i < m; i++) { prev = current; current = current->next; } // 輸出當前要刪除的節點值 printf("%d ", current->value); // 刪除當前節點:將前一個節點的next指向當前節點的下一個節點 prev->next = current->next; // 保存當前節點以便釋放內存 Node* temp = current; // 移動到下一個節點 current = current->next; // 釋放當前節點的內存 free(temp); } // 輸出最后一個節點 printf("%d\n", current->value); // 釋放最后一個節點的內存 free(current); } int main() { int n = 8; // 總人數 int k = 3; // 起始位置 int m = 4; // 計數到m的人出列 josephus(n, k, m); // 調用約瑟夫函數 return 0; }

得分 100
學習任務

cjozGV 的學生作業:

#include "stdio.h" #include "stdlib.h" #include "string.h" typedef int datatype_t; //定義鏈表節點結構 typedef struct Node{ datatype_t value; //節點的值,代表入的編號 struct Node* next; } Node; //創建包含n個節點的循環鏈表 Node* create_Circular_LinkedList(int n) { Node* head = NULL; // 鏈表頭指針 Node* prev = NULL; // 用于跟蹤前一個節點 for (int i = 1; i value = i; // 設置節點值 newNode->next = NULL; if (head == NULL) { head = newNode; // 第一個節點作為頭節點 } else { prev->next = newNode; // 將前一個節點的next指向新節點 } prev = newNode; // 更新前一個節點為當前新節點 } if (prev != NULL) { prev->next = head; // 將最后一個節點的next指向頭節點,形成循環 } return head; // 返回頭節點 } void josephus(int n, int k, int m) { Node* head = create_Circular_LinkedList(n); // 創建循環鏈表 Node* current = head; // 當前節點指針 Node* prev = NULL; // 當前節點的前一個節點指針 // 移動到起始點:第k個節點 for (int i = 1; i < k; i++) { prev = current; current = current->next; } printf("出列順序: "); // 當鏈表中不止一個節點時循環 while (current->next != current) { // 計數m-1次,找到要刪除節點的前一個節點 for (int i = 1; i < m; i++) { prev = current; current = current->next; } // 輸出當前要刪除的節點值 printf("%d ", current->value); // 刪除當前節點:將前一個節點的next指向當前節點的下一個節點 prev->next = current->next; // 保存當前節點以便釋放內存 Node* temp = current; // 移動到下一個節點 current = current->next; // 釋放當前節點的內存 free(temp); } // 輸出最后一個節點 printf("%d\n", current->value); // 釋放最后一個節點的內存 free(current); } int main() { int n = 8; // 總人數 int k = 3; // 起始位置 int m = 4; // 計數到m的人出列 josephus(n, k, m); // 調用約瑟夫函數 return 0; }

微信客服

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

幫助反饋 APP下載

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

公眾號

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