写了一个简单的链表函数,但是没输出,改了一下红色的i的类型为int就有输出了,一直搞不明白怎么回事

兔纸暴打鹰酱 2018-05-20 02:26:02
#include<stdio.h>
#include<stdlib.h>

struct link
{
char name[20];
int num;
struct link *next;
};

struct link* creatLink()
{
struct link*p;
struct link*q;
struct link*head;
char i;
p = (struct link*)malloc(sizeof(struct link));
printf("please enter the name and number\n");
scanf("%s %d",p->name,&(p->num));

head = p;
q = head;
p->next = NULL;
printf("if you want to end up entering data,please enter 1,else enter 2\n");
scanf("%d",&i);
while(i==1)
{
p = (struct link*)malloc(sizeof(struct link));
printf("please enter the name and number\n");
scanf("%s %d",p->name,&(p->num));

q->next = p;
q = p;
p->next = NULL;
printf("if you want to continue enter data,please enter 1,else enter 2\n");
scanf("%d",&i);

}
return head;

}

void printLink(struct link* head)
{
struct link* p;
p = head;
while(p!=NULL)
{
printf("%s\t",p->name);
printf("%d\n",p->num);
p= p->next;
}
}
int main()
{
struct link* head;
head =creatLink();
printLink(head);
return 0;
}
...全文
859 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-05-21
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

struct link
{
    char name[20];
    int num;
    struct link *next;
};

struct link* creatLink()
{
    struct link*p;
    struct link*q;
    struct link*head;
    int i;
    p = (struct link*)malloc(sizeof(struct link));
    printf("please enter the name and number\n");
    scanf("%s %d",p->name,&(p->num));

    head = p;
    q = head;
    p->next = NULL;
    printf("if you want to end up entering data,please enter 1,else enter 2\n");
    scanf("%d",&i);
    while(i==1)
    {
        p = (struct link*)malloc(sizeof(struct link));
        printf("please enter the name and number\n");
        scanf("%s %d",p->name,&(p->num));

        q->next = p;
        q = p;
        printf("if you want to continue enter data,please enter 1,else enter 2\n");
        scanf("%d",&i);

    }
    p->next = NULL;
    return head;

}

void printLink(struct link* head)
{
    struct link* p;
    p = head;
    while(p)
    {
        printf("%s\t",p->name);
        printf("%d\n",p->num);
        p= p->next;
    }
}
int main()
{
    struct link* head;
    head =creatLink();
    printLink(head);
    return 0;
}
参考一下吧 没有输出,那么是程序在while中死循环了吗?输入需要和格式相匹配
赵4老师 2018-05-21
  • 打赏
  • 举报
回复
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
qq_35563964 2018-05-20
  • 打赏
  • 举报
回复
因为在红色语句的子函数的第二个scanf是以%d的形式输入的,把char改成int数据类型就相符合了
iswjh 2018-05-20
  • 打赏
  • 举报
回复
不是有标准库吗?为什么要自己写?
dark9527 2018-05-20
  • 打赏
  • 举报
回复
我在vs2013中调试的时候会出现Run-Time Check Failure #2- Stack around the variable "i" was corrupted .
可能是i只占一个字节的内存,但是你用%d接收的时候,其他三个字节的内存也被影响了,但是用vs接着输出时是对的。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧