链表的存储与读取问题

cn_archer 2005-12-25 12:18:08
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<malloc.h>

struct bank
{
long account;
char name[20];
char password[10];
float saving;
struct bank *pNext;
};

struct bank *pHead,*pEnd;

struct bank *temp;


void create();
void read(struct bank *tempHead);
void list(struct bank *temp);

void write(struct bank *tempBank);

void main()
{
int choose;
if((pHead=(struct bank*)malloc(sizeof(struct bank)))==NULL)
{
printf("创建链表失败!");
exit(1);
}

if((temp=(struct bank*)malloc(sizeof(struct bank)))==NULL)
{
printf("创建临时链表失败!");
exit(1);
}

pHead=pEnd=NULL;

while(1)
{
printf("\n\n请输入选择:");
scanf("%d",&choose);
if(choose==1)
{
create();
temp=pHead;
write(temp);
continue;
}
if(choose==2)
{
read(temp);
list(pHead);
continue;
}
if(choose==3)
exit(0);
}
}

void write(struct bank *tempBank)
{
FILE *fp;
if((fp=fopen("bank.obj","ab+"))==NULL)
{
printf("数据文件创建失败!");
exit(1);
}

while(1)
{
if(tempBank->pNext==NULL)
{
fwrite(tempBank,sizeof(struct bank),1,fp);
if(ferror(fp))
{
printf("数据写入失败!");
exit(1);
}
fclose(fp);
return;
}
tempBank=tempBank->pNext;
}
}

void create()
{
float F_F_F_F=0.0; //无意义定义,只是为了提醒VC6.0需要处理浮点数
struct bank *pCurrentBank;


if((pCurrentBank=(struct bank*)malloc(sizeof(struct bank)))==NULL)
{
printf("创建链表失败!");
exit(1);
}
printf("\n\n*******************");
printf("\n 数据录入开始!");
printf("\n\n帐号: ");
scanf("%d",&pCurrentBank->account);
printf("\n姓名: ");
scanf("%s",&pCurrentBank->name);
printf("\n密码: ");
scanf("%s",&pCurrentBank->password);
printf("\n存款: ");
scanf("%f",&pCurrentBank->saving);

if(pHead==NULL)
pHead=pCurrentBank;
else
pEnd->pNext=pCurrentBank;

pEnd=pCurrentBank;
pEnd->pNext=NULL;
}

void list(struct bank *tempHead)
{
if(tempHead==NULL)
{
printf("暂无数据!\n\n");
return;
}
while(tempHead)
{
printf("帐号:%d\t\t姓名:%s\t\t存款:%0.2f\n\n",\
tempHead->account,tempHead->name,tempHead->saving);
tempHead=tempHead->pNext;
}
}

void read(struct bank * tempHead)
{
FILE *fp;
if((fp=fopen("bank.obj","rb"))==NULL)
{
printf("数据文件打开失败!");
exit(1);
}

while(!feof(fp))
{
fread(tempHead,sizeof(struct bank),1,fp);
if(pHead==NULL)
pHead=tempHead;
else
pEnd->pNext=tempHead;
pEnd=tempHead;
pEnd->pNext=NULL;

}
fclose(fp);
return;
}


在这个程序中,如果注释掉文件存储和读取的代码,链表的输入和显示功能都已正常。
但是,写入链表后,无法正常读取,读取的链表居然只有最后输入的一个节点数据。
而如果在read()函数中的fread()指令后面跟一条printf()指令来显示数据,则全部数据都会显示出来,只是最后一个数据会重复显示一次。
初学C,请各位大哥大姐帮忙!
...全文
247 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengzanmiao 2005-12-26
  • 打赏
  • 举报
回复
mark`
ywchen2000 2005-12-26
  • 打赏
  • 举报
回复
#include<stdlib.h>
#include<stdio.h>
struct list{
int num;
char name[256];
int china;
int english;
int math;
struct list *next;
};
typedef struct list node;
typedef node *link;
printf_list(link head)
{
link pointer;
pointer=head;
while(pointer!=NULL)
{
printf("number:%d\n",pointer->num);
printf("name:%s\n",pointer->name);
printf("china:%d\n",pointer->china);
printf("english:%d\n",pointer->english);
printf("math:%d\n",pointer->math);
pointer=pointer->next;
}
}
link creat_list(link head)
{
int cnum;
char cname[256];
int cchina;
int cenglish;
int cmath;
link pointer,new;
int i;
head=(link)malloc(sizeof(node));
if(head==NULL)
{ printf("memory allocate failure!!\n");
exit(0);}
else{
printf("please input number:");
scanf("%d",&cnum);
printf("please input name:");
scanf("%s",&cname);
printf("please input china:");
scanf("%d",&cchina);
printf("please input english:");
scanf("%d",&cenglish);
printf("please input math:");
scanf("%d",&cmath);
head->num=cnum;
for(i=0;i<256;i++)
{
head->name[i]=cname[i];
}
head->china=cchina;
head->english=cenglish;
head->math=cmath;
head->next=NULL;
pointer=head;
while(1)
{
new=(link)malloc(sizeof(node));
if(new==NULL){
printf("memory allocate failure!!\n");
exit(0);}
printf("please input number:");
scanf("%d",&cnum);
if(cnum==0){
break; }
printf("please input name:");
scanf("%s",cname);
printf("please input china:");
scanf("%d",&cchina);
printf("please input english:");
scanf("%d",&cenglish);
printf("please input math:");
scanf("%d",&cmath);
new->num=cnum;
for(i=0;i<256;i++){
new->name[i]=cname[i];}
new->china=cchina;
new->english=cenglish;
new->math=cmath;
new->next=NULL;
pointer->next=new;
pointer=new;
}
}
return head;
}
search_chengji(int key1,link head)
{
link pointer;
pointer=head;
while(pointer!=NULL)
{
if(pointer->num==key1)
{
printf("number:%d\n",pointer->num);
printf("name:%s\n",pointer->name);
printf("china:%d\n",pointer->china);
printf("english:%d\n",pointer->english);
printf("math:%d\n",pointer->math);
}
pointer=pointer->next;
}
}
link modify_chengji(link head,int key3)
{

link pointer;
char xname[256];
int xchina;
int xenglish;
int xmath;
int choose,i;
pointer=head;
printf("enter 0 exit modefiy\n");
printf("enter 1 modefiy name\n");
printf("enter 2 modefiy china\n");
printf("enter 3 modefiy english\n");
printf("enter 4 modefiy math\n");
scanf("%d",&choose);
switch(choose)
{
case 1:
printf("please input name:");
scanf("%s",&xname);
break;
case 2:
printf("please input china:");
scanf("%d",&xchina);
break;
case 3:
printf("please input english:");
scanf("%d",&xenglish);
break;
case 4:
printf("please input math:");
scanf("%d",&xmath);
break;
}
while(1){
pointer=pointer->next;
if(pointer->num==key3)
{
if(choose==1)
{ for(i=0;i<256;i++)
{
pointer->name[i]=xname[i];
}
break;
}
else if(choose==2)
{ pointer->china=xchina;
break;}
else if(choose==3)
{ pointer->english=xenglish;
break;
}
else if(choose==4)
{pointer->math=xmath;
break;}
}
}
return head;
}
link delete_chengji(link head,int key2)
{
link pointer;
link back;
pointer=head;
while(1)
{
if(head->num==key2)
{ head=pointer->next;
free(pointer);
break;
}
back=pointer;
pointer=pointer->next;
if(pointer->num==key2)
{
back->next=pointer->next;
free(pointer);
break;}
}
return head;
}
link insert_chengji(link head,link new,int key3)
{
link pointer;
pointer=head;
while(1)
{
if(pointer==NULL){
new->next=head;
head=new;
break;}
if(pointer->num==key3){
new->next=pointer->next;
pointer->next=new;
break;}
pointer=pointer->next;
}
return head;
}
pingjufen(link head)
{
link pointer;
int pchina,ppchina;
int penglish,ppenglish;
int pmath,ppmath;
int count;
pchina=0;
penglish=0;
pmath=0;
count=0;
pointer=head;
while(1)
{
pchina=pchina+pointer->china;
penglish=penglish+pointer->english;
pmath=pmath+pointer->math;
count=++count;
if(pointer->next==NULL)
{
break;
}
pointer=pointer->next;
}
ppchina=pchina/count;
ppenglish=penglish/count;
ppmath=pmath/count;
printf("china ping jun fen:%d\n",ppchina);
printf("english ping jun fen:%d\n",ppenglish);
printf("math ping jun fen:%d\n",ppmath);
}
main()
{
for(;;)
{
link head;
link new;
int key;
int keynum;
printf("0>exit the programm.\n");
printf("1>create list.\n");
printf("2>search chengji.\n");
printf("3>modify chengji.\n");
printf("4>delete chengji.\n");
printf("5>add chengji.\n");
printf("6>pingjunfeng.\n");
printf("7>print chengji.\n");
scanf("%d",&key);
switch(key){
case 0:
exit(0);
case 1:
head=creat_list(head);
if(head!=NULL)
{ printf_list(head);}
break;
case 2:
printf("please input 0 Exit.\n");
printf("please input number for search:");
scanf("%d",&keynum);
if(keynum==0){
break; }
search_chengji(keynum,head);
break;
case 3:
printf("please input number for modify:");
scanf("%d",&keynum);
head=modify_chengji(head,keynum);
if(head!=NULL)
{
printf_list(head);
}
break;
case 4:
printf("please input 0 exit\n");
printf("please input number for delete:");
scanf("%d",&keynum);
if(keynum==0){
break; }
head=delete_chengji(head,keynum);
break;
case 5:
if(head!=NULL){
new=(link)malloc(sizeof(node));
printf("please input number:");
scanf("%d",&new->num);
if(new->num==0){
break;}
printf("please input name:");
scanf("%s",&new->name);
printf("please input china:");
scanf("%d",&new->china);
printf("please input english:");
scanf("%d",&new->english);
printf("please input math:");
scanf("%d",&new->math);
printf("please input the data number for insert:");
scanf("%d",&keynum);
head=insert_chengji(head,new,keynum);
if(head!=NULL) {
printf_list(head);}
}
break;
case 6:
pingjufen(head);
break;
case 7:
printf_list(head);
break;
}
}
}
langzi520 2005-12-26
  • 打赏
  • 举报
回复
学习一下
cn_archer 2005-12-25
  • 打赏
  • 举报
回复
如果加了一组大括号之后,如果pHead==NULL,那么pHead=pCurrentBank;
接下去pEnd就没指向到pCurrentBank中了。

何况,这个是if()else语句,执行完else中的语句后,应该是按照程序正常的顺序向下执行,pEnd=tempHead;
pEnd->pNext=NULL;
这两句应该也是可以执行到的呀?
jixingzhong 2005-12-25
  • 打赏
  • 举报
回复
上面说的是 在最后的
void read(struct bank * tempHead)
函数中的 ...
jixingzhong 2005-12-25
  • 打赏
  • 举报
回复
...

else
{
pEnd->pNext=tempHead;
pEnd=tempHead;
pEnd->pNext=NULL;
}

你加一组大括号 ...
没有大括号,代码的流程就不对了...
一直在赋值到一个接点上 ...
自然就是你说的效果了 ...

70,020

社区成员

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

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