[英]Circular Doubly Linked List Copy Constructor C++
我正在嘗試將復制構造函數實現到我的循環雙鏈表中,但是我無法使其工作。 文件會復制,但順序不正確。 (注意: plate
被定義為template<typename T>
,我將嘗試僅包括相關功能)
類:
plate class CircularDoubleDirectedList : public ICircularDoubleDirectedList<T> {
private:
plate class Node {
public:
T data;
Node *next;
Node *prev;
};
Node<T> *current;
int nrOfElements;
direction currentDirection;
public:
CircularDoubleDirectedList() { nrOfElements = 0; currentDirection = FORWARD; current = nullptr; }
virtual ~CircularDoubleDirectedList();
CircularDoubleDirectedList(const CircularDoubleDirectedList<T>& origObj);
CircularDoubleDirectedList& operator=(const CircularDoubleDirectedList<T>& origObj);
void addAtCurrent(const T& element);
T getElementAtCurrent() const;
void removeAtCurrent();
int size() const;
void changeDirection();
void moveCurrent();
direction getCurrentDirection() const;
};
我對復制構造函數的嘗試:
plate CircularDoubleDirectedList<T>::CircularDoubleDirectedList(const CircularDoubleDirectedList<T>& origObj) {
current = nullptr;
nrOfElements = 0;
Node<T> *tmp = origObj.current;
currentDirection = origObj.currentDirection;
while (nrOfElements < origObj.nrOfElements) {
addAtCurrent(tmp->data);
tmp = tmp->next;
}
}
加法器:
plate void CircularDoubleDirectedList<T>::addAtCurrent(const T& element) {
Node<T> *tmp = new Node<T>;
tmp->data = element;
tmp->next = nullptr;
tmp->prev = nullptr;
if (current == nullptr) {
tmp->next = tmp;
tmp->prev = tmp;
}
else if (nrOfElements == 1) {
tmp->next = current;
tmp->prev = current;
current->next = tmp;
current->prev = tmp;
}
else {
if (currentDirection == FORWARD) {
tmp->prev = current;
tmp->next = current->next;
current->next->prev = tmp;
current->next = tmp;
}
else if (currentDirection == BACKWARD) {
tmp->prev = current->prev;
tmp->next = current;
current->prev->next = tmp->prev;
current->prev = tmp;
}
}
nrOfElements += 1;
current = tmp;
}
謝謝。
正如我在評論中指出的那樣,問題似乎出在最后一個函數中。 因此,我嘗試以一種更具可讀性的方式從頭開始編寫它:
plate void CircularDoubleDirectedList<T>::addAtCurrent(const T& element) {
Node<T> *tmp = new Node<T>;
tmp->data = element;
if (current == nullptr) {
tmp->next = tmp;
tmp->prev = tmp;
} else {
Node<T> * before, after;
if (currentDirection == FORWARD) {
before = current;
after = current->next;
} else { // BACKWARD
before = current->prev;
after = current;
}
before->next = tmp;
tmp->prev = before;
after->prev = tmp;
tmp->next = after;
}
nrOfElements += 1;
current = tmp;
}
我在復制構造函數中看到另一個問題:如果currentDirection是Backward,則您從左到右讀取元素,但是以“之后”方式添加元素,從而導致順序丟失。 有兩種解決方案:您可以在掃描過程中遵守順序,或將currentDirection設置為FORWARD,然后將其設置為正確的值。
plate CircularDoubleDirectedList<T>::CircularDoubleDirectedList(const CircularDoubleDirectedList<T>& origObj) {
current = nullptr;
nrOfElements = 0;
currentDirection = FORWARD; // set the scan direction temporarily
for (Node<T> *tmp = origObj.current; nrOfElements < origObj.nrOfElements; tmp = tmp->next) {
addAtCurrent(tmp->data);
}
if (nrOfElements > 0)
current = current->next; // align with the current of the copyed list
currentDirection = origObj.currentDirection; // set the right direction
}
請告訴我是否可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.