我的程序再一次进入了死循环
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof( struct person )
struct person{
char name[20]; //人名
char tel_num[10]; //电话
struct person *next_person; //下一个节点
};
int count = 0; //计数器 控制人信息个数
struct person *set_link ( )
{
struct person *head;
struct person *str1,*str2;
char name_str[20];
char tel[10];
gets( name_str ); //输入人名
gets( tel ); //输入电话
head = NULL;
str1 = str2 = ( struct person * ) malloc ( LEN ); //开辟新节点
count++;
while( count < 2)
{
strcpy(str1->name,name_str);
strcpy(str1->tel_num,tel);
if( count == 1) { head = str1; str2 =str1; }
else {
str2 -> next_person = str1;
str2 = str1;
}
str1 = ( struct person * ) malloc ( LEN );
gets( name_str );
gets( tel );
count++;
strcpy(str1->name,name_str);
strcpy(str1->tel_num,tel);
}
str2->next_person = str1;
str2 = str1;
str2 -> next_person = NULL;
return (head);
}
void display( struct person *head )
{
struct person *ptr = NULL;
printf( "\n name is \t telephone is \t\n");
ptr = head;
while( ptr != NULL )
{
printf("%s%s\n",ptr -> name,ptr -> tel_num );
ptr = ptr -> next_person;
}
}
struct person *seek( struct person *head )
{
char name_to_find[20];
struct person *ptr = NULL;
int n = 0;
printf("input the name whose telephone you want to find:\n");
gets( name_to_find );
ptr = head;
while( ptr -> next_person != NULL )
{
if( (n = strcmp( ptr -> name,name_to_find ) ) == 0)
display( ptr );
else
ptr = ptr -> next_person;
}
return ptr;
}
main()
{
struct person *head = NULL;
struct person *find = NULL;
printf( "input name and telephone number:\n " );
head = set_link ( );
display( head );
find = seek( head );
display( find );
system("pause");
}