
作業(yè)社區(qū)
探索學(xué)習(xí)新天地,共享知識(shí)資源!
浪潮君 的學(xué)生作業(yè):
#include // 引入標(biāo)準(zhǔn)輸入輸出庫(kù),提供 printf 函數(shù) #include // 引入標(biāo)準(zhǔn)庫(kù),提供 malloc、free、exit 等內(nèi)存函數(shù) // 定義鏈表節(jié)點(diǎn)結(jié)構(gòu)體 typedef struct Node { int data; // 數(shù)據(jù)域:存儲(chǔ)節(jié)點(diǎn)的整數(shù)值 struct Node *next; // 指針域:指向下一個(gè)節(jié)點(diǎn)的指針 } Node; // 創(chuàng)建一個(gè)新節(jié)點(diǎn),封裝內(nèi)存分配和初始化過(guò)程 Node *create_node(int value) { Node *new_node = (Node *)malloc(sizeof(Node)); // 分配一個(gè)節(jié)點(diǎn)大小的內(nèi)存 if (!new_node) { perror("內(nèi)存分配失敗"); // 如果分配失敗,輸出錯(cuò)誤信息 exit(EXIT_FAILURE); // 終止程序 } new_node->data = value; // 設(shè)置節(jié)點(diǎn)的數(shù)據(jù) new_node->next = NULL; // 初始時(shí)不指向其他節(jié)點(diǎn) return new_node; // 返回新節(jié)點(diǎn)地址 } // 從大到小順序插入一個(gè)節(jié)點(diǎn)(插入排序式建表) void insert_descending(Node **head, int value) { Node *new_node = create_node(value); // 創(chuàng)建新節(jié)點(diǎn) // 情況 1:鏈表為空,或者新值比頭節(jié)點(diǎn)大 → 插入頭部 if (*head == NULL || value > (*head)->data) { new_node->next = *head; // 新節(jié)點(diǎn)指向原頭節(jié)點(diǎn) *head = new_node; // 更新頭指針為新節(jié)點(diǎn) return; } // 情況 2:在鏈表中找到合適位置插入(保持從大到?。? Node *current = *head; while (current->next != NULL && current->next->data >= value) { current = current->next; // 向后遍歷直到找到插入點(diǎn) } // 插入新節(jié)點(diǎn):new_node 插入在 current 后面 new_node->next = current->next; current->next = new_node; } // 打印整個(gè)鏈表內(nèi)容 void print_list(Node *head) { printf("鏈表內(nèi)容:"); Node *current = head; while (current != NULL) { printf("%d ", current->data); // 打印當(dāng)前節(jié)點(diǎn)的值 current = current->next; // 移動(dòng)到下一個(gè)節(jié)點(diǎn) } printf("\n"); } // 刪除鏈表中所有值等于 target 的節(jié)點(diǎn) void delete_value(Node **head, int target) { Node *current = *head; // 當(dāng)前節(jié)點(diǎn),從頭開(kāi)始 Node *prev = NULL; // 前一個(gè)節(jié)點(diǎn),初始為 NULL while (current != NULL) { if (current->data == target) { // 找到目標(biāo)節(jié)點(diǎn) Node *to_delete = current; // 保存待刪除節(jié)點(diǎn) if (prev == NULL) { // 如果刪除的是頭節(jié)點(diǎn),更新 head 指針 *head = current->next; current = *head; // 當(dāng)前節(jié)點(diǎn)后移到新頭節(jié)點(diǎn) } else { // 刪除的是中間或尾部節(jié)點(diǎn) prev->next = current->next; current = current->next; // 當(dāng)前節(jié)點(diǎn)后移 } free(to_delete); // 釋放刪除的節(jié)點(diǎn)內(nèi)存 } else { // 當(dāng)前不是目標(biāo)節(jié)點(diǎn),向后遍歷 prev = current; current = current->next; } } } // 釋放整個(gè)鏈表占用的內(nèi)存 void free_list(Node *head) { Node *tmp; while (head != NULL) { tmp = head; // 暫存當(dāng)前節(jié)點(diǎn) head = head->next; // 移動(dòng)到下一個(gè)節(jié)點(diǎn) free(tmp); // 釋放當(dāng)前節(jié)點(diǎn)內(nèi)存 } } // 主函數(shù):程序入口 int main(void) { // 初始化無(wú)序數(shù)組 int values[] = {1, 5, 3, 7, 9, 5, 8, 5, 3}; int n = sizeof(values) / sizeof(values[0]); // 計(jì)算數(shù)組元素個(gè)數(shù) Node *head = NULL; // 初始化鏈表頭指針為空 // 將數(shù)組中所有元素按從大到小順序插入鏈表 for (int i = 0; i < n; i++) { insert_descending(&head, values[i]); } printf("初始有序鏈表(從大到?。篭n"); print_list(head); // 輸出插入后的鏈表內(nèi)容 // 刪除鏈表中所有值為 3 的節(jié)點(diǎn) delete_value(&head, 3); printf("刪除所有值為 3 后的鏈表:\n"); print_list(head); // 釋放鏈表占用的所有內(nèi)存 free_list(head); return 0; // 程序正常結(jié)束 }





浪潮君 的學(xué)生作業(yè):
#include // 標(biāo)準(zhǔn)輸入輸出庫(kù),提供 printf 等函數(shù) #include // 標(biāo)準(zhǔn)庫(kù),提供 malloc、free、exit 等函數(shù) // 定義鏈表節(jié)點(diǎn)結(jié)構(gòu)體 typedef struct Node { int data; // 節(jié)點(diǎn)存儲(chǔ)的整數(shù)數(shù)據(jù) struct Node *next; // 指向下一個(gè)節(jié)點(diǎn)的指針 } Node; // 創(chuàng)建一個(gè)新節(jié)點(diǎn),傳入數(shù)據(jù)值,返回節(jié)點(diǎn)指針 Node *create_node(int value) { Node *new_node = (Node *)malloc(sizeof(Node)); // 為新節(jié)點(diǎn)分配內(nèi)存空間 if (!new_node) { // 判斷內(nèi)存分配是否成功 perror("內(nèi)存分配失敗"); // 輸出錯(cuò)誤信息 exit(EXIT_FAILURE); // 退出程序,返回錯(cuò)誤碼 } new_node->data = value; // 設(shè)置節(jié)點(diǎn)數(shù)據(jù)域 new_node->next = NULL; // 初始化節(jié)點(diǎn)的下一個(gè)指針為 NULL(尾節(jié)點(diǎn)) return new_node; // 返回新節(jié)點(diǎn)指針 } // 尾插法:將節(jié)點(diǎn)插入鏈表尾部,保持元素插入順序 void append_node(Node **head, int value) { Node *new_node = create_node(value); // 創(chuàng)建新節(jié)點(diǎn) if (*head == NULL) { // 如果鏈表為空,新節(jié)點(diǎn)即為頭節(jié)點(diǎn) *head = new_node; return; } Node *current = *head; // 從頭節(jié)點(diǎn)開(kāi)始遍歷 while (current->next != NULL) { // 遍歷找到最后一個(gè)節(jié)點(diǎn) current = current->next; } current->next = new_node; // 將新節(jié)點(diǎn)掛到尾節(jié)點(diǎn)后面 } // 反轉(zhuǎn)鏈表,改變鏈表節(jié)點(diǎn)指向,實(shí)現(xiàn)逆序 void reverse_list(Node **head) { Node *prev = NULL; // 記錄當(dāng)前節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn),初始化為 NULL Node *curr = *head; // 當(dāng)前遍歷的節(jié)點(diǎn),初始化為頭節(jié)點(diǎn) Node *next = NULL; // 臨時(shí)指針,保存當(dāng)前節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)地址 while (curr != NULL) { // 遍歷整個(gè)鏈表 next = curr->next; // 先保存當(dāng)前節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn) curr->next = prev; // 當(dāng)前節(jié)點(diǎn)的 next 指向前一個(gè)節(jié)點(diǎn),實(shí)現(xiàn)反轉(zhuǎn) prev = curr; // prev 向后移動(dòng),變成當(dāng)前節(jié)點(diǎn) curr = next; // curr 向后移動(dòng),變成原來(lái)下一個(gè)節(jié)點(diǎn) } *head = prev; // 最后 prev 指向新的頭節(jié)點(diǎn),更新鏈表頭指針 } // 遍歷鏈表并打印所有節(jié)點(diǎn)數(shù)據(jù) void print_list(Node *head) { printf("鏈表內(nèi)容:"); Node *current = head; // 臨時(shí)指針,從頭節(jié)點(diǎn)開(kāi)始遍歷 while (current != NULL) { printf("%d ", current->data); // 打印當(dāng)前節(jié)點(diǎn)的數(shù)據(jù) current = current->next; // 移動(dòng)到下一個(gè)節(jié)點(diǎn) } printf("\n"); // 輸出換行 } // 釋放鏈表占用的動(dòng)態(tài)內(nèi)存,防止內(nèi)存泄漏 void free_list(Node *head) { Node *tmp; while (head != NULL) { tmp = head; // 保存當(dāng)前節(jié)點(diǎn)指針 head = head->next; // 移動(dòng)到下一個(gè)節(jié)點(diǎn) free(tmp); // 釋放當(dāng)前節(jié)點(diǎn)內(nèi)存 } } // 主函數(shù),程序入口 int main(void) { int values[] = {1, 5, 3, 7, 9}; // 初始化數(shù)據(jù)數(shù)組 int n = sizeof(values) / sizeof(values[0]); // 計(jì)算數(shù)組元素?cái)?shù)量 Node *head = NULL; // 初始化鏈表頭指針為空 // 依次將數(shù)組中的元素插入鏈表尾部 for (int i = 0; i < n; i++) { append_node(&head, values[i]); } printf("原始鏈表:\n"); print_list(head); // 打印鏈表內(nèi)容(順序) reverse_list(&head); // 反轉(zhuǎn)鏈表,實(shí)現(xiàn)逆序 printf("逆序后鏈表:\n"); print_list(head); // 打印逆序后的鏈表內(nèi)容 free_list(head); // 釋放鏈表內(nèi)存,避免泄漏 return 0; // 程序正常結(jié)束 }




