Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将两个单独链接的列表合并为第三个列表的函数_C++_List_Merge - Fatal编程技术网

C++ 将两个单独链接的列表合并为第三个列表的函数

C++ 将两个单独链接的列表合并为第三个列表的函数,c++,list,merge,C++,List,Merge,所以我正在编写一个程序,其中用户用整数填充两个链表,现在我需要创建一个函数,用第一个和第二个链表中的值创建第三个链表,而不使用重复项。 以下是我现在掌握的代码: #include <iostream> #include <stdlib.h> using namespace std; struct node { int info; node *next; node (int i, node *n=NULL) { info =

所以我正在编写一个程序,其中用户用整数填充两个链表,现在我需要创建一个函数,用第一个和第二个链表中的值创建第三个链表,而不使用重复项。 以下是我现在掌握的代码:

#include <iostream>
#include <stdlib.h>
using namespace std;

struct node
{
    int info;
    node *next;
    node (int i, node *n=NULL)
    {
        info = i;
        next = n;
    }
    ~node() 
    {
        cout <<"NODE CONTAINING \"" << info << "\" WAS DELETED!" << endl;
    }
};
struct list
{
    node* startList1, *lastList1, *startList2, *lastList2;
    int menuOption;
    int nodeCount1=0, nodeCount2=0; 


    list() {
        startList1 = NULL;
        startList2 = NULL;

    }


    void addList1(node *p)
    {
        int n;
        cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE:";
        cin >> n;
        p = new node(n);
        nodeCount1++;
        if(startList1==NULL)
        {
            startList1 = lastList1 = p;
        }
        else
        {
            lastList1->next = p;
            lastList1 = p;
        }
    }

    void printList1(node *pr)
    {
        node *pr;
        for (pr=startList1; pr!=NULL; pr=pr->next)
        {
            cout << pr->info << endl;
        }
    }



    void addList2(node *q)
    {
        int n;
        cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE:";
        cin >> n;
        q = new node(n);
        nodeCount2++;
        if(startList2==NULL)
        {
            startList2 = lastList2 = q;
        }
        else
        {
            lastList2->next = q;
            lastList2 = q;
        }
    }
    void printList2(node *pr)
    {
        for (pr=startList2; pr!=NULL; pr=pr->next)
        {
            cout << pr->info << endl;
        }
    }

   // this just prints first and second lists to show what is inside..
    void printBoth(node *pr, node *qr)
    {
        cout << "Elements of the first list:" << endl;
        for (pr=startList1; pr!=NULL; pr=pr->next)
        {
            cout << pr->info << endl;
        }
        cout << "Elements of the second list:" << endl;
        for (pr=startList2; pr!=NULL; pr=pr->next)
        {
            cout << pr->info << endl;
        }
    }


    void printMenu()
    {
        cout << "MENU" << endl;
        cout << "(1) ADD ELEMENT LIST1." << endl;
        cout << "(2) PRINT LIST1" << endl;
        cout << "(3) ADD ELEMENT LIST2" << endl;
        cout << "(4) PRINT LIST2" << endl;
        cout << "(5) PRINT BOTH LISTS" << endl;
    cout << "(6) USE MERGE FUNCTION" << endl;
        cout << "(7) TO EXIT" << endl;

        cin >> menuOption;
        system ("cls");
    };


    void dragons()
    {

        node *temp1 = startList1;
        node *temp2 = startList2;
        while(temp1)
        {
            temp1 = startList1->next;
            delete startList1;
            startList1=temp1;
        }
        while(temp2)
        {
            temp2 = startList2->next;
            delete startList2;
            startList2=temp2;
        }
    };      

};

int main()
{
    struct node *p = NULL, *q = NULL;

    list s;
    s.printMenu();
    node* list1;
    node* list2;
    node* sorting;
    while(s.menuOption!=7)
    {
        switch (s.menuOption)
        {
            case 1: s.addList1(list1);
                    break;
            case 2: s.printList1(list1);
                    break;
            case 3: s.addList2(list2);
                    break;
            case 4: s.printList2(list2);
                    break;
            case 5:s.printBoth(list1, list2);
                    break;
            case 6:s.merge();
                    break;
            default: cout << "SOMETHING WENT WRONG!!!!" << endl;
                    break;
        }
        system ("pause");
        system ("cls");
        s.printMenu();
    }
    s.dragons();
    return 0;
}
#包括
#包括
使用名称空间std;
结构节点
{
国际信息;
节点*下一步;
节点(int i,节点*n=NULL)
{
info=i;
next=n;
}
~node()
{
(下一步)
{
计算机信息;
q=新节点(n);
nodeCount2++;
如果(STARTIST2==NULL)
{
STARTIST2=lastList2=q;
}
其他的
{
lastList2->next=q;
lastList2=q;
}
}
无效打印列表2(节点*pr)
{
for(pr=strist2;pr!=NULL;pr=pr->next)
{

cout info这是您的代码的一个版本,但是使用STL列表容器来提供链接列表,这提供了它自己的功能,可以实现您想要的功能。此代码用于演示,并没有优化效率,它只是显示了一种可能的方法,可以合并两个列表中的唯一元素。(为此,列表要求对其元素进行排序,并且需要为列表中的节点提供“小于”和“等于”的谓词。) (学习一些STL容器和函数确实需要时间,但许多人建议这样做,而不是从头开始创建自己的基于指针的原始链表。)

希望这有帮助,或者是有兴趣的

#include <iostream>
#include <stdlib.h>
#include <list>

struct node
{
    int info;
    node (int i)
    {
        info = i;
    }
    ~node()
    {
        //std::cout <<"NODE CONTAINING \"" << info << "\" WAS DELETED!" << std::endl;
    }
};

bool nodeLess(node n1, node n2)
{
    return n1.info < n2.info;
}

bool nodeEqual(node n1, node n2)
{
    return n1.info == n2.info;
}

struct mylist
{
    int menuOption;

    std::list<node> list1;
    std::list<node> list2;

    void addList(std::list<node>& l)
    {
        int x;
        std::cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE: ";
        std::cin >> x;
        node n(x);
        l.push_back(n);
    }

    void printList(const std::list<node>& l)
    {
        for (std::list<node>::const_iterator it = l.cbegin(); it != l.cend(); ++it)
        {
            std::cout << it->info << std::endl;
        }
    }

    void addList1()  { addList(list1); }
    void addList2()  { addList(list2); }

    void printList1() { printList(list1); }
    void printList2() { printList(list2); }

    // this just prints first and second lists to show what is inside..
    void printBoth()
    {
        std::cout << "Elements of the first list:" << std::endl;
        printList1();
        std::cout << "Elements of the second list:" << std::endl;
        printList2();
    }

    void simpleMerge()
    {
        std::list<node> merged;
        merged.insert(merged.end(), list1.begin(), list1.end());
        merged.insert(merged.end(), list2.begin(), list2.end());
        std::cout << "CONTENTS OF LIST1 THEN LIST2: " << std::endl;
        printList(merged);
    }

    void uniqueSortMerge()
    {
        std::list<node> sorted1(list1.begin(), list1.end());
        std::list<node> sorted2(list2.begin(), list2.end());
        sorted1.sort(nodeLess);
        sorted2.sort(nodeLess);
        sorted1.unique(nodeEqual);
        sorted2.unique(nodeEqual);
        std::list<node> merged;
        std::merge(sorted1.begin(), sorted1.end(),
                   sorted2.begin(), sorted2.end(),
                   std::back_inserter(merged),
                   nodeLess);
        std::cout << "UNIQUE CONTENTS OF LIST1 AND LIST2 SORTED AND MERGED: " << std::endl;
        printList(merged);
    }

    void printMenu()
    {
        std::cout << "MENU" << std::endl;
        std::cout << "(1) ADD ELEMENT LIST1." << std::endl;
        std::cout << "(2) PRINT LIST1" << std::endl;
        std::cout << "(3) ADD ELEMENT LIST2" << std::endl;
        std::cout << "(4) PRINT LIST2" << std::endl;
        std::cout << "(5) PRINT BOTH LISTS" << std::endl;
        std::cout << "(6) USE SIMPLE MERGE FUNCTION" << std::endl;
        std::cout << "(7) USE UNIQUE, SORT AND MERGE FUNCTION" << std::endl;
        std::cout << "(8) TO EXIT" << std::endl;

        std::cin >> menuOption;
        system ("cls");
    };


    void dragons()
    {
        list1.clear();
        list2.clear();
    };
};

int main()
{
    mylist s;

    do
    {
        s.printMenu();
        switch (s.menuOption)
        {
            case 1:
                s.addList1();
                break;
            case 2:
                s.printList1();
                break;
            case 3:
                s.addList2();
                break;
            case 4:
                s.printList2();
                break;
            case 5:
                s.printBoth();
                break;
            case 6:
                s.simpleMerge();
                break;
            case 7:
                s.uniqueSortMerge();
                break;
            case 8:
                break;
            default:
                std::cout << "SOMETHING WENT WRONG!!!!" << std::endl;
                break;
        }
        system ("pause");
        system ("cls");
    } while(s.menuOption != 8);

    s.dragons();
    return 0;
}
#包括
#包括
#包括
结构节点
{
国际信息;
节点(int i)
{
info=i;
}
~node()
{

//std::cout如果无法从链表中检索数据,链表有什么用?请注意,您的整个
结构列表
无法从
main
获取添加到其中的数据,也无法从
main
从头到尾进行迭代。这将是获取
merge()的第一步抱歉,我是C++的新手,我不太明白你在说什么。我应该怎么做“从主数据中添加数据,或者从主开始到最后迭代”。?您构建了一个链表类,无法从
list
结构外部读取这些列表中的数据。其次,
addList1
addList2
函数不会更改传入的参数。您将未初始化的指针发送到
addList1
addList2
,它们将重新初始化返回这些函数时处于未初始化状态。如果需要验证,请在调用
add…()
函数。调用这些函数后,您将看到它们仍然是
nullptr
。因此,无论何时使用
list1
list2
时,您的整个
main
都有bug。