链表联盟

时间:2012-04-04 09:31:12

标签: c++

在我的“void union”函数中,我不确定如何从用户分别输入的链接列表“A”和“B”中插入数据,因为“AUB”不在void函数内。我会说:

AUB.insert

但我不确定。有什么建议吗?

#include "stdafx.h"
#include <iostream>

using namespace std;

class Sets
{
private:struct NODE
        {
            int info;
            NODE *next;
        };
        NODE *list;
public:Sets()
       {
           list=NULL;
       }
       void Insert(int x)
       {
           NODE *p=list, *q=list, *r;
           //create a new node
           r = new (NODE);
           r->info = x;
           r->next = NULL;
           //find the insertion place
           while(p != NULL && p->info < x)
           {
               q=p;
               p=p->next;
           }
           if(p==list)//x is the first info
           {
               list=r;
               r->next=p;
           }
           else if(p==NULL)//x is the last info
           {
               q->next=r;
           }
           else //x is neither forst nor last info
           {
               r->next=p;
               q->next=r;
           }
       }
        void display()
        {
            NODE *p=list;
            while(p != NULL)
            {
                cout << p->info << "-->";
                p=p->next;
            }
            cout << "NULL\n";
        }
        void Union(Sets setA,Sets setB)
        {
            NODE *p=setA.list, *q=setB.list;
            while(p != NULL && q != NULL)
            {
                if(p->info > q-> info)
                {
                    (q->info)
                    q=q->next;
                }
                else if(p->info == q->info)
                {
                    insert(p->info)
                    p=p->next;
                    q=q->next;
                }
                else//P->info < q->info
                {
                    insert(p->info);
                    p=p->next;
                }
            }
            while(p !=NULL)
            {
                insert(p->info);
                p=p->next;
            }
            while(q != NULL)
            {
                insert(q->info);
                q=q->next;
            }
        }
};


int main()
{   
    //create a set of integers
    int x;
    Sets A, B, setAUB;
    cout << "Enter data for setA:\n";
    cout << "Enter a group of positive integer numbers with -1 at the end end: ";
    cin >> x;
    while(x != -1)
    {
        A.Insert(x);
        cin >> x;
    };
    //display setA
    cout << endl << "setA=";
    A.display();

    cout << "Enter data for setB:\n";
    cout << "Enter a group of positive integer numbers with -1 at the end end: ";
    cin >> x;
    while(x != -1)
    {
        B.Insert(x);
        cin >> x;
    };
    //display setB
    cout << endl << "setB=";
    B.display();

    setAUB.Union(A, B);
    //display setAUB
    cout << endl << "setAUB=";
    setAUB.display();

    system ("pause");

    //terminate program
    return 0;
}; 

1 个答案:

答案 0 :(得分:0)

您定义它:void Union(Sets setA,Sets setB)

你正在做什么?两者都按值传递,返回值为void - 结果在哪里?

您当前的对象(this函数中的Union)是否成为该联盟?如果是这样,那里的数据会发生什么变化?你没有删除它,所以你基本上合并了三套,而不是两套......

我建议创建一个静态 merge函数,该函数将采用这两个参数并返回一个列表是两者的合并。

否则,创建一个常规merge函数,该函数只接受1个参数,并将其合并到当前对象中。

顺便说一下 - 为什么Sets,当它显然是一个有序的链表?

相关问题