【发布时间】:2020-06-28 05:56:13
【问题描述】:
【问题讨论】:
标签: data-structures linked-list circular-dependency
【问题讨论】:
标签: data-structures linked-list circular-dependency
如果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;
}
【讨论】: