Head pointers and head nodes in linked lists

Source: Internet
Author: User
Tags list of attributes

The disadvantage of using sequential (array) storage for linear tables is that a large amount of mobile data is required when inserting and deleting, which is very time consuming, so you can use chained storage, which has a pointer field (single linked list) to record the location (address) of the next node. This allows you to simply modify the pointer field when inserting and deleting nodes, thus reducing the amount of time spent moving data. Consider the definition of a linked list:

struct Node {int data;  struct node *next;}; 

There are two elements, data is a field for storing data, and next is a pointer field for storing the location (address) of the next node. So what is a head pointer? We refer to the pointer to the first node as the head pointer, so each time you access the linked list, you can walk through each element of the list in turn, for example:

struct node first;struct node *head = &first;

This head pointer is the first pointer.
The meaning of this head pointer is that when accessing a linked list, always know where the linked list is stored (from where to start access), because the list of attributes (next pointer), know the head pointer, then the entire list of elements can be accessed, that is, the head pointer must exist. Examples are as follows:

#include <stdio.h>struct node {int data;struct node *next;}; int main (void) {struct node *head, first, Second;head = &first;first.data = 1;first.next = &second;second.data = 2; Second.next = Null;while (head) {printf ("%d\n", head->data); head = Head->next;} return 0;}

It is important to note that the while part ( traversing the entire list through the head pointer).

So what is the head node again? Many times, a node is attached to the head of the list, and the data field of the node can be stored without any information, and this node is called the head node.
The pointer field of a head node points to the first node, for example:

struct node head, First;head.next = &first;

So who's the head pointer here, not a pointer to the first node, but a pointer to a head node, for example:

struct node *root = &head;

That is, the root pointer is the head pointer. Examples are as follows:

#include <stdio.h>struct node {int data;struct node *next;}; int main (void) {struct node *root, head, first, second;root = &head;root->data = 0;root->next = &first;first. data = 1;first.next = &second;second.data = 2;second.next = Null;while (root) {printf ("%d\n", root->data); root = Ro Ot->next;} return 0;}

Head pointers and head nodes in linked lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.