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

如何删除用单链表保存在文本的数据?

2012-05-14 
怎么删除用单链表保存在文本的数据???用单链表保存在文本里的数据,在Delete()函数中删除,不知道怎么删除不

怎么删除用单链表保存在文本的数据???
用单链表保存在文本里的数据,在Delete()函数中删除,不知道怎么删除不了,郁闷
希望路过的帮帮我......谢谢!
[code=C/C++][/code]
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct PhoneMessage//数据类
{
char name[10];//姓名
char lPhoneNumber[15];//长号
char sPhoneNumber[10];//短号
char idNumber[20];//身份证号
string email;//邮箱地址
PhoneMessage *next;

void Input();//输入信息
void Display();//输出信息
void ReadFile(istream &infile);//从文本中读出数据
};

class PhoneFunction //功能类
{
public:
PhoneFunction();//构造函数
//~PhoneFunction();
void Insert();//插入信息
void Show();//显示全部人的电话信息
void Save();//保存数据
PhoneMessage *Find(char *); //查找
void Delete(char *);//删除
private:
PhoneMessage *head,*rear;
ifstream infile;
ofstream outfile;
};

void PhoneMessage::Input()
{
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入电话号码(长号):";
cin>>lPhoneNumber;
cout<<"请输入电话号码(短号):";
cin>>sPhoneNumber;
cout<<"请输入身份证号码:";
cin>>idNumber;
cout<<"请输入邮箱地址:";
cin>>email;
}

void PhoneMessage::Display()
{
cout<<"姓名:"<<name<<'\t'<<"长号:"<<lPhoneNumber<<'\t'
<<"短号:"<<sPhoneNumber<<'\t'<<"身份证号码:"<<idNumber
<<'\t'<<"电子邮箱地址:"<<email<<endl;
}


void PhoneMessage::ReadFile(istream &infile)
{

infile>>name>>lPhoneNumber>>sPhoneNumber>>idNumber>>email;
}

PhoneFunction::PhoneFunction()
{
head=new PhoneMessage;//生成头结点
head->next=new PhoneMessage;
rear=head->next;

infile.open("PhoneMessage.text");
if(!infile)
cout<<"电话号码系统中没有任何号码,请输入号码!"<<endl;
else
{
while(!infile.eof())
{
rear->ReadFile(infile);
rear->next=new PhoneMessage;
rear=rear->next;
}
infile.close();
cout<<"读取电话号码成功!"<<endl;
}
}

void PhoneFunction::Insert() //尾插法插入数据
{
PhoneMessage *s;
string st="yes";
while(st=="yes")  
{
s=new PhoneMessage; //生成新结点
rear->Input(); 
rear->next=s;//新结点插入到表尾
rear=s; //尾指针rear指向新的表尾
cout<<"是否继续输入(yes/no)......";
cin>>st;
}
rear->next=NULL; //尾结点后继指针置空

}

void PhoneFunction::Show()
{
for(PhoneMessage *p=head->next;p!=rear;p=p->next)
p->Display();
}

void PhoneFunction::Save()
{
outfile.open("PhoneMessage.text");
for(PhoneMessage *p=head->next;p!=rear;p=p->next)
outfile<<p->name<<"\t"<<p->lPhoneNumber<<"\t"<<p->sPhoneNumber
<<"\t"<<p->idNumber<<"\t"<<p->email<<endl;
outfile.close();
cout<<"保存成功!"<<endl;
}


PhoneMessage *PhoneFunction::Find(char *n)//按姓名查找
{
PhoneMessage *p;
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,n)==0)
{
p->Display();
return p;
}
}
if(p==NULL)  
cout<<"查无此人!"<<endl;

}

void PhoneFunction::Delete(char *n) //这函数出错了,不知道怎么改???


{
char c='y';
PhoneMessage *p,*q;
p=new PhoneMessage;
q=NULL;
p->next=Find(n);
cout<<"是否要删除此人的信息(y/n):";
cin>>c;
while(c=='y')
{
q=p->next;
p->next=p->next->next; //摘链
delete q;
cout<<"删除成功!"<<endl;
break;
}
}


int main()
{
PhoneFunction Ph;
Ph.Insert();
Ph.Show();

char name3[10];
cout<<"请输入要删除人的姓名:";
cin>>name3;
Ph.Delete(name3);

Ph.Save();
return 0;
}

[解决办法]
不知道怎么删除不了==>为什么删除不了呢,报错了吗?是什么错误呢?
[解决办法]
你的查找函数有问题
for(p=head->next;p!=NULL;p=p->next)
上面这一句有问题,在你创建链表时,尾指针rear中的next并没有指向空,而只有在插入新数据时,才指向了空,因此将for循环中的条件表达式改为:p != rear

然后将该函数中的最后一个判断语句if(p == NULL)改为如下:

if(p == rear)
{
cout<<"查无此人!"<<endl;
return NULL;
}


[解决办法]
你的删除函数有问题,问题在于不管查找是否成功都要进行删除,因此是错误的,只有查找成功时,才进行删除,修改如下:

C/C++ code
void PhoneFunction::Delete(char *n) {char c='y';PhoneMessage *p,*q;p=Find(n);cout<<"是否要删除此人的信息(y/n):";cin>>c;while(c=='y'){if(p != NULL){q = this->head;while(q->next != p)q = q->next;q->next = p->next; //摘链delete p;cout<<"删除成功!"<<endl;}else{cout << "没有找到此人,无法删除!" << endl;}break;}}
[解决办法]
这是对你代码的修改,对照看一下吧:

C/C++ code
#include <iostream>#include <string>#include <fstream>using namespace std; struct PhoneMessage //数据类{string name; //姓名string lPhoneNumber; //长号string sPhoneNumber; //短号string idNumber; //身份证号string email; //邮箱地址PhoneMessage *next;void Input(); //输入信息void Display(); //输出信息void ReadFile(istream &infile); //从文本中读出数据};class PhoneFunction //功能类{public:PhoneFunction(); //构造函数// ~PhoneFunction();void Insert(); //插入信息void Show(); //显示全部人的电话信息void Save(); //保存数据PhoneMessage *Find(char *); //查找void Delete(char *); //删除PhoneMessage *gethead()const{return head;}PhoneMessage *gettail()const{return rear;}private:PhoneMessage *head,*rear;ifstream infile;ofstream outfile;};void PhoneMessage::Input(){cout<<"请输入姓名:";cin>>name;cout<<"请输入电话号码(长号):";cin>>lPhoneNumber;cout<<"请输入电话号码(短号):";cin>>sPhoneNumber;cout<<"请输入身份证号码:";cin>>idNumber;cout<<"请输入邮箱地址:";cin>>email;}void PhoneMessage::Display(){cout<<"姓名:"<<name<<'\t'<<"长号:"<<lPhoneNumber<<'\t'<<"短号:"<<sPhoneNumber<<'\t'<<"身份证号码:"<<idNumber<<'\t'<<"电子邮箱地址:"<<email<<endl;}void PhoneMessage::ReadFile(istream &infile){infile>>name>>lPhoneNumber>>sPhoneNumber>>idNumber>>email;}PhoneFunction::PhoneFunction(){head=new PhoneMessage; //生成头结点rear=new PhoneMessage;head->next = rear;rear->next = NULL;infile.open("PhoneMessage.txt");if(!infile)cout<<"电话号码系统中没有任何号码,请输入号码!"<<endl;else{    PhoneMessage *p;    string s;while(!infile.eof()){        p = new PhoneMessage;        p->ReadFile(infile);        p->next = head->next;        head->next=p;}infile.close();cout<<"读取电话号码成功!"<<endl;}}void PhoneFunction::Insert() //尾插法插入数据{PhoneMessage *s,*p;p = head;while(p->next != rear)p = p->next;string st="yes";while(st=="yes")   {s=new PhoneMessage; //生成新结点s->Input();   p->next=s; //新结点插入到表尾s->next = rear;//尾指针rear指向新的表尾p = s;cout<<"是否继续输入(yes/no)......";cin>>st;}}void PhoneFunction::Show(){for(PhoneMessage *p=head->next;p!=rear;p=p->next)p->Display();}void PhoneFunction::Save(){outfile.open("PhoneMessage.txt");for(PhoneMessage *p=head->next;p!=rear;p=p->next)outfile<<p->name<<"\t"<<p->lPhoneNumber<<"\t"<<p->sPhoneNumber<<"\t"<<p->idNumber<<"\t"<<p->email<<endl;outfile.close();cout<<"保存成功!"<<endl;}PhoneMessage *PhoneFunction::Find(char *n) //按姓名查找{PhoneMessage *p;for(p=head->next;p!=rear;p=p->next){if(strcmp((p->name).c_str(),n)==0){p->Display();return p;}} cout<<"查无此人!"<<endl;return NULL;}void PhoneFunction::Delete(char *n) //这函数出错了,不知道怎么改???{char c='y';PhoneMessage *p =NULL,*q;p=Find(n);cout<<"是否要删除此人的信息(y/n):";cin>>c;while(c=='y'){    if( p != NULL)    {        q = head;        while(q->next != p)            q = q->next;        q->next = p->next; //摘链delete p;cout<<"删除成功!"<<endl;    }    else    {        cout << "查找无此人,无法删除!" << endl;    }break;}}int main(){PhoneFunction Ph;PhoneMessage *p = Ph.gethead(),*q = Ph.gettail();if(p->next != q)Ph.Show();Ph.Insert();Ph.Show(); char name3[10];cout<<"请输入要删除人的姓名:";cin>>name3;Ph.Delete(name3);Ph.Save();return 0;} 

热点排行