如何合并两个双重链接列表(访问下一个链接)

How to merge two Doubly Linked Lists (Accessing next link)

本文关键字:链接 访问 下一个 列表 何合并 合并 两个      更新时间:2023-10-16

我正在尝试合并两个双重链接列表。我已经创建了一个函数,可以按正确的顺序插入新节点。这些参数是由我的教授设定的,因此我无法更改它们。我能够将第一个项目添加到List1,但无法再添加。

我在尝试继续浏览List2并在List1中添加更多项目时会遇到错误。我尝试了递归和循环时做一个。在尝试使用do-while循环

struct nodeType{
int info;
nodeType *next;
nodeType *back;
};
class OrderedDoublyLinkedList{
public:
    //Insert x in appropriate place in the list to keep it 
    sorted
    void insertNode(int x);
    void mergeLists(OrderedDoublyLinkedList &List1, 
    OrderedDoublyLinkedList &List2);
private:
    int count;
    nodeType *first;
    nodeType *last;
};
void 
OrderedDoublyLinkedList::mergeLists(OrderedDoublyLinkedList 
&List1, OrderedDoublyLinkedList &List2){
//First Technique
do{
    List1.insertNode(List2.first->info);
    List2.first->next; //Error: Expresion result unused
}
while(List2.first!=NULL)
//Second Technique
while(List2.first!=NULL)
    List1.insertNode(List2.first->info);
    mergeLists(&List1, &List2.first->next);
//If I try to use this it says cannot bind to a temporary of 
type

我需要帮助访问下一个节点将其余信息添加到List1。

看起来很简单,而循环就是您所需要的

nodeType* n = List2.first;
while (n != NULL)
{
    List1.insertNode(n->info);
    n = n->next;
}

尽管我仍然担心这是否是可以接受的解决方案。您说您需要 List2移动到List1中,这不是此代码所做的,此代码 copies List2 to List1List2不受此代码的影响。