俺新手,请教高手们一个关于链表的问题。。。
这个源代码网上找到的,是一个简单通信录的演示,可是我发现这个通信录在输入4条以上数据后删除某个中间的记录,选择LIST列出所有项后,就会死循环地重复列某一个记录,请教该如何修改?/*--------------------------------------------------华丽的分割线----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
struct address{
char name[20];
char street[20];
char city[10];
char state[10];
char zip[7];
struct address *next;
struct address *prior;
}list_entry;
struct address *start;
struct address *last;
void enter();
void display();
void search();
void save();
void load();
void list();
struct address *find(char *name);
struct address *dls_store(struct address *,struct address *);
main()
{
clrscr();
start=last=NULL;
load();
for (;;){
switch(menu_select()){
case 1:enter();break;
case 2:delete();break;
case 3:list();break;
case 4:search();break;
case 5:save();break;
case 6:exit(0);
}
}
}
menu_select()
{
char c;
printf("1.Enter\n");
printf("2.Delete\n");
printf("3.List\n");
printf("4.Search\n");
printf("5.Save\n");
printf("6.Quit\n");
do{
printf("\nEnter you choice:");
c=getche();
}while (c<'0'||c>'6');
return c-48;
}
void enter()
{
struct address *info;
for (;;){
info=(struct address *)malloc(sizeof(list_entry));
if (!info){
printf("\nout of memory");
return;
}
inputs("\nenter name:",info->name,19);
if (!info->name[0]) break;
inputs("enter street:",info->street,19);
inputs("enter city:",info->city,9);
inputs("enter state:",info->state,9);
inputs("enter zip:",info->zip,6);
start=dls_store(info,start);
}
}
inputs(char *prompt,char *s,int count)
{
char p[255];
do{
printf(prompt);
gets(p);
if (strlen(p)>count) printf("\ntoo long\n");
}while (strlen(p)>count);
strcpy(s,p);
}
struct address *dls_store(struct address *i,struct address *top)
{
struct address *old,*p;
if (last==NULL){
i->next=NULL;
i->prior=NULL;
last=i;
return i;
}
p=top;
old=NULL;
while(p){
if (strcmp(p->name,i->name)<0){
old=p;p=p->next;
}
else{
if(p->prior){
p->prior->next=i;
i->next=p;
i->prior=p->prior;
p->prior=i;
return top;
}
i->next=p;
i->prior=NULL;
p->prior=i;
return i;
}
}
old->next=i;
i->next=NULL;
i->prior=old;
last=i;
return start;
}
delete()
{
struct address *info;
char s[80];
printf("enter name:");
gets(s);
info=find(s);
if (info) {
if (start==info){
start=info->next;
if (start)
start->prior=NULL;
else
last=NULL;
}
else{
info->prior->next=info->next;
if(info!=last)
info->prior->next=info->prior;
else
last=info->prior;
}
free(info);
}
}
struct address *find(char *name)
{
struct address *info;
info=start;
while(info){
if(!strcmp(name,info->name)) return info;
info=info->next;
}
printf("name not found\n");
return NULL;
}
void list()
{
struct address *info;
info=start;
printf("\n%-20s%-20s%-10s%-10s%-6s\n",
"姓名","街道","城市","省","邮编");
while(info){
display(info);
info=info->next;
}
printf("\n\n");
}
void display(struct address *info)
{
printf("%-20s%-20s%-10s%-10s%-6s\n",
info->name,info->street,info->city,info->state,info->zip);
}
void search()
{
char name[40];
struct address *info,*find();
printf("enter name to find:");
gets(name);
info=find(name);
if(!info)
printf("not found\n");
else
display(info);
}
void save()
{
struct address *info;
FILE *fp;
if((fp=fopen("mlist","wb"))==NULL){
printf("can not open file\n");
exit(1);
}
printf("\nSaving file\n");
info=start;
while(info){
fwrite(info,sizeof(struct address),1,fp);
info=info->next;
}
fclose(fp);
}
void load()
{
struct address *info,*temp=NULL;
FILE *fp;
if((fp=fopen("mlist","rb"))==NULL){
printf("can not open file\n");
return;
}
while(start){
info=start->next;
free(info);
start=info;
}
printf("\nLoading file\n");
start=(struct address *)malloc(sizeof(struct address));
if(!start){
printf("out of memory!\n");
return;
}
info=start;
while(!feof(fp)){
if(1!=fread(info,sizeof(struct address),1,fp)) break;
info->next=(struct address *)malloc(sizeof(struct address));
if (!info->next){
printf("out of memory\n");
return;
}
info->prior=temp;
temp=info;
info=info->next;
}
temp->next=NULL;
last=temp;
start->prior=NULL;
fclose(fp);
}
[ 本帖最后由 lxrmido 于 2009-12-29 16:47 编辑 ]