请问如何在文件中使用普通链表函数,把数据存入link.txt文件中,请说详细点
#include<iostream>
#include<fstream>
using namespace std;
void construct();
int main()
{
char ch;
ofstream ofile;
ifstream ifile;
ofile.open("link.txt");
if(!ofile.fail())
{
construct();//请问如何在文件ofile中使用construct函数,把数据存入link.txt文件中
}
ofile.close();
ifile.open("link.txt");
while(!ifile.fail())
{
ifile>>ch;
cout<<ch;
}
return 0;
}
struct Node
{
char data;
Node *next;
};
void construct() //新建链表
{
Node *head=NULL,*p;
p=new Node;
while(1)
{
cout<<"Input data:"<<endl;
cin>>p->data;
if(p->data=='#')
break;
else
{
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
head->next=p;
p->next=NULL;
}
}
}
}
[解决办法]
把节点信息保存下来。