我正在尝试将复制构造函数实现到我的循环双向链接列表中,但我无法使其工作。文件会复制,但顺序不正确。 (注意: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;
}
谢谢。
答案 0 :(得分:1)
正如我在评论中指出的那样,问题似乎出现在最后一个函数中。所以我尝试以更易读的方式从头开始编写它:
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,你正在从左到右阅读元素,但是在&#34;之后添加元素&#34;时尚,随之失去秩序。 有两种解决方案:您可以在扫描期间遵守顺序,或将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
}
请告诉我是否能解决您的问题。