Linked List
This page provides a brief introduction to linked lists.
Introduction
A linked list is a data structure used to store data, connecting elements via pointers like a chain. Its key features are convenient insertion and deletion of data, but relatively poor performance in searching and reading data.
Comparison with Arrays
Both linked lists and arrays can be used to store data. Unlike linked lists, arrays store all elements sequentially in order. Their different storage structures give them distinct advantages:
Due to its chain-like structure, a linked list can conveniently delete and insert data with an operation count of \(O(1)\). However, because of this, searching and reading data is less efficient than in arrays, with an operation count of \(O(n)\) for random access.
Arrays can conveniently search and read data, with an operation count of \(O(1)\) for random access. However, deletion and insertion operations take \(O(n)\) steps.
Constructing a Linked List
Tip
When constructing a linked list, the pointer-related parts can be abstract. Relying solely on textual descriptions and code may make it difficult to understand. It is recommended to use diagrams alongside the explanation.
Singly Linked List
A singly linked list contains a data field and a pointer field. The data field stores data, while the pointer field connects the current node to the next node.
Implementation
1 2 3 4 | |
1 2 3 4 | |
Doubly Linked List
A doubly linked list also has data and pointer fields. The difference is that the pointer field has left/right (or previous/next) distinctions, used to connect the previous node, the current node, and the next node.
Implementation
1 2 3 4 5 | |
1 2 3 4 5 | |
Inserting (Writing) Data into a Linked List
Singly Linked List
The general process is as follows:
- Initialize the data
nodeto be inserted; - Set
node'snextpointer to point to the node afterp; - Set
p'snextpointer to point tonode.
Refer to the following diagram for the specific process:
Code implementation:
Implementation
1 2 3 4 5 6 | |
1 2 3 4 5 | |
Singly Circular Linked List
Connecting the head and tail of a linked list turns it into a circular linked list. Since the list is connected end-to-end, when inserting data, it is necessary to check whether the original list is empty: if empty, it points to itself; if not empty, insert data normally.
The general process is as follows:
- Initialize the data
nodeto be inserted; - Determine if the given linked list
pis empty; - If empty, set both
node'snextpointer andpto point to itself; - Otherwise, set
node'snextpointer to point to the node afterp; - Set
p'snextpointer to point tonode.
Refer to the following diagram for the specific process:
Code implementation:
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 | |
Doubly Circular Linked List
When inserting data into a doubly circular linked list, besides checking if the given list is empty, both the left and right pointers need to be modified simultaneously.
The general process is as follows:
- Initialize the data
nodeto be inserted; - Determine if the given linked list
pis empty; - If empty, set
node'sleftandrightpointers, andp, to point to itself; - Otherwise, set
node'sleftpointer to point top; - Set
node'srightpointer to point top's right node; - Set the
leftpointer ofp's right node to point tonode; - Set
p'srightpointer to point tonode.
Code implementation:
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
Deleting Data from a Linked List
Singly (Circular) Linked List
Let the node to be deleted be p. To delete it from the list, overwrite p with the value of p->next, and simultaneously update p's next-next node.
The general process is as follows:
- Assign the value of
p's next node top, effectively erasingp->value; - Create a temporary node
tto store the address ofp->next; - Set
p'snextpointer to point top's next-next node, effectively erasingp->next; - Delete
t. Although the address of the original nodepis still in use, what is deleted is the address of the originalp->next. However,p's data is overwritten byp->next, makingpexist in name only.
Refer to the following diagram for the specific process:
Code implementation:
Implementation
1 2 3 4 5 6 | |
1 2 3 | |
Doubly Circular Linked List
The general process is as follows:
- Set the
rightpointer ofp's left node to point top's right node; - Set the
leftpointer ofp's right node to point top's left node; - Create a temporary node
tto store the address ofp; - Assign the address of
p's right node topto avoidpbecoming a dangling pointer; - Delete
t.
Code implementation:
Implementation
1 2 3 4 5 6 7 | |
1 2 3 4 | |
Techniques
XOR Linked List
An XOR Linked List is essentially a doubly linked list, but it utilizes the bitwise XOR value to achieve the functionality of a doubly linked list using only the memory size of a single pointer.
In the structure Node, we define lr = left ^ right, which is the bitwise XOR value of the addresses of the previous and next elements. During forward traversal, XOR-ing the address of the previous element with the current node's lr yields the address of the next element. During backward traversal, XOR-ing the address of the next element with the current node's lr yields the address of the previous element. This way, the same functionality as a doubly linked list can be achieved with half the memory.
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用