C++ 双链接列表的反向部分
我正在尝试实现我自己的列表类,但在反转列表的一部分时遇到了问题 相关代码:C++ 双链接列表的反向部分,c++,linked-list,reverse,C++,Linked List,Reverse,我正在尝试实现我自己的列表类,但在反转列表的一部分时遇到了问题 相关代码: void List<T>::reverse(ListNode * & head, ListNode * & tail) { ListNode* t; ListNode* curr = head; ListNode * funtail = tail; int stop=0; while(stop==0) { if(curr==fu
void List<T>::reverse(ListNode * & head, ListNode * & tail)
{
ListNode* t;
ListNode* curr = head;
ListNode * funtail = tail;
int stop=0;
while(stop==0)
{
if(curr==funtail)
{
stop = 1;
}
t = curr->prev;
curr->prev = curr->next;
curr->next = t;
curr = curr->prev;
}
t = tail;
tail = head;
head = t;
}
我传入指向1和4的指针,那么列表应该如下所示
4 3 2 1 5 6 7 8 9 10
问题是,我的列表返回为
1
列表的其余部分丢失(好吧,仍然可以从我的全局尾部变量访问)。有什么想法吗?我的方法错了吗?在方法参数中,您将“指针”与“引用”混合在一起 也许你是说
void List::reverse(ListNode* head, ListNode* tail)
在方法参数中,您混合了“指针”和“引用” 也许你是说
void List::reverse(ListNode* head, ListNode* tail)
如果您反转段[first,last],您希望
first->next
设置为last->next
,而不是像代码那样设置为first->prev
。如果您反转段[first,last],您希望first->next
设置为last->->next
,而不是设置为first->->->prev
,正如您的代码所做的那样。问题发生在第一个节点上,因为节点1的prev指针为NULL,您正在将其分配给节点1的下一个。您应该将1的next分配给节点5问题发生在第一个节点,因为节点1的prev指针为NULL,您正在将其分配给节点1的next。您应该在节点5旁边指定1,您的头->上一个必须在第一个for循环中指向NULL
。你最好以数字化的方式思考和实施,这会很有帮助。您需要t->next->next=t->next
在第一个for循环中,您的head->prev
必须指向NULL
。你最好以数字化的方式思考和实施,这会很有帮助。您需要t->next->next=t->next
一个更简单的解决方案
/**
* Traverses half of the list and swaps a node with another node(
here by termed as the reflection node)
* which lies at a position = listSize - (i +1) for every i.
* Reassignment of element is not needed, hence a soul saver from
* the copy constructor thing ( '=' assignment operator stuff).
*/
template <typename E> void DLinkedList<E>::reverse(){
int median = 0;
int listSize = size();
int counter = 0;
if(listSize == 1)
return;
/**
* A temporary node for swapping a node and its reflection node
*/
DNode<E>* tempNode = new DNode<E>();
for(int i = 0; i < listSize/2 ; i++){
DNode<E>* curNode = nodeAtPos(i);
// A node at 'i'th position
DNode<E>* reflectionNode = nodeAtPos(listSize - (i + 1));
// Reflection of a node considering the same distance from the median
/**
* swap the connections from previous and next nodes for current and
* reflection nodes
*/
curNode->prev->next = curNode->next->prev = reflectionNode;
reflectionNode->prev->next = reflectionNode->next->prev = curNode;
/**
* swapping of the nodes
*/
tempNode->prev = curNode->prev;
tempNode->next = curNode->next;
curNode->next = reflectionNode->next;
curNode->prev = reflectionNode->prev;
reflectionNode->prev = tempNode->prev;
reflectionNode->next = tempNode->next;
}
delete tempNode;
}
template <typename E> int DLinkedList<E>::size(){
int count = 0;
DNode<E>* iterator = head;
while(iterator->next != tail){
count++;
iterator = iterator->next;
}
return count;
}
template <typename E> DNode<E>* DLinkedList<E>::nodeAtPos(int pos){
DNode<E>* iterator = head->next;
int listSize = size();
int counter = 0;
while(counter < pos){
iterator = iterator->next;
counter++;
}
return iterator;
}
/**
*遍历列表的一半并将一个节点与另一个节点交换(
此处称为反射节点)
*每个i位于一个位置=listSize-(i+1)。
*元素的重新分配是不需要的,因此从
*复制构造函数的东西('='赋值运算符的东西)。
*/
模板无效DLinkedList::reverse(){
整数中值=0;
int listSize=size();
int计数器=0;
if(listSize==1)
返回;
/**
*用于交换节点及其反射节点的临时节点
*/
DNode*tempNode=新的DNode();
对于(int i=0;iprev->next=curNode->next->prev=reflectionNode;
reflectionNode->prev->next=reflectionNode->next->prev=curNode;
/**
*交换节点
*/
tempNode->prev=curNode->prev;
tempNode->next=curNode->next;
curNode->next=reflectionNode->next;
curNode->prev=reflectionNode->prev;
reflectionNode->prev=tempNode->prev;
reflectionNode->next=tempNode->next;
}
删除临时节点;
}
模板int DLinkedList::size(){
整数计数=0;
DNode*迭代器=头;
while(迭代器->下一步!=尾部){
计数++;
迭代器=迭代器->下一步;
}
返回计数;
}
模板DNode*DLinkedList::nodeAtPos(int pos){
DNode*迭代器=头->下一步;
int listSize=size();
int计数器=0;
while(计数器下一步;
计数器++;
}
返回迭代器;
}
一个更简单的解决方案
/**
* Traverses half of the list and swaps a node with another node(
here by termed as the reflection node)
* which lies at a position = listSize - (i +1) for every i.
* Reassignment of element is not needed, hence a soul saver from
* the copy constructor thing ( '=' assignment operator stuff).
*/
template <typename E> void DLinkedList<E>::reverse(){
int median = 0;
int listSize = size();
int counter = 0;
if(listSize == 1)
return;
/**
* A temporary node for swapping a node and its reflection node
*/
DNode<E>* tempNode = new DNode<E>();
for(int i = 0; i < listSize/2 ; i++){
DNode<E>* curNode = nodeAtPos(i);
// A node at 'i'th position
DNode<E>* reflectionNode = nodeAtPos(listSize - (i + 1));
// Reflection of a node considering the same distance from the median
/**
* swap the connections from previous and next nodes for current and
* reflection nodes
*/
curNode->prev->next = curNode->next->prev = reflectionNode;
reflectionNode->prev->next = reflectionNode->next->prev = curNode;
/**
* swapping of the nodes
*/
tempNode->prev = curNode->prev;
tempNode->next = curNode->next;
curNode->next = reflectionNode->next;
curNode->prev = reflectionNode->prev;
reflectionNode->prev = tempNode->prev;
reflectionNode->next = tempNode->next;
}
delete tempNode;
}
template <typename E> int DLinkedList<E>::size(){
int count = 0;
DNode<E>* iterator = head;
while(iterator->next != tail){
count++;
iterator = iterator->next;
}
return count;
}
template <typename E> DNode<E>* DLinkedList<E>::nodeAtPos(int pos){
DNode<E>* iterator = head->next;
int listSize = size();
int counter = 0;
while(counter < pos){
iterator = iterator->next;
counter++;
}
return iterator;
}
/**
*遍历列表的一半并将一个节点与另一个节点交换(
此处称为反射节点)
*每个i位于一个位置=listSize-(i+1)。
*元素的重新分配是不需要的,因此从
*复制构造函数的东西('='赋值运算符的东西)。
*/
模板无效DLinkedList::reverse(){
整数中值=0;
int listSize=size();
int计数器=0;
if(listSize==1)
返回;
/**
*用于交换节点及其反射节点的临时节点
*/
DNode*tempNode=新的DNode();
对于(int i=0;iprev->next=curNode->next->prev=reflectionNode;
reflectionNode->prev->next=reflectionNode->next->prev=curNode;
/**
*交换节点
*/
tempNode->prev=curNode->prev;
tempNode->next=curNode->next;
curNode->next=reflectionNode->next;
curNode->prev=reflectionNode->prev;
reflectionNode->prev=tempNode->prev;
reflectionNode->next=tempNode->next;
}
删除临时节点;
}
模板int DLinkedList::size(){
整数计数=0;
DNode*迭代器=头;
while(迭代器->下一步!=尾部){
计数++;
迭代器=迭代器->下一步;
}
返回计数;
}
模板DNode*DLinkedList::nodeAtPos(int pos){
DNode*迭代器=头->下一步;
int listSize=size();
int计数器=0;
while(计数器下一步;
计数器++;
}
返回迭代器;
}
在循环的第一次迭代中,您将curr->prev
分配给t
。如果在没有前一个节点的头部开始反转,会发生什么情况?在while
循环的第一次迭代中,您将curr->prev
分配给t
。如果在没有前一个节点的头部开始反转,会发生什么情况?这并不能回答问题。还要注意,他重新分配了参数,这就是为什么这些参数是指针的引用。不是最佳实践,而是他的代码