| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:正在改着一位前辈的信息管理系统程序~关于删除结点的问题
只看楼主 加入收藏
x_tacet
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-8-7
结帖率:0
收藏
 问题点数:0 回复次数:1 
正在改着一位前辈的信息管理系统程序~关于删除结点的问题
程序代码:
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include<time.h>
using namespace std;

class Node      
{
private:
    char Name[10];         //姓名
    int Phone;             //电话
    char Mobile[15];       //手机
    char EMail[20];        //email
    Node *next;            //下一结点指针
public:
    char Time[128];        //系统时间
    Node(const char *n="0",const int o=0,const char *m="0",const char *e="0",const char *t="0",Node *nx=0)//构造函数,各参数均有缺省值
    {
        strcpy(Name,n);
        Phone=o;
        strcpy(Mobile,m);
        strcpy(EMail,e);
        strcpy(Time,t);
        next=nx;
    } 
    void Show();               //显示结点数据
    void SetName(char *name);  //修改姓名
    void SetPhone(int op);     //修改电话号码
    void SetMobile(char *mp);  //修改手机号码
    void SetEMail(char *email);  //修改email地址
    void SetTime(char *t);       //记录时间
    friend class AddrList;       //将AddrList类说明为友元类
    friend void writetotxtfile(AddrList&);
    friend void addfromtxtfile(AddrList&);
};
void Node::Show()                  //类外定义函数
    {
        cout<<Name<<'\t'<<Phone<<'\t'<<'\t'<<Mobile<<'\t'<<'\t'<<EMail<<'\t'<<Time;
    }
void Node::SetName(char *name)
    {
        if(name)
            strcpy(Name,name);
        else
            strcpy(Name," ");
    }
void Node::SetPhone(int op)     
    {   Phone=op;}
void Node::SetMobile(char *mp)       
    {
        if(mp)
            strcpy(Mobile,mp);
        else
            strcpy(Mobile," ");
    }
void Node::SetEMail(char *email)            
    {
        if(email) strcpy(EMail,email);
        else strcpy(EMail," ");
    }
void Node::SetTime(char *t)
    {
        if(time)strcpy(Time,t);
        else strcpy(Time," ");
    }



class AddrList
{
private:
    Node *Head;   //链表首指针
    Node *Tail;   //链表尾指针
public:
    AddrList()      //构造函数,创建空链表
    {
        Node *p=new Node;
        p=NULL;
        Head=p;
        Tail=p;
    }
    void AddTail(Node *p)     //将p指向的结点加入到链表尾部  //类内定义函数
    {
        if(Head==0)
        {   Head=p;
            Tail=p;
            p->next=NULL;
        }
        else
        {
            Tail->next=p;
            Tail=p;
            p->next=NULL;
        }
    }
    Node *LookUp(char *name)         //按姓名查找结点并返回该结点指针
    {
        Node *p;
        p=Head;
        while(p!=NULL)
        {
            if(strcmp(p->Name,name)==0)
                return p;
            p=p->next;
        }
        return NULL;
    }
    void Delete(char *name)        //删除p结点
    {
        Node *p,*head1;
        p=LookUp(name);
        head1=Head;
        if(head1=p)
        {
            Head=p->next;
            delete p;
        }
        else
        {
            head1->next=p->next;
            delete p;
        }
    }
    void AddSort(Node *p)     //将p指向的结点按Tag指定的顺序插入到链表中
    {
        Node *p1,*p2;          //p指向的结点插在p1与p2指向的结点之间                   
            if(Head==NULL)              //插入前原始链表为空
            {   Head=p;
                Tail=p;
                p->next=NULL;
            }
            if(strcmp(Head->Name,p->Name)>0)           //插在链表首部
            {   p->next=Head;
                Head=p;
            }
            p1=Head;                               //插在链表中间或尾部
             p2=Head;
               while(p2->next&&strcmp(p2->Name,p->Name)<0)
            {   p1=p2;
                p2=p2->next;
            }
             if(strcmp(p2->Name,p->Name)<0)
            {
                p2->next=p;
                p->next=NULL;
            }
            else if(strcmp(p2->Name,p->Name)>0)
            {
                p->next=p2;
                p1->next=p;
            }s
    }
    void ShowAll()            //显示全部结点,每20个显示一屏
    {
        Node *p=Head;
        if(p==NULL)
            cout<<"链表为空!\n";
        else
        while(p)
        {
            cout<<"Name"<<'\t'<<"Phone"<<'\t'<<'\t'<<"Mobile"<<'\t'<<'\t'<<"EMail"<<'\t'<<"Time"<<endl;
            for(int i=0;i<20;i++)
            {
                p->Show();
                p=p->next;
                if(p==NULL) break;
            }
            system("pause");        
        }
    }
    Node *GetHeadPtr()        //取首指针
    {   return Head; }
    ~AddrList()              //析构函数,释放链表空间
    {
        Node *p;
        while(Head)
        {
            p=Head;
            Head=Head->next;
            delete p;
        }
    }
void CreateList(char *filename)                       //从二进制文件中读入数据,构造链表 
{
    ifstream infile(filename, ios::in|ios::binary); 
    infile.read((char *)0, 4);                     //读出二进制文件的第1个数据 
    int n;
    infile.read((char *)&n, 4);                       //读出记录数 
    cout<<"正在从数据中加载...";
    Node *p; 
    for(int i=0;i<n;i++)
    {
        p=new Node;
        infile.read((char *)p, sizeof(Node)); 
        AddTail(p); 
    }
    infile.close(); 
    cout<<"数据从二进制文件加载成功!";
}
void WriteToFile(char *filename)//将链表中数据写入指定的二进制文件 
{
    ofstream outfile;
    outfile.open(filename, ios::out|ios::binary); 
    outfile.write((char *)0,4);
    Node *p, *p1;
    int n=0;
    for(p1=Head; p1!=NULL; p1=p1->next) n++;
    outfile.write((char *)&n,4);
    p=Head; 
    while(p)
    { 
        outfile.write((char *)p, sizeof(Node)); 
        p=p->next; 
    } 
    cout<<"成功导出二进制文件";
    outfile.close(); 
}
};
void DisplayMenu()
{
    char *menu[]={
        "1.Add Record","2.Delete Record","3.Display All Record","4.Search",
                "5.Modify Record","6.Add from a Text File","7.Write to a Text File"
                      ,"8.Quit"};
    cout<<endl<<endl;
    cout<<"*********************      Contacts By Alex     ************************"<<endl<<endl<<endl<<endl;
    for(int i=0;i<8;i++)
            cout<<'\t'<<'\t'<<'\t'<<*(menu+i)<<endl;
    cout<<endl<<endl<<endl<<"************************************************************************"<<endl;
}
void add(AddrList &addrlist)             //增加记录,插入结点后,链表仍然保持有序
{
    
        Node *p=new Node;
         char name[10],Mobile[15],email[20];
         int Phone;
        char s;
        cout<<"请输入姓名:";
        cin>>name;
        p->SetName(name);
        cout<<endl;
        cout<<"请输入电话:";
        cin>>Phone;
        p->SetPhone(Phone);
        cout<<endl;
        cout<<"请输入手机号码:";
        cin>>Mobile;
        p->SetMobile(Mobile);
        cout<<endl;
        cout<<"请输入email地址:";
        cin>>email;
        p->SetEMail(email);    
         cout<<endl;

        char ch[128];
        time_t t;
        memset(ch,0x0,128);
        time(&t);
        sprintf(ch,"%s", ctime(&t));
        strcpy(p->Time,ch);

        cout<<"确认添加?(y or n)\n";
        cin>>s;
        if(s=='y')
        {
            addrlist.AddSort(p);
            p->Show();
             cout<<endl;
            cout<<"增加记录成功!\n";
        }
        else if(s=='n')
        {
            p->Show();
             cout<<endl;
            cout<<"该记录未添加!\n";
        }
        else
            cout<<"您的输入有误!\n";
}
      
void showall(AddrList &addrlist)         //显示所有记录
{
    addrlist.ShowAll();
}
void search(AddrList &addrlist)          //按姓名查找并显示
    {
        char name[10];
        Node *p;
        cout<<"please input the name"<<endl;
        cin>>name;
        p=addrlist.LookUp(name);
        if(p)
        {
            cout<<"Contact is found."<<endl;
            p->Show();
        }
        else
            cout<<"The contact is not found."<<endl;
        system("pause");
    }
void del(AddrList &addrlist)
{
    char name[10],s;
    Node *p;
    cout<<"please input the name"<<endl;
    cin>>name;
    p=addrlist.LookUp(name);
    cout<<"Confirm deleted?(y or n)\n";
    cin>>s;
        if(s=='y')
    if(p)
    {
        addrlist.Delete(name);
        cout<<name<<" has been deleted"<<endl;
    }
    else cout<<"The contact is not found."<<endl;
         if(s=='n')
        {
            p->Show();
             cout<<endl;
            cout<<"The contact is not deleted\n";
        }
}

void addfromtxtfile(AddrList &addrlist)         //从正文文件中添加数据到库表中 
{ 
    cout<<"请输入源文件的文件名(扩展名为txt):"; 
    char filename[80]; 
    cin.get();
    cin.getline(filename, 80); 
    ifstream in(filename, ios::in|ios::_Nocreate); 
    if(!in)
    { 
        cout<<"无法打开源文件";return; 
    } 
    char na[10],m[15],e[20],t[128]; 
    int o;
    Node *p; 
    while(!in.eof()) 
    {
        in>>na>>o>>m>>e>>t;
        p=new Node(na,o,m,e,t);
        addrlist.AddTail(p); 
    } 
    in.close();
    cout<<"记录从正文文件添加成功!";
    system("pause"); 
} 
void writetotxtfile(AddrList &addrlist)            //将链表中的数据写入指定的正文文件 
{
    cout<<"请输入目标文件的文件名(扩展名为txt):"; 
    char filename[80]; 
    cin.get(); 
    cin.getline(filename, 80); 
    ofstream out; 
    out.open(filename, ios::out|ios::_Noreplace); 
    if(!out)
    {
        cout<<"创建文件失败!";
    }
    else
    { 
        Node *p; 
        p=addrlist.GetHeadPtr(); 
        while(p) 
        {
            out<<p->Name<<" "<<p->Phone<<" "<<" "<<p->Mobile<<" "<<p->EMail<<" "<<p->Time<<" "<<endl;
            p=p->next; 
        } 
        out.close(); 
        cout<<"成功导出正文文件!"; 
    } 
} 
void quit(AddrList &addrlist)
{
    cout<<endl<<endl<<"*****************谢谢使用!****************\n";
    cout<<endl;
    exit(0);
}
void main()
{
    int choice=0,state;
    AddrList addrlist;
    while(choice!=8)
    {
        DisplayMenu();      //显示主菜单
        cout<<"请输入选择:1~8\n";
        cin>>choice;
        state=cin.rdstate();
        if(state)           //处理非法输入,如输入一个字符是非法的
        {
            char str[80];
            cin.clear();
            cin.getline(str,80);
            choice=10;
        }
        system("cls");        
        switch(choice)
        {
            case 1:add(addrlist);break;
            case 2:del(addrlist);break;
            case 3:showall(addrlist);break;
            case 4:search(addrlist);break;
    //        case 5:modify(addrlist);break;
            case 6:addfromtxtfile(addrlist);break;
            case 7:writetotxtfile(addrlist);break;
            case 8:quit(addrlist);break;
            default:;
        }
        system("cls");
    }
}


调试的时候貌似删除那块出问题了,录如信息后,删除后边的结点会把前边的都给删除~~void Delete(char *name)        //删除p结点
    {
        Node *p,*head1;
        p=LookUp(name);
        head1=Head;
        if(head1=p)
        {
            Head=p->next;
            delete p;
        }
        else
        {
            head1->next=p->next;
            delete p;                    //目测是这块的问题~~求各位前辈释疑~~~
        }
    }
求解先行谢过!!!!
搜索更多相关主题的帖子: 管理系统 信息 
2013-08-08 17:22
x_tacet
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-8-7
收藏
得分:0 
更正一下~if(head1==p)
2013-08-08 20:01
快速回复:正在改着一位前辈的信息管理系统程序~关于删除结点的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028999 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved