Implement the Set Frame (10): The realization of double-linked list

Source: Internet
Author: User
Tags prev

I. Data structure of doubly linked list 1. What is a doubly linked list?

Doubly linked list is for the single-linked list, we know that each node of the single-linked list has two fields, one is the data field, the storage element node data, one is the next Link field, the address of the node to store the successor element. Each node of a doubly-linked list has three fields, a data field, data that stores the element nodes, two links, respectively, to its predecessor and successor nodes, and a single node structure diagram is as follows:

The structure of the doubly linked list is shown in the following figure:
2. Why use a doubly linked list?

In a single-linked list, each node has only one chain that points to its successor node. If you are looking for a node's predecessor, you need to traverse the list direction from the head node of the list, and the operation is inefficient, so we need to use a doubly linked list at this point. Two. The realization of double-linked list

The doubly linked list adds a chain to the precursor node in the data structure of the node, so that a node can directly acquire its predecessor and successor nodes, and the double-linked list can traverse in the front and back two directions, which brings great convenience to the operation of the linked list. Double-linked list to determine whether it is empty, traversal and other operations and a single-linked list is similar, the following is no longer repeated discussion, you can refer to the previous article to implement the set Framework (four): single-linked list implementation, the following main discussion of double-linked list insert and delete operations. 1. Defining a doubly linked list node class

The definition of a doubly linked list node class Doublelinknode is as follows:


Code Explanation:

The data element of the member variable holds the current node, Prev points to the predecessor node, and next points to the successor node. 2. Define a doubly linked list class

The double-linked list class doublelinkedlist is defined as follows, and the head member variable represents the first node of the doubly linked list.

Code Explanation:

The doubly linked list Doublelinkedlist class implements the interface llist, so the relevant methods of the interface must be implemented, and the specific implementation is explained below. 3. Double-linked list insertion

It is very convenient to insert the double-linked list, just change the link relationship between nodes, and do not need to move the elements. The code is as follows:

Code Explanation:

Set p to a node of a doubly linked list, and the schematic of the Q node inserted before the P node is shown below:
The statement that inserts the Q node before the P node is as follows:
Doublelinknode q = new Doublelinknode (element);
Q.prev = P.prev;
Q.next = p;
P.prev.next = q;
P.prev = q; 4. Double-linked list deletion

Deleting a node in a single-linked list requires changing only certain links, without moving the node data elements, as shown in the following code:

Code Explanation:

Set p to point to a node in the doubly linked list, and delete the node that points to p as shown below:
The execution statement that deletes the node that P points to is as follows:
P.prev.next = P.next;
if (p.next! = null) {
P.next.prev = P.prev;
} 5. Clear the double-linked list

The code looks like this:

6. Overriding the ToString () method

The code looks like this:

three. Testing

The test code looks like this:

The results of the operation are as follows:
Four. Source code example

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.