Linked List Summary

Source: Internet
Author: User

 

I. Single-chain table

A one-way linked list (single-chain table) is a type of linked list. Its link direction is unidirectional. Access to the linked list should start from the header through sequential reading; A linked list is a list constructed by pointers. It is also called a node list, because the linked list is assembled by nodes. Each node has a pointer member variable that refers to the next node in the list;

A list consists of nodes. The head Pointer Points to the first node that becomes the header and ends with the last pointer pointing to null;

1. Create a linked list (tail insertion ):

The basic idea of the tail plug method:
1. Generate a node and store the read data in the data domain of the new node.
2. Insert the new node to the End Node of the current linked list.
3. Repeat the above process until the end mark is entered.

 

The order in which data is read by the end plug method is the same as that in the linear table.

Void creat_list (node * & head) {// you can specify a pointer to the header, which must be saved. Note that node * & is a node * type reference node * Pre, * newnode; head = pre = new node; // generate the header node newnode = new node; // create a new node CIN> newnode-> num; // enter the value of the new node while (newnode-> num) {// condition for determining the end of input: Pre-> next = newnode; // connect the previous node to the new node: Pre = newnode; // because you need to apply for a new space immediately, in this case, the new node is about to become the "Last node" and saved to prenewnode = new node; // apply for a new space CIN> newnode-> num; // input value} Pre-> next = NULL; // after the end, set the rear pointer of the last node in the single-linked list to null, marking the end}

2. Create a linked list (Head insertion)

The basic idea of the header insertion method:
1. Generate a node and store the read data in the data domain of the new node.
2. After the new node is used as the first table node
3. Repeat the appeal process until the end mark is entered.

 

The order in which the header is inserted into the data is the opposite of that in the linear table.

Void creat_head (node * & head) {node * newnode; head = new node; head-> next = NULL; newnode = new node; CIN> newnode-> num; // input data while (newnode-> num) {newnode-> next = head-> next; head-> next = newnode; newnode = new node; cin >>> newnode-> num ;}}

 

3. Single-chain table search: search by value

Node * search_value (node * head, int X) {// X indicates the value of node * P = head-> next; while (P! = NULL & P-> num! = X) P = p-> next; If (p-> num = x) Return (p); return NULL ;}

4. Single-chain table search: search by location

Node * search_local (node * head, int I) {// I is the node * P = head; int COUNT = 0; while (p-> next! = NULL & count <I) {P = p-> next; ++ count;} If (COUNT = I) Return (p); return NULL ;}

5. Single-chain table insertion: Forward (backward)

Procedure:
1. generate a new node newnode and assign the value X to the data domain of the new node newnode.
2. Start from the header node and find the P precursor node Q
3. Modify the pointer field of the relevant node, point the newnode pointer field to the P node, and the Q node to the newnode

Node * inset_front (node * head, int I, int X) {// X indicates the value to be inserted. I is the position node * P, * newnode; newnode = new node; // apply for a new space newnode-> num = x; // assign a value of P = search_local (Head, I-1); // just change the I-1 to I, it becomes newnode-> next = p-> next; P-> next = newnode; return head ;}

6. delete a single-chain table (delete the value X in the Linked List)

Procedure:
1. Assume that there are two nodes pre and P. PRE is the precursor node of P, and P is the node to be deleted.
2. Find the PRE, start from the head node of the linked list, and perform a Backward Search. When pre-Next = P, find
3. modify pre-Next = p-Next, point the P precursor node to the P successor node, and delete the space.

Node * delete_link (node * head, int X) {node * Pre, * P; // pre-stored P's precursor node P = head; while (P! = NULL & P-> num! = X) {pre = P; P = p-> next;} If (P! = NULL) {pre-> next = p-> next; Delete (p); return head;} else {cout <"the value to be deleted does not exist \ n "; return NULL ;}}

 

Ii. Loop list

Circular linked list: the pointer field of the last node in the table is no longer empty, but directed to the first node. The whole linked list forms a ring.

Feature: you can find all other nodes from any node in the table.

Purpose: in many practical applications, operations on the linked list are performed at the header and end of the table. In this case, you can change the identification method of the linked list without a head pointer.
It is identified by the tail pointer rear pointing to the end node to improve efficiency.

Comparison with a single-chain table:
In a single-chain table, the time of the first element is O (1), and the last element is O (n );
When the circular linked list is applied, the last element is found to be O (1). Because the last element points to the first element, the first element is also found to be O (1)

Calculation: the operation of the cyclic linked list is basically the same as that of the single-chain table. The difference is that the conditions for completing the traversal table are different. The single-chain table determines whether the current pointer field is null, while the loop
The linked list determines whether the pointer field of the current node is the header pointer.

 

// The following uses the search operation as an example.

// The following uses the search operation as an example: node * search_value (node * head, int X) {// X indicates the value of node * P = head-> next; while (p-> next! = Head & P-> num! = X) // P-> next! = Headp = p-> next; If (p-> num = x) Return (p); return NULL ;}

 

 

3. Two-way linked list

Two-way linked list: a linear linked list that can be traversed along the forward and backward directions.
Each node has two pointer fields: one pointing to its successor node and the other pointing to its precursor node.

 

The Node Type of the two-way linked list is defined:

Struct dnode {int data; // The element dnode * Next, * Prior in the data storage table; // next points to the next node pointer, and prior points to the previous node pointer };

 

Advantages of two-way linked list: two-way linked list has both forward and backward pointer fields, which makes the two-way linked list forward and backward insertion
And delete operations are very convenient. You only need to modify the pointer fields of several nodes without having to perform a lot of data exchange and traversal operations.

// Node type struct dnode {int data; // The element dnode * Next, * Prior in the data storage table; // next points to the next node pointer, and prior points to the previous node pointer }; // forward insertion of a two-way linked list (insert a new node s before a node p) dnode * insert_before (dnode * head, dnode * P, int X) {dnode * s; // s is the node to be inserted s = new dnode; // create a new node S-> DATA = x; // assign S-> next = P to the data field of S nodes; // Step 1: point the successor pointer of node s to P; s-> prior = p-> prior; // Step 2: point the forward pointer of node s to the forward node P of node p-> prior-> next = s; // Step 3: modify the successor pointer of the forward node of P to point to the new node s; P-> prior = s; // Step 4: Change the forward pointer of the forward node P to point to the new node S. re Turn head;} // two-way linked list insertion (insert a new node s after a node P): dnode * insert_back (dnode * head, dnode * P, int X) {dnode * s; // s is the node to be inserted s = new dnode; // create a new node S-> DATA = x; // assign S-> next = p-> next to the data field of the S node; // Step 1: point the next pointer of node s to P; s-> prior = P; // Step 2: point the forward pointer of node s to the forward node P of node p-> next-> prior = s; // Step 3: modify the successor pointer of the forward node of P to point to the new node s; P-> next = s; // Step 4: Change the forward pointer of node P to point to the new node S. return head;} // delete a two-way linked list node: dnode * delete_dnode (dnode * head, dnode * P) {If (P! = NULL) {P-> prior-> next = p-> next; // Step 1: change the successor pointer of the forward node of P to P's successor p-> next-> prior = p-> prior; // Step 2: change the forward pointer of the successor node of P to the forward delete (P) of P; // Step 3: Release the bucket return head of P; // return the head pointer of the two-way linked list of the leading node} elsereturn NULL ;}

 


-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double



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.