当前位置: 首页 > 知识库问答 >
问题:

如何合并排序已经排序的链表?

焦博实
2023-03-14

我写了一个合并两个已经排序的链表的方法。然而,由于某种原因,列表的最后一个节点没有打印出来。有什么想法吗?

#include <iostream>
using namespace std;

struct node {
        int number;
        node *next;
        node *prev;
};

    void mergeSort(node*& head, node*& head1);

    int main(int argc, const char * argv[])
    {

//Linked List #1 
        node *head;
        node *tail;
        node *curr;

//1st node
        curr = new node;
        curr-> number = 1;
        curr-> prev = NULL;
        head = curr;
        tail = curr;

        //2nd node
        curr = new node;
        curr-> number = 3;
        curr -> prev = tail;
        tail -> next = curr;
        tail = curr;

        //3rd node
        curr = new node;
        curr-> number = 5;
        curr -> prev = tail;
        tail -> next = curr;
        tail = curr;

        //4th node (tail)
        curr = new node;
        curr-> number = 7;
        curr -> prev = tail;
        tail -> next = curr;
        tail = curr;
        tail->next = NULL;

        //Print Linked List #1
        cout<< "Linked List #1: " << endl;
        curr = head;
        while (curr != NULL){
            cout << curr-> number;
            curr = curr->next;
        }
        cout << endl;

    //Linked List #2
        node *head1;
        node *tail1;
        node *curr1;

        //1st node
        curr1 = new node;
        curr1-> number = 2;
        curr1-> prev = NULL;
        head1 = curr1;
        tail1 = curr1;

        //2nd node
        curr1 = new node;
        curr1-> number = 4;
        curr1 -> prev = tail1;
        tail1 -> next = curr1;
        tail1 = curr1;

        //3rd node
        curr1 = new node;
        curr1-> number = 6;
        curr1 -> prev = tail1;
        tail1 -> next = curr1;
        tail1 = curr1;

        //4th node (tail)
        curr1 = new node;
        curr1-> number = 8;
        curr1 -> prev = tail1;
        tail1 -> next = curr1;
        tail1 = curr1;
        tail1->next = NULL;

        //Print Linked List #2
        cout<< "Linked List #2: " << endl;
        curr1 = head1;
        while (curr1 != NULL){
            cout << curr1-> number;
            curr1 = curr1->next;
        }
        cout << endl;

        //Call MergeSort function
        mergeSort(head, head1);


        return 0;
    }

下面是链接列表的合并排序方法。

    void mergeSort(node*& head, node*& head1){

        //Set up the Merge Sorted Linked List

        node *head2;
        node *tail2;
        node *curr2;

        node *curr = head;
        node *curr1 = head1;

        node* next = curr->next;
        node* next1 = curr1->next;
        node* next2 = curr2->next;

        //1st NODE
        curr2 = new node;

        if (curr->number > curr1->number){
            curr2->number = curr1->number;
            curr1 = curr1->next;
            curr1 = next1;
        }
        else if (curr1->number > curr->number){
            curr2->number = curr->number;
            curr = curr->next;
            curr = next;
        }


        //Set prev of head to Null
        curr2 -> prev = NULL;
        //Set head
        head2 = curr2;
        //Set tail
        tail2 = curr2;



        //BODY NODES
        while (curr != NULL && curr1 != NULL ){
            curr2 = new node;



        //compare the data between the two nodes

            //compare the data between the two nodes
            if (curr1->number >= curr -> number){
                //set the new node's data to the smallest of the previous 
              //node's datas
                //from the other two Linked Lists
                curr2 -> number = curr -> number;

                //link the new node to the previous
                curr2 -> prev = tail2;
                //attach the predecessor node's next pointer to the current         
                //node
                tail2 -> next = curr2;
                //set the new node as the tail
                tail2 = curr2;

                //iterate through the selected Linked List
                curr = curr -> next;



            }


            else if (curr->number >= curr1-> number){
           //set the new node's data to the smallest of the previous node's 
           //datas
            //from the other two Linked Lists
                curr2 -> number = curr1 -> number;

            //link the new node to the previous
                curr2 -> prev = tail2;
            //attach the predecessor node's next pointer to the current node
                tail2 -> next = curr2;
            //set the new node as the tail
                tail2 = curr2;

            //iterate through the selected Linked List
                curr1 = curr1 -> next;

            }


        } tail2 -> next = NULL;



    //Print Linked List #3
            cout<< "Linked List #3: " << endl;
            curr2 = head2;
            while (curr2){
                cout << curr2-> number;
                curr2 = curr2->next;

            }
            cout << endl;

    }

共有1个答案

景稳
2023-03-14

合并函数不应分配节点,而应仅链接现有节点的指针。下面是合并两个单个链接列表的示例。如果您需要以前的指针,可以在执行合并后完成:

NODE * MergeLists(NODE *pSrc1, NODE *pSrc2)
{
NODE *pDst = NULL;                      /* destination head ptr */
NODE **ppDst = &pDst;                   /* ptr to head or prev->next */
    while(1){
        if(pSrc1 == NULL){
            *ppDst = pSrc2;
            break;
        }
        if(pSrc2 == NULL){
            *ppDst = pSrc1;
            break;
        }
        if(pSrc2->data < pSrc1->data){  /* if src2 < src1 */
            *ppDst = pSrc2;
            pSrc2 = *(ppDst = &(pSrc2->next));
            continue;
        } else {                        /* src1 <= src2 */
            *ppDst = pSrc1;
            pSrc1 = *(ppDst = &(pSrc1->next));
            continue;
        }
    }
    return pDst;
}
 类似资料:
  • 假设列表“A”是1- 请回顾一下这个,帮我即兴创作

  • 我试图借助单链表编写合并排序,节点定义如下, 合并排序的工作方式如下: i、 将未排序的列表划分为n个子列表,每个子列表包含1个元素(1个元素的列表被视为已排序)。 二、。重复合并子列表以生成新排序的子列表,直到只剩下1个子列表。这将是排序列表。代码如下所示。 我像这样打入会电话, 结果是,所有负值似乎都消失了。这里有什么问题?

  • 合并排序通常是对链表排序的首选方式。链表缓慢的随机访问性能使得一些其他算法(如quicksort)表现不佳,而另一些算法(如heapsort)则完全不可能。我一直在努力在链表上做归并排序。它不断返回一个错误。我正在提供我试图执行的代码。请一定要帮我。它不断给出运行时错误。

  • 双向合并排序与递归合并排序有何不同? 假设在合并排序中有5个数字需要排序8,9,1,6,4,我们按如下步骤1进行划分:{8,9,1}{6,4} 步骤2:{8,9}{1}{6}{4} 步骤3:{8}{9}{1}{6}{4} 现在合并 步骤4:{8,9}{1}{4,6} 步骤5:{1,8,9}{4,6} 第六步:{1,4,6,8,9} 但在双向合并排序中,我们将数组分为两个元素(但根据维基百科,在合并

  • //我怎样才能实现这个功能?我想合并两个未排序的链表。

  • 本文向大家介绍合并排序,包括了合并排序的使用技巧和注意事项,需要的朋友参考一下 合并排序技术基于分而治之。我们将整个数据集分成较小的部分,然后按排序顺序将它们合并成较大的部分。在最坏情况下它也非常有效,因为该算法在最坏情况下的时间复杂度也较低。 合并排序技术的复杂性 时间复杂度: 所有情况下为O(n log n) 空间复杂度:  O(n) 输入输出 算法 合并(数组,左,中,右) 输入- 数据集数