| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 498 人关注过本帖
标题:类和链表
只看楼主 加入收藏
笋尖兰
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-11-27
收藏
 问题点数:0 回复次数:1 
类和链表
在调试的时候,当我选择1.3.8.10的操作时,它总能输出正确的答案,但紧接着程序就被终止了.那个错误提示没看懂,谁能教我一下吗?
还有就是,怎么让它循环操作啊?比如说当用户输入11的时候,程序终止?我希望里面的内容是连续的,不是每进行一次,数据就初始化一次
万分感谢!
(我还没学到模板...)



C/C++ code#include <iostream>
using namespace std;
struct Node
{
    int content;
    Node *next;
};
class Set
{    Node *node;   
    Node *head;
public:
    Set()
    {
        head=NULL;
    }
    Set(const Set& s)
   {                              
       Node *p=s.head;
       Node *q;//=head; head此时的值为NULL,对q的操作都建立在空指针之上,如何不错!!!
       if(this==&s) return;
       for(;p!=NULL;p=p->next,q=q->next)
        {
         q=new Node;
         if(head==NULL) head=q;
         q->content=p->content;
        }
       q=NULL;
   }
  ~Set()
    {
        Node *q=head;
        while (q)
        {
            Node* del = q;//这里要注意,不能直接delete q,否则q=q->next就没意义了。
            q = q->next;
            delete del;
        }
    }
    bool is_empty() const
    {
        return (head==NULL);
    }
    int size() const
    {
        int count=0;
        Node *q=head;
        for(;q!=NULL;q=q->next)
            count++;
        return count;
    }
    bool is_element(int e) const
    {
        Node *q=head;
        for(;q!=NULL;q=q->next)
            if(q->content==e)
                return true;
        return false;
    }
    bool is_subset(const Set& s) const
    {
        if(s.size()>size())
            return false;
        else
        {    Node *q=s.head;
            for (;q!=NULL;q=q->next)
                if(is_element(q->content)==false)
                    break;
            return (q==NULL);
        }
    }
    bool is_equal(const Set& s) const
    {
        return (is_subset(s) && s.is_subset(*this));
    }
    void display() const
    {
        Node *q=head;
        for(;q!=NULL;q=q->next)
            cout<<q->content<<'\t';
        cout<<endl;
    }
    Set& insert(int e)
    {
        if(is_element(e))
            cout<<e<<"已经在该集合中!"<<endl;
        else
        {    Node *p=new Node;//p必须初始化;
            p->content=e;
            p->next=head;
            head=p;
        }
        return *this;
    }
    Set& remove(int e)
    {
        Node *p=head;
        if(p->content==e)
        {    head=p->next;
            delete p;
        }
        else
        {    Node *q=p;
            p=p->next;
            for(;p!=NULL;p=p->next,q=q->next)
                if(p->content==e)
                    break;
            q->next=p->next;
            delete p;
        }
        return *this;
    }
    Set union2(const Set& s) const
    {
        Set bing(*this);
        Node *p=s.head;
        for(;p!=NULL;p=p->next)
            if(!is_element(p->content))
                bing.insert(p->content);
        bing.display();
        return bing;
    }
    Set intersection(const Set& s) const
    {
        Set jiao;
        Node *p=s.head;
        for(;p!=NULL;p=p->next)
            if(is_element(p->content))
                jiao.insert(p->content);
        jiao.display();
        return jiao;
    }
    Set difference(const Set& s) const
    {
        Set cha(*this);
        Node *p=s.head;
        for(;p!=NULL;p=p->next)
            if(is_element(p->content))
                cha.remove(p->content);
        cha.display();
        return cha;
    }
};
void creat(Set& A)
{   
    int num,pd=2;
    cin>>num;
    if(num==-1)
    {    cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
        cin>>pd;
    }
    while(pd==2)
    {    A.insert(num);
        cin>>num;
        if(num==-1)
        {    cout<<"请问这个-1是代表结束吗?输入1代表结束,输入2代表是集合里的值:";
            cin>>pd;
        }
    }
}
void at(Set A,int element)
{
    if(A.is_element(element))
        cout<<"有该元素"<<endl;
    else
        cout<<"没有该元素"<<endl;
}
void kongji(Set A)
{
    if(A.is_empty())
        cout<<"是空集"<<endl;
    else
        cout<<"不是空集"<<endl;
}            
int main()
{
    Set A,B;
    cout<<"请输入集合A中的元素,以-1结束:"<<endl;
    creat(A);
    cout<<"请输入集合B中的元素,以-1结束:"<<endl;
    creat(B);
        cout<<"请选择要做的操作:"<<endl;
        cout<<"1.判断是否为空集"<<endl;
        cout<<"2.获取元素个数"<<endl;
        cout<<"3.判断某元素是否在集合中"<<endl;
        cout<<"4.判断两集合的大小关系"<<endl;
        cout<<"5.显示集合中的所有元素"<<endl;
        cout<<"6.添加元素"<<endl;
        cout<<"7.删除元素"<<endl;
        cout<<"8.计算集合的并集"<<endl;
        cout<<"9.计算集合的交集"<<endl;
        cout<<"10.计算集合的差"<<endl;
        int pd;
        cin>>pd;
        switch(pd)
        {    case 1: cout<<"A";
                    kongji(A);
                    cout<<"B";
                    kongji(B);
                    break;
            case 2:cout<<"A中有"<<A.size()<<"个元素,B中有"<<B.size()<<"个元素。"<<endl;
                   break;
            case 3: cout<<"请输入该元素:";
                    int element;
                    cin>>element;
                    cout<<"集合A中";
                    at(A,element);
                    cout<<"集合B中";
                    at(B,element);
                    break;
            case 4:if(A.is_equal(B))
                       cout<<"两集合相等"<<endl;
                    else if (A.is_subset(B))
                        cout<<"B包含于A"<<endl;
                    else if(B.is_subset(A))
                        cout<<"A包含于B"<<endl;
                    else
                        cout<<"两者没有大小关系"<<endl;
                    break;
            case 5:cout<<"A中的元素为:"<<endl;
                   A.display();
                   cout<<"B中的元素为:"<<endl;
                   B.display();
                   break;
            case 6:cout<<"请输入所要添加的元素(一次只能添一个):";
                   int add;
                   cin>>add;
                   cout<<"输入1添加到集合A,输入2添加到集合B:";
                   char ch;
                   cin>>ch;
                   if(ch=='A')
                       A.insert(add);
                   else
                       B.insert(add);
                   break;
            case 7:cout<<"请输入所要删除的元素(一次只能删一个):";
                   int shan;
                   cin>>shan;
                   cout<<"输入1对集合A执行,输入2对集合B执行:";
                   char ch2;
                   cin>>ch2;
                   if(ch2=='A')
                       A.remove(shan);
                   else
                       B.remove(shan);
                   break;
            case 8:A.union2(B);break;
            case 9:A.intersection(B);break;
            case 10:cout<<"A-B为{";
                    A.difference(B);
                    cout<<"}"<<endl;
                    cout<<"B-A为{";
                    B.difference(A);
                    cout<<"}"<<endl;break;
            default:cout<<"Input Error!"<<endl;
        }
    return 0;
}
搜索更多相关主题的帖子: 链表 
2008-12-07 00:05
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
最好先说是什么错误?不要一上来就贴一大堆代码,这样很少有人耐心去看得

天行健,君子以自强不息!!QQ:68660681
2008-12-08 17:12
快速回复:类和链表
数据加载中...
 
   



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

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