运行后第一个name不能输入数据(直接跳过了),还有输出的结果也不对(number>2)时
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct linkinfo)
struct linkinfo
{
char name[20];
char place[128];
char birthday[10];
char telephone[16];
struct linkinfo *next;
};
struct linkinfo *create(int number)
{
struct linkinfo *new,*head = NULL,*tail = NULL;
int i,count = 0;
for(i=0; i<number; i++)
{
new = (struct linkinfo *)malloc(LEN);
printf("name:");
gets(new->name); //改为scanf("%s",new->name);就行,但是scanf不能输入空格和空数据
printf("\n%s",new->name);
printf("place:");
scanf("%s",new->place);
printf("birthday:");
scanf("%s",new->birthday);
printf("tel:");
scanf("%s",new->telephone);
count++;
new->next = NULL;
if(count <= 1)
{
head = new;
}
else
{
tail->next = new;
}
tail = new;
}
return (head);
}
main()
{
struct linkinfo *p_main;
int i,number;
printf("number:");
scanf("%d",&number);
p_main = create(number);
for(i=0; i<number; i++,p_main = p_main->next)
{
printf("%s\n",p_main->name);
printf("%s\n",p_main->place);
printf("%s\n",p_main->birthday);
printf("%s\n",p_main->telephone);
}
}
[此贴子已经被作者于2007-2-8 12:54:18编辑过]