【问题标题】:Implementing a doubly linked list copy constructor in C++在 C++ 中实现双向链表复制构造函数
【发布时间】:2014-02-26 02:17:30
【问题描述】:

我无法弄清楚如何为双向链表实现复制构造函数。我相信到目前为止我的代码是不正确的,因为我忘记了头节点,但我不知道如何纠正这个问题。任何帮助将不胜感激!

双链表.h

#include <cstdlib>
#include <iostream>
using namespace std;
class DoublyLinkedList; // class declaration

// list node
class DListNode {
private: 
    int obj;
    DListNode *prev, *next;
    friend class DoublyLinkedList;
public:
    DListNode(int e = 0, DListNode *p = NULL, DListNode *n = NULL)
        : obj(e), prev(p), next(n) {}

    int getElem() const { return obj; }
    DListNode * getNext() const { return next; }
    DListNode * getPrev() const { return prev; }
};

// doubly linked list
class DoublyLinkedList {
protected: 
    DListNode header, trailer;
public:
    DoublyLinkedList() : header(0), trailer(0) // constructor
        { header.next = &trailer; trailer.prev = &header; }
    DoublyLinkedList(const DoublyLinkedList& dll); // copy constructor
    ~DoublyLinkedList(); // destructor
    DoublyLinkedList& operator=(const DoublyLinkedList& dll); // assignment operator


    DListNode *getFirst() const { return header.next; } // return the pointer to the first node
    const DListNode *getAfterLast() const { return &trailer; }  // return the pointer to the trailer
    bool isEmpty() const { return header.next == &trailer; } // return if the list is empty
    int first() const; // return the first object
    int last() const; // return the last object

    void insertFirst(int newobj); // insert to the first of the list
    int removeFirst(); // remove the first node
    void insertLast(int newobj); // insert to the last of the list
    int removeLast(); // remove the last node

};

// output operator
ostream& operator<<(ostream& out, const DoublyLinkedList& dll);

DoublyLinkedList.cpp - 复制构造函数

// copy constructor
DoublyLinkedList::DoublyLinkedList(const DoublyLinkedList& dll)
{
  // Initialize the list
  header = 0;
  trailer = 0;
  header.next = &trailer; 
  trailer.prev = &header;

  // Copy from dll
    DListNode* head = new DListNode;
    head->prev = dll.header.prev;
    head->obj = dll.header.obj;
    head->next = dll.header.next;

    DListNode* tail = new DListNode;
    tail->prev = dll.trailer.prev;
    tail->obj = dll.trailer.obj;
    tail->next = dll.trailer.next;

    DListNode* curr = new DListNode;
    curr->prev = head;

    while(curr != tail) {

        DListNode* n = new DListNode;
        curr->next = n;
        n = curr->prev->next;
        curr = curr->next;
    }

    curr = tail;
}

【问题讨论】:

  • 另外,构造函数总是至少执行 3 次 new DListNode。但是如果dll为空,实际上应该使用动态存储零次。

标签: c++ copy-constructor doubly-linked-list


【解决方案1】:

与其编写特定代码来使用内部结构复制 dll 中的列表,不如像往常一样(使用 dll.getFirst() 和 dll.getAfterLast())循环遍历 dll 中的列表并调用 insertLast每个值。

【讨论】:

  • 那不是给我一个浅拷贝,而不是一个深拷贝吗?
  • 您会将每个值从一个列表复制到另一个列表,因此这将是一个深层复制。
  • @user2555471 - Wouldn't that just give me a shallow copy...? 你为什么这么说?暂时忘记复制构造函数吧,如果我编写了一个以空列表开始的独立函数,然后在循环中通过调用 insertLast() 将每个项目从填充列表复制到空列表,该怎么办?你是说你怀疑它会正确创建我的新列表吗?然后,如果您有这些疑问,那么您最好修复您的 insertLast() 函数,因为您不相信它能够正确完成工作。
猜你喜欢
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
相关资源
最近更新 更多