Self-Realization Set Framework (VII): the implementation of the lead node single-link list

Source: Internet
Author: User

I. What is a single linked list of lead nodes?

The single-linked list of lead nodes is similar to the normal single-linked list, but adds a special node before the first node of the ordinary single-linked list, which is called the head node. In the single-linked list of the lead nodes, head points to the header node. Although the head node is not used to store data, it consumes a storage unit that is equivalent to sacrificing a node's storage space to simplify the operation of a single-linked list. Because the added head node makes all linked lists (including empty tables) of the head node non-empty, and allows the insertion of the single-linked list, the delete operation does not need to distinguish whether the single-linked list is empty or at the first position, so that it can be consistent with the insertion and deletion in other locations, as shown in the following illustration:
(a) empty list


(b) head inserted, without changing head, without distinguishing between insertion position
(c) Header deletion, does not change head, does not distinguish between deleted location
two. Implementation of single-link list with lead node 1. A single-linked list class that defines the lead node

The single-linked list class Headsinglylinkedlist declaration as follows, the implementation way and ordinary single-linked list singlylinkedlist difference is not small, also use single-linked list node class nodes.

Code Explanation:

1. Single-linked list Headsinglylinkedlist class implementation interface Llist, so must implement the interface of the relevant methods, and ordinary single-linked list method similar to the method is no longer listed below, please see the previous single-linked list implementation of the article, the following will be a different way to do some explanation.
2. In the constructor, the head node and the tail node are specified, the data of the head node is empty, and the head node and the tail node point to the head node when initializing. 3. Determine if the lead node single-linked list is empty

Code Explanation:

Because of the addition of the head node, the criteria for determining if a single linked list is empty is based on whether the next node of the head node is empty. 4. Find the length of the single linked list of the lead node

Code Explanation:

Due to the addition of the member variable N, which represents the length of the single-linked table, it is possible to get the single-linked table length directly, and the time complexity of the operation is O (1). 5. Inserting a single linked list with the lead node

Code Explanation:

1. The single-linked list with the lead node does not need to differentiate the insertion position when inserting.
2. The insert operation maintains the length of the single-linked list n, after inserting an element n plus 1. 6. The deletion of the lead node single linked list

Code Explanation

The single-linked list of lead nodes does not need to distinguish between deleted locations when deleting operations.

The delete operation maintains the length of the single-linked list, n minus 1 after deleting an element.


7. Clear the single-linked list of lead nodes

8. Overriding the ToString () method

three. Testing

The test code looks like this:

The result of the operation is shown in the following figure:
Four. Operation efficiency analysis of single-link table with lead node

Because the member variable n is added to maintain the length of the single-linked list, the time complexity of the length () operation is O (1). Due to the addition of the tail node rear points to the last node of the single-linked list, the time complexity of the insertion operation at the tail node is O (1). The time complexity of other operations is the same as for a regular single-linked list, see Analysis of the implementation of the previous article single-linked list. Five. 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.