首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教怎么在文件中使用普通链表函数,把数据存入link.txt文件中,请说详细点

2012-06-01 
请问如何在文件中使用普通链表函数,把数据存入link.txt文件中,请说详细点#includeiostream#includefstr

请问如何在文件中使用普通链表函数,把数据存入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;
}
}

}
}

[解决办法]
把节点信息保存下来。

热点排行