我的程序再一次进入了死循环

jjw_hacker 2005-05-21 11:29:28
#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");

}
...全文
154 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzheng318 2005-05-24
  • 打赏
  • 举报
回复
#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\t%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( !strcmp( ptr -> name,name_to_find ) )
{
// printf("a=%s %s", ptr->name,ptr->tel_num );
// getchar();
return ptr;
}
else
ptr = ptr -> next_person;
}
return NULL;
}

int main()
{
struct person *head = NULL;
struct person *find = NULL;
printf( "input name and telephone number:\n " );
head = set_link ( );
display( head );
find = seek( head );
if (find != NULL)
printf("%s %s",find->name,find->tel_num);
else
printf("No find");
system("pause");
return 0;
}
erxwu 2005-05-24
  • 打赏
  • 举报
回复
#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;
}

}

void 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)
{

printf("%s%s\n",ptr -> name,ptr -> tel_num );
return;
}
else
ptr = ptr -> next_person;
}
printf("Can't find");
}

void main()
{
struct person *head = NULL;
//struct person *find = NULL;
printf( "input name and telephone number:\n " );
head = set_link ( );
display( head );
seek( head );

system("pause");

}
//pass
5420 2005-05-24
  • 打赏
  • 举报
回复
UP
mostideal 2005-05-24
  • 打赏
  • 举报
回复
mark
zhousqy 2005-05-22
  • 打赏
  • 举报
回复
while( ptr -> next_person != NULL )
{
if( (n = strcmp( ptr -> name,name_to_find ) ) == 0)
display( ptr );//执行这个后没有移动指针,所以ptr -> next_person != NULL还是满足
------------
同意
llf_hust 2005-05-22
  • 打赏
  • 举报
回复
我写的那个调试通过了
llf_hust 2005-05-22
  • 打赏
  • 举报
回复
#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\t%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( !strcmp( ptr -> name,name_to_find ) )
{
// printf("a=%s %s", ptr->name,ptr->tel_num );
// getchar();
return ptr;
}
else
ptr = ptr -> next_person;
}
return NULL;
}

int main()
{
struct person *head = NULL;
struct person *find = NULL;
printf( "input name and telephone number:\n " );
head = set_link ( );
display( head );
find = seek( head );
if (find != NULL)
printf("%s %s",find->name,find->tel_num);
else
printf("No find");
system("pause");
return 0;
}
yangwuhan 2005-05-22
  • 打赏
  • 举报
回复
while( ptr -> next_person != NULL )
{
if( (n = strcmp( ptr -> name,name_to_find ) ) == 0)
display( ptr );//执行这个后没有移动指针,所以ptr -> next_person != NULL还是满足
jjw_hacker 2005-05-21
  • 打赏
  • 举报
回复
程序在seek函数中出现了死循环,请教高手帮帮忙赐教

69,370

社区成员

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

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