合并两个单链表以形成第三个的功能

时间:2017-04-26 17:10:31

标签: c++ list merge

所以我正在编写一个程序,其中用户用整数填充两个链表,现在我需要创建一个函数,创建第三个链表,其中包含来自第一个和第二个列表的值,没有重复。 这是我现在的代码:

#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;
}

所以现在用户可以将元素输入到两个列表并查看它们,但是如何创建一个函数,它将合并那些没有重复值的列表?例如: List1 = 1,2,3,6,8; List2 = 2,4,5,7,8;

合并列表= 1,2,3,4,5,7,8; (实际上可以是任何顺序,也无关紧要)

任何建议将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

这是您的代码的一个版本,但使用STL列表容器来提供链接列表,这提供了自己的功能,可以实现您想要的功能。此代码用于演示,并未针对效率进行优化,它仅显示了合并两个列表中的唯一元素的一种可能方法。 (为此,列表需要对其元素进行排序,并且需要为列表中的节点提供&#34;小于&#34;和#34;等于&#34;的谓词。) (确实需要时间来学习一些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;
}