Unveil the mystery of a content-independent linked list

Source: Internet
Author: User

I first learned the concept of linked list in the data structure class. At that time, we used the "Data Structure" tutorial edited by Yan Yumin, Tsinghua, at that time, we thought that the linked list node is the pointer of Data + pointing to other linked list nodes, for example:

[Html] typedef struct tagNode {
TDataType data;
Struct tagNode * pNext;
} TNode;

Typedef struct tagNode {
TDataType data;
Struct tagNode * pNext;
} TNode; now I want to come. At that time, I just stayed on the book surface. Later I opened the source code (linux, rtems, and yaffs) and found that the linked list node can still have data without it, as shown in the following figure:

[Html] typedef struct tagNode {
Struct tagNode * pNext;
Struct tagNode * pPre;
} TNode;

Typedef struct tagNode {
Struct tagNode * pNext;
Struct tagNode * pPre;
} TNode; the aforementioned linked list node does not depend on any data. In this case, you do not need to write a linked list for each data that needs to be stored in the linked list, all data can be shared with the same linked list. Let's take a look at how this data-independent linked list is shared. Suppose we have one copy of data:

[Html] typedef struct tagData {
.....
} TData;

Typedef struct tagData {
.....
} TData; how can we organize the data using a linked list?

[Html] typedef struct tagLinkData {
TNode node;
TData data;
} TLinkData;
Or
Typedef struct tagLinkData {
TData data;
TNode node;
} TLinkData;

Typedef struct tagLinkData {
TNode node;
TData data;
} TLinkData;
Or
Typedef struct tagLinkData {
TData data;
TNode node;
} TLinkData;
Through the above structure, the linked list node can be associated with the data to be concerned. The linked list node can be placed at the beginning of the data or at the end of the data, as shown above, I usually put it at the beginning of the data when using it. This is more convenient. In the following 1st cases, I will explain how to use this data-independent linked list. We know that there are append, delete, and obtain the first node in the chain table 1. I will introduce the usage of these three aspects:

1. Assume that the Linked List Operation statement is as follows:

[Html] void append (TNode * pList, const TNode * pNode );
Void delete (TNode * pList, const TNode * pNode );
Const TNode * getFirst (const TNode * pList );
TNode list;
2. append 1 TLinkData to the list linked list.

Void append (TNode * pList, const TNode * pNode );
Void delete (TNode * pList, const TNode * pNode );
Const TNode * getFirst (const TNode * pList );
TNode list;
2. append 1 TLinkData to the list linked list.
TLinkData data;
Append (& list, (const TNode *) & data );

If node is placed at the end of data, the second parameter here will be changed, (const TNode *) & data. node;

3. delete node pNode from list

[Html]? Delete (& list, pNode );

Delete (& list, pNode); 4. Obtain the 1st nodes of the list and use it
Const TNode * pNode = getFirst (& list );
Const TLinkData * pLinkData = (const TLinkData *) pNode;
Const TData & data = pLinkData-> data;

If the node is placed at the end of data, the second line cannot be converted directly like this. The pNode address must be subtracted from one sizeof (TData), which is the first address.

Related Article

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.