69,372
社区成员
发帖
与我相关
我的任务
分享
#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中死循环了吗?输入需要和格式相匹配