A2112999619 2022-08-19 22:29 采纳率: 100%
浏览 116
已结题

c语言链表运行的时候出现乱码

学链表的时候不知道为什么会有一行乱码
(学编程真快乐,就是搞不懂)
代码如下:
#include<stdio.h>
#include<stdlib.h>
char* gets(char* str);
typedef struct NODE {
char name[9];
int num;
struct NODE* next;
}node;

node* putin() {
int num = 1;
node* head ;
node* newnode;
head = (node*)malloc(sizeof(node));
node* nownode = head;

while (1)
{
    int a = 1;
    printf("数字:\n");
    scanf_s("%d", &num);
    if (num != 0) {
        newnode = (node*)malloc(sizeof(node));
        newnode->next = NULL;
        printf("字符:\n");
        gets(nownode->name,getchar());
        newnode->num = num;
        nownode->next = newnode;
        nownode = newnode;
    }
    else {
        goto a;
    }
}a:
printf("-----分割线-----\n");
printf("-----分割线-----\n");
return head;

}

void put(node* head)
{
node* temp = head->next;
while (temp != NULL) {
printf("数字:");
printf("%d\n", temp->num);
printf("字符:");
printf("%s\n", temp->name);
}
}

int main() {
node* head=NULL;
head = putin();
put(head);
return 0;
}

运行的时候就出现了乱码:

img


就是第一次输入的字符会在最后变成乱码输出

  • 写回答

2条回答 默认 最新

  • 滴水不穿石 2022-08-20 00:05
    关注

    修改过的加了备注,仅供参考!谢谢!

    img

    #include<stdio.h>
    #include<stdlib.h>
    char *gets(char *str);
    typedef struct NODE
    {
        char name[9];
        int num;
        struct NODE *next;
    } node;
    
    node *putin()
    {
        int num = 1;
        node *head;
        node *newnode;
        head = (node *) malloc(sizeof(node));
        node *nownode = head;
    
        while (1)
        {
            int a = 1;
            printf("数字:\n");
            scanf("%d", &num);//自行改回scanf_s
            getchar();
            if (num != 0)
            {
                newnode = (node *) malloc(sizeof(node));
                newnode->next = NULL;
                printf("字符:\n");
                scanf("%s",newnode->name);//被修改过//自行改回gets
                newnode->num = num;
                nownode->next = newnode;
                nownode = newnode;
            }
            else
            {
                goto a;
            }
        }
      a:
        printf("-----分割线-----\n");
        printf("-----分割线-----\n");
        return head;
    }
    
    void put(node * head)
    {
        node *temp = head->next;
        while (temp != NULL)
        {
            printf("数字:");
            printf("%d\n", temp->num);
            printf("字符:");
            printf("%s\n", temp->name);
            temp=temp->next;//这里被添加了一句
        }
    }
    
    int main()
    {
        node *head = NULL;
        head = putin();
        put(head);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月28日
  • 已采纳回答 8月20日
  • 修改了问题 8月19日
  • 创建了问题 8月19日

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄