一个链表的问题?
请问怎么把一个链表中的所有内容保存到一个文件上??如一下链表:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Customer
{
private:
string cCode;
char cName[30];
char cAddress[50];
public:
void getData(const string &code)
{
cCode=code;
cin.ignore();
cout<<endl<<"Enter Customer Name: ";
cin.getline(cName,30);
cout<<endl<<"Enter Customer Address: ";
cin.getline(cAddress,50);
}
void showData()
{
cout<<"****The Customer Details are****"<<endl;
cout<<"Customer Code is: "<<cCode<<endl;
cout<<"Customer Name is: "<<cName<<endl;
cout<<"Cusromer Address is: "<<cAddress<<endl;
}
string getCode()
{
return cCode;
}
};
struct Node
{
public:
Customer obj;
Node *NEXT;
Node(const string &s,Node *n=NULL)
{
NEXT=n;
obj.getData(s);
}
};
class List
{
private:
Node *START,
*CURRENT,
*PRECEDE;
public:
List();
~List()
{
destroy();
}
void addNode(const string &s);
bool delNode(const string &s);
bool queryNode(const string &s);
void displayNode(const string &s);
void traverse();
void destroy();
};
List::List()
{
START=CURRENT=PRECEDE=NULL;
}
void List::destroy()
{
while(START!=NULL)
{
CURRENT=START;
START=START->NEXT;
delete CURRENT;
}
START=CURRENT=PRECEDE=NULL;
}
void List::addNode(const string &s)
{
if(START==NULL || s<=START->obj.getCode())
{
START=new Node(s,START);
return;
}
Node *prev,*curr;
for(prev=curr=START;curr!=NULL && s>curr->obj.getCode();prev=curr,curr=curr->NEXT)
{}
Node *n=new Node(s,curr);
prev->NEXT=n;
}
bool List::queryNode(const string &s)
{
for(PRECEDE=CURRENT=START;CURRENT!=NULL && s!=CURRENT->obj.getCode();PRECEDE=CURRENT,CURRENT=CURRENT->NEXT)
{}
return (CURRENT!=NULL);
}
bool List::delNode(const string &s)
{
if(queryNode(s)==false)
{
return false;
}
PRECEDE->NEXT=CURRENT->NEXT;
if(CURRENT==START)
{
START=START->NEXT;
}
delete CURRENT;
return true;
}
void List::displayNode(const string &s)
{
CURRENT->obj.showData();
}
void List::traverse()
{
for(Node *temp=START;temp!=NULL;temp=temp->NEXT)
{
temp->obj.showData();
cout<<endl;
}
}
int main()
{
List list;
char ch;
while(1)
{
cout << endl << "1. Enter Customer Details" << endl;
cout << "2. Delete Customer Details from list " << endl;
cout << "3. Search for a Customer " << endl;
cout << "4. Display Details of all Customers" << endl;
cout << "5. Exit" << endl;
cout << endl << "Enter choice :: ";
cin >> ch;
switch(ch)
{
case '1':
{
cout << endl << "Enter Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
list.addNode(cCode);
}
break;
case'2':
{
cout << endl << "Enter Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
if(list.delNode(cCode) == false)
{
cout << "Customer not found" << endl;
}
}
break;
case '3':
{
cout << endl << "Enter a Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
if(list.queryNode(cCode) == false)
{
cout << "Customer not found" << endl;
}
else
{
cout << "Customer found in list\n\n"<<endl;
list.displayNode(cCode);
}
}
break;
case '4':
list.traverse();
break;
case '5':
exit(0);
break;
default:
cout << endl << "Enter a correct choice ";
break;
}
}
return 0;
}