好的,下面是一个名为dLinikedList的类(是的,我知道我应该使用STL容器):
class dLinkedList
{
public:
//constructors will go here
explicit dLinkedList(const int value)
{
createFirstNode(value);
nodeCount = new size_t;
updateNodeCount();
}
dLinkedList(const dLinkedList &rhs)
{
Node *temp = rhs.head;
createFirstNode(temp->data);
nodeCount = new size_t;
updateNodeCount();
temp = temp->next;
while(temp)
{
push_back(temp->data);
temp = temp->next;
}
updateNodeCount();
}
explicit dLinkedList(size_t numberOfNode, int initializationValue)
{
createFirstNode(initializationValue);
for(size_t i = 1; i < numberOfNode; ++i)
push_back(initializationValue);
nodeCount = new size_t;
updateNodeCount();
}
//class destructor will go here
~dLinkedList()
{
clear();
delete nodeCount;
nodeCount = nullptr;
}
//member functions will go here
void push_back(int); // will attach a new node at the end of the list
void push_front(int); // will insert a new node at the beginning of the list
bool insertNode(int, int, bool, bool); // will insert a new node after the existing node (true = first occurrence from the head with value int OTHERWISE if false, then from the tail.)
bool deleteNode(int, bool); // will delete the existing node (true = first occurrence from the head with value int OTHERWISE if false, then from the tail.)
void pop_back(); // will delete the last node in the list and return the value of the internal data
void pop_front(); // will delete the first node in the list
size_t size(); // will return the number of nodes/elements - experimental feature
void printList(bool); // will print the values of the data - (true for ordered list, false for reverse ordered list)
void swap(dLinkedList &rhs); // will swap this linked-list with rhs
//operator overloading go here
dLinkedList& operator = (const dLinkedList &rhs);
dLinkedList& operator + (const dLinkedList &rhs);
dLinkedList& operator += (const dLinkedList &rhs);
dLinkedList& operator >> (const size_t numberOfNodes);
dLinkedList& operator << (const dLinkedList &rhs);
private:
//defining the double linked-list structure
struct Node
{
int data; //this is a generic place holder - will be replaced later with some actual data-structures
Node *next;
Node *previous;
explicit Node(int x) : data(x), next(nullptr), previous(nullptr) {}
};
//member functions go here
void createFirstNode(int val); //will create the first node when the list is empty
void clear(); // will be called when class destructor is called
void updateNodeCount(); // keeps the nodeCount variable up-to-date
bool empty(); // returns true if the list is empty
//some experimental utility functions for internal use
void ectomizeAndClip(Node*);
Node *detectData(int, bool);
void insertBefore(Node*, int);
void insertAfter(Node*, int);
//member variables go here
Node *head {nullptr};
Node *tail {nullptr};
size_t *nodeCount {nullptr}; //experimental feature
};有这一成员职能,目前实现为:
void dLinkedList::swap(dLinkedList &rhs)
{
dLinkedList temp {rhs};
rhs.clear();
Node *traverser = head;
while(traverser != nullptr)
{
rhs.push_back(traverser->data);
traverser = traverser->next;
}
clear();
traverser = temp.head;
while(traverser != nullptr)
{
push_back(traverser->data);
traverser = traverser->next;
}
}显然,随着列表长度的增长,这个操作需要大量的时间。以下是我的想法(如果有可能的话-以尽量减少在较大列表情况下执行的时间):
void dLinkedList::swap(dLinkedList *rhs)
{
// what I am planning to achieve is simply swap the address mutually.
dLinkedList *temp {rhs};
rhs = this;
this = temp;
}但是,此代码不起作用,因此出现了如下错误:
error: lvalue required as left operand of assignment|我想澄清我对这是否可以实现的疑虑/误解?如果是的话,密码是什么。
发布于 2022-08-21 07:50:44
您的类由三个指针组成,只需交换这些指针。这是非常有效的,不取决于列表的大小。
像这样
void dLinkedList::swap(dLinkedList &rhs)
{
std::swap(head, rhs.head);
std::swap(tail, rhs.tail);
std::swap(nodeCount, rhs.nodeCount);
}有时,如果节点包含指向包含对象的指针,则此方法将无法工作,但这里的情况并非如此。
发布于 2022-08-21 07:47:01
this是一个常量/不变指针dLinkedList* const this,不能被this = temp修改。
使用标准交换
void dLinkedList::swap(dLinkedList *rhs)
{
std::swap(*this, *rhs);
}函数参数相当不正确,应该是
void dLinkedList::swap(dLinkedList &rhs)
{
std::swap(*this, rhs);
}发布于 2022-08-21 07:53:13
您可以将要交换的dLinkedList作为交换函数中的引用,只需使用std::swap交换this和rhs的head。
void dLinkedList::swap(dLinkedList& rhs) {
std::swap(this->head, rhs.head);
}https://stackoverflow.com/questions/73432451
复制相似问题