@@@@@@@@@@@@@@@@@@@@如何用二进制文件来存储链表@@@@@@@@@@@@@@@@@@@@@@@
typedef struct L
{
char name[20];
int id;
int chinese;
int english;
int math;
struct L * next;
}Lcx, * Lmycx;
class Find
{
public:
struct L * del(struct L *,int);
void findNU(struct L *,int);
void findone(struct L *,char n[] );
char FName[20];
Find();
virtual ~Find();
struct L * creat();
};
#include"stdlib.h"
#include "Find.h"
#include "iostream.h"
#include "string.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Find::Find()
{
}
Find::~Find()
{
}
struct L * Find::creat()
{
struct L * head,*p,*s;
int i,j;
cout<<"请输入人数: "<<endl;
cin>>j;
head=(struct L *)malloc(sizeof(Lcx));
p=head;
for(i=0;i<j;i++)
{
cout<<"请输入姓名:"<<endl;
s=(Lcx *)malloc(sizeof(Lcx));
cin>>s->name;
cout<<"请输入id:"<<endl;
cin>>s->id;
cout<<"请输入语文成绩:"<<endl;
cin>>s->chinese;
cout<<"请输入英语成绩: "<<endl;
cin>>s->english;
cout<<"请输入数学成绩: "<<endl;
cin>>s->math;
p->next=s;
p=s;
}
head=head->next;
p->next=NULL;
return (head);
}
void Find::findone(struct L * head, char n[])
{
struct L *p;
p=head;
while( p!=NULL)
{
if(strcmp(p->name,n)==0)
{
cout<<"找到: "<<endl;
cout<<"英文姓名"<<p->name<<endl;
cout<<"学号"<<p->id<<endl;
cout<<"语文成绩"<<p->chinese<<endl;
cout<<"英语成绩"<<p->english<<endl;
cout<<"数学成绩"<<p->math<<endl;
return ;
}
else
p=p->next;
}
}
void Find::findNU(struct L *head, int c)
{
struct L *p;
p=head;
while(p->id!=c && p!=NULL) p=p->next;
if(p!=NULL)
{
cout<<"找到: "<<endl;
cout<<p->name<<endl;
cout<<p->id<<endl;
cout<<p->chinese<<endl;
cout<<p->english<<endl;
cout<<p->math<<endl;
}
else
{
cout<<"找不到您所要查询的人"<<endl;
}
}
struct L * Find::del(struct L *head, int t)
{
struct L *p,*q;
if(head==NULL)cout<<"链表下益"<<endl;
if(head->id==t)
{
p=head;
head=head->next;
free(p);
}
else
{
q=head;
p=head->next;
while(p!=NULL&&p->id!=t)
if(p->id!=t)
{
q=p;p=p->next;
}
if(p!=NULL)
{
q->next=p->next;
free(p);
}
}
return head;
}
void main()
{
Find k;
char FName[20];
int c,c2,t,w;
struct L * r,*r2;
r=k.creat();
cout<<"请输入被查询者姓名: "<<endl;
cin>>FName;
k.findone(r,FName);
cout<<"请输入被删除者学号"<<endl;
cin>>t;
r2=k.del(r,t);
cout<<"请输入被查询者学号: "<<endl;
cin>>c2;
k.findNU(r2,c2);
cout<<"请输入被查询者学号: "<<endl;
cin>>c;
k.findNU(r,c);
}
我想用二进制文件来存储我所创建的单向链表,请问该怎么做。
问题点数:0、回复次数:4Top
1 楼flyhigh(一不小心)回复于 2003-06-03 14:01:16 得分 0
first save all nodes's data field to a file in order with you specified file format.
to load a link table you need read node's data from the file and insert it to a new link tableTop
2 楼coldname(重阳)回复于 2003-06-03 14:24:15 得分 0
只要你按自己定义的规则写入文件, 然后按这个规则读出来就可以了。像你上面那种结构成员比较简单的结构,直接按照name(固定占用空间),id, chinese, english, math顺序一个节点一个节点写下去就可以了。
读也一样。
Top
3 楼shanmao502()回复于 2003-06-03 16:27:25 得分 0
非常感谢前面几位的解答,我现在已经可以用二进制保存链表内容了,不过怎么读出保存的文件内容(ifsteam)就don't known 了。请帮帮我。谢谢。Top
4 楼shanmao502()回复于 2003-06-03 16:30:46 得分 0
忘了说明,我的链表保存方法:
void Find::Save(struct L * head) //head为链表头指针
{
struct L *p;
p=head;
ofstream outbal("chenxiao.asc",ios::out|ios::binary);
if(!outbal)
{
cout<<"Cannot open output file."<<endl;
exit(1);
}
while( p!=NULL)
{
outbal.write((const char *) p,sizeof(struct L));
p=p->next;
}
outbal.close();
}Top




