0
Follow
0
View

How to solve the problem of # header #?

silence1227 注册会员
2023-02-26 14:15

NODE* node = (NODE*)malloc(sizeof(node));
newnode->next = NULL;
if (head == NULL)
{
        head = newnode;
}

Is this how you create a new node as a header?

The pointer under newnode is null, which means that an empty list is created and the data node is inserted later.

ofhzxzg 注册会员
2023-02-26 14:15
daituzhe 注册会员
2023-02-26 14:15
< div class = "md_content_show e397 data - v - 3967" = "" >

is not completely correct. In this code, although a new node is also created and its pointer field is set to NULL, this is not how the header node is created. It is simply used to create the first node in an empty linked list and use it as the head node of the list.

To create a header, insert a new node before the header pointer points to it. This new node serves as the header, and its data field can be any value, but the pointer field must point to where the header pointer points to in order to ensure the connectivity of the entire list.

Here is sample code to create a header node and insert it into the header of a linked list:

NODE* head = NULL;  // 头指针初始化为NULL

// 创建头结点
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->next = head;

// 将头指针指向头结点
head = newnode;


creates a newnode, newnode, and sets its pointer field to where the head of the header pointer points. The header is then created by pointing the head of the header pointer to the newnode newnode.

dingdh13 注册会员
2023-02-26 14:15

Line 15 defines a header pointer, not a header node. A header pointer is a pointer to the head of a list, and a header node is an actual node of the head of a list, usually used to avoid special treatment of a header pointer when the list is empty. In this code snippet, the header pointer head has a value of NULL, indicating that the list is empty and therefore no header is needed. If you need to insert a new node into the header of the list, you need to create a new node as the header and point the header pointer to it.

xye20070408 注册会员
2023-02-26 14:15


NODE* node = (NODE*)malloc(sizeof(node));
newnode->next = NULL;
if (head == NULL)
{
        head = newnode;
}

Is this how you create a new node as a header?

The pointer under newnode is null, which means that an empty list is created and the data node is inserted later.

czyroy 注册会员
2023-02-26 14:15
  • Line 15 defines a header pointer. A header pointer is a pointer to the first node in the list, not a node in the list.
  • Header inserts an empty node into the header of the list for easy manipulation. The data domain of the header node generally does not store the actual data and is used only as a sentinel node.
  • In this code, no headers are defined, just a header pointer head that points to the head of the list.

About the Author

Question Info

Publish Time
2023-02-26 14:15
Update Time
2023-02-26 14:15