用dev時鏈錶輸出出現亂碼

CSDN問答 2021-12-30 07:20:07 阅读数:747

dev 出出
問題遇到的現象和發生背景
問題相關代碼,請勿粘貼截圖
#include<stdio.h>#include<stdlib.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 (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;
運行結果及報錯內容

img

 口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)//(scanf("%d", &n), n >= 0) { 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) { //print(head); 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"); //print(head);}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;}

版权声明:本文为[CSDN問答]所创,转载请带上原文链接,感谢。 https://primo.wiki/2021/12/202112271605320024.html