具有一个元素的双向循环链表的反转
Reverse of a doubly circular linked list having one element
我怀疑如果我们有一个只有一个节点(即)头的双向循环链表,并且如果我写 head.next = NULL
,那么 head.prev
也会指向 NULL
?
如果 head.next = NULL
,则如下所示:
head.prev
仍然指向 head
,如您的 post 所示。
如果 head.next = NULL
,它不会改变 head.prev
(它仍然指向 HEAD
您可以运行下面的一段代码来检查它是如何工作的。 (C++ 实现)
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
void insertEnd(struct Node** start, int value)
{
if (*start == NULL)
{
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = new_node->prev = new_node;
*start = new_node;
return;
}
Node *last = (*start)->prev;
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = *start;
(*start)->prev = new_node;
new_node->prev = last;
last->next = new_node;
}
int32_t main(){
FastIO;
struct Node* start = NULL;
insertEnd(&start, 5);
cout << start -> next->data << endl;
cout << start -> prev->data << endl;
start -> next = NULL;
if(start->next) cout << "Next is Null\n";
else cout << "Next is not Null\n";
if(start->prev) cout << "Prev is Null\n";
else cout << "Prev is not Null\n";
return 0;
}
我怀疑如果我们有一个只有一个节点(即)头的双向循环链表,并且如果我写 head.next = NULL
,那么 head.prev
也会指向 NULL
?
如果 head.next = NULL
,则如下所示:
head.prev
仍然指向 head
,如您的 post 所示。
如果 head.next = NULL
,它不会改变 head.prev
(它仍然指向 HEAD
您可以运行下面的一段代码来检查它是如何工作的。 (C++ 实现)
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
void insertEnd(struct Node** start, int value)
{
if (*start == NULL)
{
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = new_node->prev = new_node;
*start = new_node;
return;
}
Node *last = (*start)->prev;
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = *start;
(*start)->prev = new_node;
new_node->prev = last;
last->next = new_node;
}
int32_t main(){
FastIO;
struct Node* start = NULL;
insertEnd(&start, 5);
cout << start -> next->data << endl;
cout << start -> prev->data << endl;
start -> next = NULL;
if(start->next) cout << "Next is Null\n";
else cout << "Next is not Null\n";
if(start->prev) cout << "Prev is Null\n";
else cout << "Prev is not Null\n";
return 0;
}