問題遇到的現象和發生背景
問題相關代碼,請勿粘貼截圖
typedef struct link_list{ int num; char sex[20]; char name[30]; struct link_list* next;}STU;void creat(STU* head){ int n; STU* p = NULL, * q = head; while (scanf("%d", &n), n >= 0) { p = (STU*)malloc(sizeof(STU)); p->num = n; scanf("%s%s", p->sex, p->name); q->next = p; q = p; } q->next = NULL;}void print(STU* head){ while (head != NULL) { printf("%d\n%s\n%s", head->num, head->sex, head->name); head = head->next; }}STU* search(STU* head, int n){ while (head != NULL) { if (head->num == n) { print(head); return head; } head = head->next; } return NULL;}void charu(STU* head, int n){ STU* x = (STU*)malloc(sizeof(STU)); scanf("%d %s %s", &x->num, x->sex, x->name); STU* b = head->next; while (b->num != n) { b = b->next; head = b; } x->next = b; head->next = x;}void deleted(STU* head, int n){ STU* p = head->next; while (p && p->num != n) { head = p, p = p->next; } if (p) { head->next = p->next; free(p); } print(head);}int main(){ STU* head = (STU*)malloc(sizeof(STU)); creat(head); printf("輸出鏈錶請扣1\n鏈錶插入請扣2\n鏈錶查找請扣3\n鏈錶删除請扣4\n輸入-1結束\n然後輸入學號,如果只需輸出鏈錶學號可隨意輸入"); int requst, n; scanf("%d%d", &requst, &n); switch (requst) { case 1:print(head); break; case 2:charu(head, n),print(head); break; case 3:search(head, n); break; case 4:deleted(head, n); break;
運行結果及報錯內容

口C:Users186159Documents\Untitled1.exe 口 X1faa輸出鏈錶請扣1鏈錶插入請扣2鏈錶查找請扣3 錶删除請扣4輸入-1結束 後輸入學號,如果只需輸出鏈錶學號可隨意輸入11011098288 STRINGDefault1aaProcess exited after 24.79 seconds with return value 0青按任意鍵繼續
我的解答思路和嘗試過的方法
我想要達到的結果
把圖上的亂碼去掉
修改如下,供參考:
#include<stdio.h>#include<stdlib.h>#include <cstddef>#include <windows.h>typedef struct link_list{ int num; char sex[20]; char name[30]; struct link_list* next;}STU;void creat(STU* head) { int n; STU* p = NULL, * q = head; while (1) { printf("請輸入學號(-1:結束輸入):\n"); scanf("%d", &n); if (n < 0) break; p = (STU*)malloc(sizeof(STU)); p->num = n; printf("請輸入性別 姓名:\n"); scanf("%s%s", p->sex, p->name); q->next = p; q = p; } q->next = NULL;}void print(STU* head){ head = head->next; while (head != NULL) { printf("\n%d %s %s\n", head->num, head->sex, head->name); head = head->next; }}STU* search(STU* head, int n){ head = head->next; while (head != NULL) { if (head->num == n) { printf("\n%d %s %s\n", head->num, head->sex, head->name); return head; } head = head->next; } if (!head) printf("未找到學號為:%d 的記錄!\n", n); return NULL;}void charu(STU* head, int n){ STU* b = head->next; while (b && b->num != n) { head = b; b = b->next; } if (!b) printf("未找到待插入的學號!\n"); else{ STU* x = (STU*)malloc(sizeof(STU)); printf("請輸入新插入的學號 性別 姓名:\n"); scanf("%d %s %s", &x->num, x->sex, x->name); x->next = b; head->next = x; }}void deleted(STU* head, int n){ STU* p = head->next; while (p && p->num != n) { head = p, p = p->next; } if (p) { head->next = p->next; free(p); printf("删除成功!\n"); } else printf("未找到删除的學號記錄!\n"); }int main(){ STU* head = (STU*)malloc(sizeof(STU)); creat(head); int requst = 1, n; while(requst){ printf("輸出鏈錶請扣1\n鏈錶插入請扣2\n鏈錶查找請扣3\n鏈錶删除請扣4\n輸入-1結束\n然後輸入學號,如果只需輸出鏈錶學號可隨意輸入"); scanf("%d%d", &requst, &n); switch (requst) { case 1:print(head); break; case 2:charu(head, n), print(head); break; case 3:search(head, n); break; case 4:deleted(head, n); break; default:requst = 0; break; } system("pause"); system("cls"); } return 0;}