秃头_酱 2021-12-27 16:00 采纳率: 50%
浏览 79
已结题

用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 口 X
1faa
输出链表请扣1
链表插入请扣2
链表查找请扣3
  表删除请扣4
输入-1结束
  后输入学号,如果只需输出链表学号可随意输入1
10
11098288
 STRINGDefault1
aa
Process exited after 24.79 seconds with return value 0
青按任意键继续
我的解答思路和尝试过的方法
我想要达到的结果

把图上的乱码去掉

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-12-28 09:12
    关注

    修改如下,供参考:

    #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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月6日
  • 已采纳回答 12月29日
  • 创建了问题 12月27日

悬赏问题

  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方
  • ¥15 关于生产日期批次退货退款,库存回退的问题
  • ¥15 手机应用的时间可以修改吗
  • ¥15 docker 运行OPEN-webui异常