回 帖 发 新 帖 刷新版面

主题:这是等价类划分的stack解法,是正确的

//=============================================类声明=====================================================
#include<iomanip.h>
template <class T> class iterator;
template <class T> class list;
template <class T> class stack;
template <class T>
class node
{
    friend class list<T> ;
    friend class iterator<T>;
    friend class stack<T> ;
    private:
        T data;
        node<T>  * next;
};
//----------
template <class T>
class list
{
    friend class iterator<T>;
    public:
        list(){first=NULL;}
        ~list();
        bool insert(int a,T b);
        int length();
    private:
        node<T> * first;
};

//----------
template <class T>
class iterator
{
    public:
        T * init(list<T> & c)
        {
            location=c.first;
            if(location==0)
                return 0;
            else
                return &location->data;
        }
        T * next()
        {
            if(location==0)
                return 0;
            else
            {
                location=location->next;
                if(location==0)
                    return 0;
                else
                    return &location->data;
            }
        }
    private:
        node<T> * location;
};
//----------
template <class T>
class stack
{
    public:
        stack(){top=NULL;}
        ~stack();
        void push(T a);
        bool pop(T& savepop);
        bool empty();
        
    private:
        node<T> * top;
};
//=================================================类函数声明================================================
template<class T>
list<T>::~list()
{
    node<T>* p;
    for(int i=0;first;i++)
    {
        p=first;
        first=first->next;
        delete p;
    }
}

//----------
template<class T>
int list<T>::length()
{
    node<T>* p=first;
    for(int i=0;p;i++)
        p=p->next;
    return i;
}
//----------
template<class T>
bool list<T>::insert(int a,T input)
{
    node<T> * p=first;
    node<T> * q=new node<T>;
    q->data=input;
    int t=length();
    if(a<0 ||a>t)
        return 0;
    if(a==0)
    {
        q->next=first;
        first=q;
    }
    else
    {
        for(int i=1;i<a;i++)
            p=p->next;
        q->next=p->next;
        p->next=q;
    }
    return 1;
}
//----------
template<class T>
stack<T>::~stack()
{
    node<T>* p;
    for(int i=0;top;i++)
    {
        p=top;
        top=top->next;
        delete p;
    }
}
//----------
template<class T>
void stack<T>::    push(T a)
{
    node<T>* p=new node<T>;
    p->data=a;
    p->next=top;
    top=p;
}
//----------
template<class T>
bool stack<T>:: pop(T& savepop)
{
    if(top==NULL)
    return 0;
    else
    {
        savepop=top->data;
        node<T>* p=top;
        top=top->next;
        delete p;
    }
    return 1;
}
//----------
template<class T>
bool stack<T>::empty()
{return top==NULL;}
//=========================================等价类划分主程序=====================================
void main()
{
    stack<int> c;
    int total,num;
    cout<<"输入元素总个数";
    cin>>total;
    cout<<"输入关系总个数";
    cin>>num;
    list<int> * l;
    l=new list<int> [total+1];
    bool * label;
    label=new bool [total+1];
    for(int i=1;i<=total;i++)
        label[i]=false;
    cout<<"输入各个配对的二元关系"<<endl;
    for(int j=1;j<=num;j++)
    {
        int a,b;
        cout<<"第"<<j<<"组:";
        cin>>a>>b;        
        l[a].insert(0,b);
        l[b].insert(0,a);
    }
    for(int k=1;k<=total;k++)
    {
        if(label[k]==false)
        {
            cout<<"等价类"<<k<<": ";
            label[k]=true;
            c.push(k);
            cout<<k<<" ";
            while(!c.empty())
            {
                int savedeq;
                c.pop(savedeq);
            
                iterator<int> p;
                int * q=p.init(l[savedeq]);
                while(q)
                {
                    if(label[*q]==false)
                    {
                        label[*q]=true;
                        cout<<*q<<" ";
                        c.push(*q);
                    }
                    q=p.next();
                }
            }
            cout<<endl;
        }
    
    }
int u;
cin>>u;


}

        







回复列表 (共1个回复)

沙发


我来回复

您尚未登录,请登录后再回复。点此登录或注册