| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 585 人关注过本帖
标题:C++中实现两个链表的交集 求找错
只看楼主 加入收藏
麦迪依然
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-3-22
结帖率:75%
收藏
 问题点数:0 回复次数:1 
C++中实现两个链表的交集 求找错
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
    string name;
    string sex;
    long number;
    double score;
    Student* prior;
    Student* next;
public:
    Student()
    {
        number=NULL;
        score=NULL;
        prior=next=NULL;
    }
    ~Student()
      {
         cout<<name;
         cout<<"调用学生析构函数\n";
         prior=NULL;
         next=NULL;
         delete prior;
         delete next;
    }
};

class List
{
protected:
    Student*Head;
    Student*Tail;
    int Length;
public:
    List();
    ~List();
    void Create();
    void Insert_Before();
    void Insert_After();
    void Print();
    void Delete();
    void Search();
    void Change();
};

List::List()
{
    Head=Tail=NULL;
    Length=0;
}

void List::Create()
{
    string name1,sex1;
    long number1;
    double score1;
    cout<<"开始生成学生管理系统:\n";
    cout<<"操作要求:依次输入学生的姓名、性别、学号和成绩,以姓名为#时表示输入结束.\n";
    cout<<"请开始输入:\n";
    cin>>name1>>sex1>>number1>>score1;
    Student *p,*q;
    while(name1!="#")
    {
        p=new Student,
        p->name=name1;
        p->sex=sex1;
        p->number=number1;
        p->score=score1;
        
        if(!Head)
        {
            Head=p;
            q=p;
        }
        else
        {
            q->next=p;
            p->prior=q;
            q=p;
        }
        Length++;
        cout<<"请继续输入:\n";
        cin>>name1>>sex1>>number1>>score1;
    }
    Tail=p;
}

List::~List()
{
    Student *p,*q;
    for(p=Head;p;)
    {
        q=p;
        p=p->next;
        delete q;
    }
    Head=NULL;
    Tail=NULL;
    delete Head;
    delete Tail;
    Length=NULL;
    cout<<"调用链表析构函数\n";
}

void List::Print()
{
    Student *p;
    cout<<"学生信息为:\n";
    for(p=Head;p;p=p->next)
    {
        cout<<"***************************\n";
        cout<<"学生姓名:"<<p->name<<endl;
        cout<<"学生性别:"<<p->sex<<endl;
        cout<<"学生学号:"<<p->number<<endl;
        cout<<"学生成绩:"<<p->score<<endl;
        cout<<"***************************\n";
        cout<<endl;
    }
}


void List::Insert_Before()
{
    int key,i;
    Student *p,*q;
    cout<<"在学生前插入一个学生,请输入该学生的序号:";
    cin>>key;
    while(key>Length)
    {
        cout<<"输入错误,请重新输入:";
        cin>>key;
    }
    for(p=Head,i=1;i<key;p=p->next,i++);
    q=new Student;
    cout<<"请依次输入学生的姓名、性别、学号和成绩.\n";
    cin>>q->name>>q->sex>>q->number>>q->score;
    q->next=p;
    q->prior=p->prior;
    p->prior->next=q;
    p->prior=q;
    Length++;
    cout<<"\n插入成功!\n";
}

void List::Insert_After()
{
    int key,i;
    Student *p,*q;
    cout<<"在学生后插入一个学生,请输入该学生的序号:";
    cin>>key;
    while(key>Length)
    {
        cout<<"输入错误,请重新输入:";
        cin>>key;
    }
    for(p=Head,i=1;i<key;p=p->next,i++);
    q=new Student;
    cout<<"请依次输入学生的姓名、性别、学号和成绩.\n";
    cin>>q->name>>q->sex>>q->number>>q->score;
    if(key==Length)
    {
        p->next=q;
        q->prior=p;
    }
    else
    {
        q->next=p->next;
        q->prior=p;
        p->next->prior=q;
        p->next=q;
    }
    Length++;
    cout<<"\n插入成功!\n";
}

void List::Delete()
{
    string key;
    Student *p;
    cout<<"请输入您要删除的学生姓名:\n";
    cin>>key;
    for(p=Head;p&&p->name!=key;p=p->next);
    while(!p)
    {
        cout<<"\n没有要删除的学生,请重新输入学生姓名:\n";
        cin>>key;
        for(p=Head;p&&p->name!=key;p=p->next);
    }
    if(p->next)
    {
        p->prior->next=p->next;
        p->next->prior=p->prior;
    }
    else  p->prior->next=NULL;
    delete p;
    cout<<"\n删除成功!\n";
}

void List::Search()
{
    string key;
    Student *p;
    cout<<"请输入您要查询的学生姓名:";
    cin>>key;
    for(p=Head;p&&p->name!=key;p=p->next);
    if(!p)
        cout<<"没有该学生的信息.\n";
    else
    {
        cout<<"查找成功!该学生的信息为:\n";
        cout<<"***************************\n";
        cout<<"学生姓名:"<<p->name<<endl;
        cout<<"学生性别:"<<p->sex<<endl;
        cout<<"学生学号:"<<p->number<<endl;
        cout<<"学生成绩:"<<p->score<<endl;
        cout<<"***************************\n";
    }
}

void List::Change()
{
    string key;
    Student *p;
    cout<<"\n请输入您要修改学生信息的的学生姓名:";
    cin>>key;
    for(p=Head;p&&p->name!=key;p=p->next);
    if(!p)
        cout<<"没有该学生的信息.\n";
    else
    {
        char choice;
        cout<<"可修改该学生的学号或者成绩,请选择:A.修改学号 B.修改成绩.\n";
        cin>>choice;
        switch(choice)
        {
        case 'A':
            cout<<"请重新输入学生的学号:\n";
            cin>>p->number;
            break;
        case 'B':
            cout<<"请重新输入学生的成绩:\n";
            cin>>p->score;
            break;
        }
        cout<<"修改成功!该学生的信息为:\n";
        cout<<"***************************\n";
        cout<<"学生姓名:"<<p->name<<endl;
        cout<<"学生性别:"<<p->sex<<endl;
        cout<<"学生学号:"<<p->number<<endl;
        cout<<"学生成绩:"<<p->score<<endl;
        cout<<"***************************\n";
    }
}

class Set:public List
{
public:
    void And(Set,Set);//求两个学生链表的交集
   };
void Set::And(Set S1,Set S2)
{
    Student *p,*q,*r,*m;
    p=S1.Head;
    while(p)
    {
        q=S2.Head;
        while(q)
        {
            if(p->number==q->number)
            {
                r=new Student;
                r->name=p->name;
                r->number=p->number;
                r->sex=p->sex;
                r->score=p->score;
                if(!Head)
                {
                    Head=r;
                    m=r;
                }
                else
                {
                    m->next=r;
                    r->prior=m;
                    m=r;
                }
                Length++;
                Tail=m;
                break;
            }
            else q=q->next;
        }
        p=p->next;
    }
}
int main()
{
    Set A,B,C;
    A.Create();
    B.Create();
    cout<<"两个学生链表的交集为:\n";
    C.And(A,B);
    C.Print();
    return 0;
}

为什么一运行到return 0;前面就出错了呢?老是出一堆类似汇编语言的东西  求高手速速解惑  不胜感激!
搜索更多相关主题的帖子: next double include public number 
2012-05-31 08:59
麦迪依然
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-3-22
收藏
得分:0 
求速回!
2012-06-01 11:09
快速回复:C++中实现两个链表的交集 求找错
数据加载中...
 
   



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

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