双向链表试图实现从源到目标的复制构造函数假设目的地的长度为零C++

问题描述

 struct Node {
    int data;
    Node* next;
    Node* prev;
    Node() : data(0),next(nullptr),prev(nullptr) { }
    explicit Node(int elt) : data(elt),prev(nullptr) { }
};


class DequeEmptyException { };



class Deque {
 public:

Deque();

~Deque();


Deque(const Deque& orig);


Deque& operator=(const Deque& rhs);

 private:
    Node* head;
    Node* tail;
    int length;

void copy(const Deque& source,Deque& destination);
};

这是我从源到目标的复制构造函数

 void Deque::copy(const Deque& source,Deque& destination) {
    // create dummy header
    destination.head = new Node;
    destination.head->next = destination.head->prev = destination.head;
    // copy nodes
    for (Node *p = source.head->next; p != source.head; p = p->next) {
    Node *t = new Node;
    t->data = p->data;

 // insert at end of list
    t->next = destination.head;
    t->prev = destination.head->prev;
    t->prev->next = t;
    t->next->prev = t;
    }
source.length = destination.length;
} 

这是我的尝试。它看起来是否正确,因为 Deque 中还有许多我无法展示的其他实现。这是最令人困惑的。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...