一个纠结的c语言问题(双向链表练习)

mich_mia 2010-11-16 09:03:29
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#include<iostream.h>
struct address
{
char name[9];
char street[40];
char city[9];
char province[9];
char zip[7];
struct address *prior;
struct address *next;
}list_entry;
struct address *start;
struct address *last;
void enter(),display(),search(),save(),load();
main()
{
char s[80],choice;
struct address *info;
start=last=NULL;
for(;;)
{
switch(menue_select())
{
case 1:enter();break;
case 2:delete();break;
case 3:list();break;
case 4:search();break;
case 5:save();break;
case 6:load();break;
case 7:exit(0);
}
}
}

menue_select()
{
char *s[80];
int c;
printf("\t 请选择功能:\n\n");
printf("\t 1.输入通讯处:\n");
printf("\t 2.删除通讯处:\n");
printf("\t 3.列表显示:\n" );
printf("\t 4.查找:\n");
printf("\t 5.将文件存盘:\n");
printf("\t 6.将文件从盘中装入:\n");
printf("\t 7.退出:\n");
do{
printf("\n\t请按数字键选择:");
gets(s);
c=atoi(s);
}while(c<0||c>7);
return(c);
}
void enter()
{
struct address *info,dls_store();
for(;;)
{
info=(struct address *)malloc(sizeof(list_entry));
if(!info)
{
printf("\t内存已用完!\n");
return;
}
inputs("\t 请输入姓名:",info->name,8);
if(!*info->name) break;
inputs("\t 请输入街名:",info->street,39);
inputs("\t 请输入市名:",info->city,8);
inputs("请输入省名:",info->province,8);
inputs("请输入邮编:",info->zip,7);
*start=dls_store(info, start);
}
}
inputs(prompt,s,count)
char *prompt,*s;
int count;
{
char p[255];
do{
printf(prompt);
gets(p);
if(strlen(p)>count)
printf("\t 太长了!");
}while(strlen(p)>count);
strcpy(s,p);
}
struct address *dls_store(i,top)
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,*find();
char s[80];

printf("\t:请输入姓名:");
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->next->prior=info->prior;
else last=info->prior;
}
free(info);
}
}
struct address *find(name)
char *name;
{

struct address *info,*start;
info=start;
while(info)
{
if(!strcmp(name,info->name))
return(info);
info=info->next;
}
printf("\t 未能找到此姓名!\n");
return(NULL);
}
list()
{
register int t;
struct address *info;
info=start;
while(info)
{
display(info);
info=info->next;
}
printf("\n\n");
}
void display(info)
struct address *info;
{
printf("\t%s\n",info->name);
printf("\t%s\n",info->street);
printf("\t%s\n",info->city);
printf("\t%s\n",info->province);
printf("\t%s\n",info->zip);
printf("\n\n");
}
void search()
{
char name[40];
struct address *info,*find();
printf("\t请给要找的姓名:");
gets(name);
if(!(info=find(name)))
printf("\t未找到!");
else
display(info);
}
void save()
{
register int t;
struct address *info;
FILE *fp;
if((fp=fopen("mlist","wb"))==NULL)
{
printf("\t文件不存在!");
exit(0);
}
printf("\t正在存入文件\n");
info=start;
while(info)
{
fwrite(info,sizeof(struct address),1,fp);
info=info->next;
}
fclose(fp);

}
void load()
{
register int;
struct address *info,*temp=NULL;
FILE *fp;
if((fp=fopen("mlist","wb"))==NULL)
{
printf("\t文件打不开!");
exit(1);
}
while(start)
{
info=start->next;
free(info);
start=info;
}
printf("\t正在装入文件!\n");
start=(struct address *)malloc(sizeof(struct address));
if(!start)
{
printf("\t内存已用完!\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("\t内存已用完!\n");
return;
}
info->prior=temp;
temp=info;
info=info->next;
}
temp->next=NULL;
last=temp;
start->prior=NULL;
fclose(fp);
}

编译之后 出现的是有大概14个warning and 1error
暂时不管warning 只看error
是这样子的
红色部分提示 是
error C2040: 'dls_store' : 'struct address *()' differs in levels of indirection from 'struct address ()'改如何解决
请高手支招

...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mich_mia 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bobo364 的回复:]
我当c语言看了,struct address *dls_store(i,top)
这句c中不能这么定义,没有结构体指针后面跟个函数的,在说即使是c++也要有这个函数吧

C/C++ code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//#include<fstream.h>
//#include<iostrea……
[/Quote]
但是这个没问题呀
struct address *find(name)
char *name;
{

struct address *info,*start;
info=start;
while(info)
{
if(!strcmp(name,info->name))
return(info);
info=info->next;
}
printf("\t 未能找到此姓名!\n");
return(NULL);
}
下面的 这个为什么不提示错误?
bobo364 2010-11-16
  • 打赏
  • 举报
回复
我当c语言看了,struct address *dls_store(i,top)
这句c中不能这么定义,没有结构体指针后面跟个函数的,在说即使是c++也要有这个函数吧
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//#include<fstream.h>
//#include<iostream.h>
struct address
{
char name[9];
char street[40];
char city[9];
char province[9];
char zip[7];
struct address *prior;
struct address *next;
}list_entry;

struct address *start;
struct address *last;
void enter(),display(),search(),save(),load();

int main()
{
char s[80],choice;
struct address *info;
start=last=NULL;
for(;;)
{
switch(menue_select())
{
case 1:enter();break;
case 2:delete();break;
case 3:list();break;
case 4:search();break;
case 5:save();break;
case 6:load();break;
case 7:exit(0);
}
}
}

menue_select()
{
char *s[80];
int c;
printf("\t 请选择功能:\n\n");
printf("\t 1.输入通讯处:\n");
printf("\t 2.删除通讯处:\n");
printf("\t 3.列表显示:\n" );
printf("\t 4.查找:\n");
printf("\t 5.将文件存盘:\n");
printf("\t 6.将文件从盘中装入:\n");
printf("\t 7.退出:\n");
do{
printf("\n\t请按数字键选择:");
gets(s);
c=atoi(s);
}while(c<0||c>7);
return(c);
}
void enter()
{
struct address *info,dls_store();
for(;;)
{
info=(struct address *)malloc(sizeof(list_entry));
if(!info)
{
printf("\t内存已用完!\n");
return;
}
inputs("\t 请输入姓名:",info->name,8);
if(!*info->name) break;
inputs("\t 请输入街名:",info->street,39);
inputs("\t 请输入市名:",info->city,8);
inputs("请输入省名:",info->province,8);
inputs("请输入邮编:",info->zip,7);
*start=dls_store(info, start);
}
}
inputs(prompt,s,count)
char *prompt,*s;
int count;
{
char p[255];
do{
printf(prompt);
gets(p);
if(strlen(p)>count)
printf("\t 太长了!");
}while(strlen(p)>count);
strcpy(s,p);
}
struct address *dls_store(i,top)//这句超级怪啊,指针又不是函数
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,*find();
char s[80];

printf("\t:请输入姓名:");
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->next->prior=info->prior;
else last=info->prior;
}
free(info);
}
}
struct address *find(name)
char *name;
{

struct address *info,*start;
info=start;
while(info)
{
if(!strcmp(name,info->name))
return(info);
info=info->next;
}
printf("\t 未能找到此姓名!\n");
return(NULL);
}
list()
{
register int t;
struct address *info;
info=start;
while(info)
{
display(info);
info=info->next;
}
printf("\n\n");
}
void display(info)
struct address *info;
{
printf("\t%s\n",info->name);
printf("\t%s\n",info->street);
printf("\t%s\n",info->city);
printf("\t%s\n",info->province);
printf("\t%s\n",info->zip);
printf("\n\n");
}
void search()
{
char name[40];
struct address *info,*find();
printf("\t请给要找的姓名:");
gets(name);
if(!(info=find(name)))
printf("\t未找到!");
else
display(info);
}
void save()
{
register int t;
struct address *info;
FILE *fp;
if((fp=fopen("mlist","wb"))==NULL)
{
printf("\t文件不存在!");
exit(0);
}
printf("\t正在存入文件\n");
info=start;
while(info)
{
fwrite(info,sizeof(struct address),1,fp);
info=info->next;
}
fclose(fp);

}
void load()
{
register int;
struct address *info,*temp=NULL;
FILE *fp;
if((fp=fopen("mlist","wb"))==NULL)
{
printf("\t文件打不开!");
exit(1);
}
while(start)
{
info=start->next;
free(info);
start=info;
}
printf("\t正在装入文件!\n");
start=(struct address *)malloc(sizeof(struct address));
if(!start)
{
printf("\t内存已用完!\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("\t内存已用完!\n");
return;
}
info->prior=temp;
temp=info;
info=info->next;
}
temp->next=NULL;
last=temp;
start->prior=NULL;
fclose(fp);
}

mich_mia 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ouyh12345 的回复:]
struct address *dls_store(i,top)
struct address *i;
struct address *top;{

address结构并没有提供构造函数,struct address *dls_store(i,top)想表达什么意思?而且后面没有分号
struct address *top;{应该是struct address *top{
[/Quote]
其实那个定义的话 {在变量定义的前后没有太大的关系吧?这个是构造函数呢struct address *dls_store(i,top)
像你这样的改 行不通的
mich_mia 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hnuqinhuan 的回复:]
没见过这样的函数写法 诡异啊
[/Quote]
呵呵 这个是双链表的一个练习,主要是想练习下的 ,但是处理问题 ,没辙了....
mich_mia 2010-11-16
  • 打赏
  • 举报
回复
4楼
用vc++6.0写的
用什么调试呢?我用vc调试不出来 要不然也不会贴出来了。
無_1024 2010-11-16
  • 打赏
  • 举报
回复
没见过这样的函数写法 诡异啊
mich_mia 2010-11-16
  • 打赏
  • 举报
回复
回复2楼struct address *dls_store(i,top)
struct address *i;
struct address *top;{
这是一个函数呀 不能这样写吗?
struct address *dls_store(i,top)
这是那个函数的名字前面有个调用
*start=dls_store(start,info);
goingfly 2010-11-16
  • 打赏
  • 举报
回复
最怕给别个改程序````自己的程序还是自己调试吧``````调试工具不会用吗?
luciferisnotsatan 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ouyh12345 的回复:]

struct address *dls_store(i,top)
struct address *i;
struct address *top;{

address结构并没有提供构造函数,struct address *dls_store(i,top)想表达什么意思?而且后面没有分号
struct address *top;{应该是struct address *top{
[/Quote]
+1
Csuxiaowu 2010-11-16
  • 打赏
  • 举报
回复
我疯掉了 你还是看看书再写程序吧
ouyh12345 2010-11-16
  • 打赏
  • 举报
回复
struct address *dls_store(i,top)
struct address *i;
struct address *top;{

address结构并没有提供构造函数,struct address *dls_store(i,top)想表达什么意思?而且后面没有分号
struct address *top;{应该是struct address *top{

69,369

社区成员

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

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