用dev时链表输出出现乱码

CSDN问答 2021-12-30 07:19:51 阅读数:726

链表 出现 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://ask.csdn.net/questions/7616889