Home Backend Development C#.Net Tutorial .NET Framework-Doubly Linked List (LinkedList) Code Analysis (Figure)

.NET Framework-Doubly Linked List (LinkedList) Code Analysis (Figure)

Mar 18, 2017 am 11:37 AM



.NETLinkList in the framework implements a two-way linked list. Let’s summarize its implementation source code.

Let’s first look at the map of the public properties and methods provided by LinkedList:


.NET Framework-Doubly Linked List (LinkedList) Code Analysis (Figure)
## 1

Interface implemented by LinkedList:

public class LinkedList<T> : ICollection<T>, ICollection, IReadOnlyCollection<T>, ISerializable, IDeserializationCallback
Copy after login

2 Global

variables of LinkedList include,

head is the head node in the encapsulated class;

        // This LinkedList is a doubly-Linked circular list.
        internal LinkedListNode<T> head;        
        internal int count;        
        internal int version;        
        private object _syncRoot;        
        //A temporary variable which we need during deserialization.  
        private SerializationInfo _siInfo; 

        // names for serialization
        private const string VersionName = "Version";        
        private const string CountName = "Count";  
        private const string ValuesName = "Data";
Copy after login
The data structure of each node encapsulated is:

public sealed class LinkedListNode<T>
{   public LinkedListNode(T value);   
//获取LinkedListNode所属的LinkedList
   public LinkedList<T> List { get; }   
   public LinkedListNode<T> Next { get; }   
   public LinkedListNode<T> Previous { get; }   
   //获取节点中包含的值。
   public T Value { get; set; }
}
Copy after login

3

Constructor

:

When constructing a collection of IEnumerable type, the AddLast(T) method is used. It also has an

overload

. The working details are as follows:

The above 2 methods , the semantics is to insert a node,

Insert new nodes into empty lists, InternalInsertNodeToEmptyList

Insert new nodes into non-empty lists, InternalInsertNodeBefore, and give the node before which newNode is inserted, and also judge The newly inserted node is not a valid new node.

 internal void ValidateNewNode(LinkedListNode<T> node)
        {            if (node == null)
            {                throw new ArgumentNullException(nameof(node));
            }            if (node.list != null)
            {                throw new InvalidOperationException(SR.LinkedListNodeIsAttached);
            }
        }
Copy after login

At the same time, it also provides a way to determine whether a node is a valid node:

        internal void ValidateNode(LinkedListNode<T> node)
        {            if (node == null)
            {                throw new ArgumentNullException(nameof(node));
            }            if (node.list != this)
            {                throw new InvalidOperationException(SR.ExternalLinkedListNode);
            }
        }
Copy after login

This is an important internal method of a doubly linked list,

InternalInsertNodeToEmptyList implementation details:

        private void InternalInsertNodeToEmptyList(LinkedListNode<T> newNode)
        {
            Debug.Assert(head == null && count == 0, "LinkedList must be empty when this method is called!");
            newNode.next = newNode;
            newNode.prev = newNode;
            head = newNode;
            version++;
            count++;
        }
Copy after login

InternalInsertNodeBefore implementation details:

        private void InternalInsertNodeBefore(LinkedListNode<T> node, LinkedListNode<T> newNode)
        {
            newNode.next = node;
            newNode.prev = node.prev;
            node.prev.next = newNode;
            node.prev = newNode;
            version++;
            count++;
        }
Copy after login

4 A linked list is naturally inseparable from the public method of inserting a node,

        public LinkedListNode<T> AddAfter(LinkedListNode<T> node, T value)
        {
            ValidateNode(node);
            LinkedListNode<T> result = new LinkedListNode<T>(node.list, value);
            InternalInsertNodeBefore(node.next, result);            return result;
        }        public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode)
        {
            ValidateNode(node);
            ValidateNewNode(newNode);
            InternalInsertNodeBefore(node.next, newNode);
            newNode.list = this;
        }        public LinkedListNode<T> AddBefore(LinkedListNode<T> node, T value)
        {
            ValidateNode(node);
            LinkedListNode<T> result = new LinkedListNode<T>(node.list, value);
            InternalInsertNodeBefore(node, result);            if (node == head)
            {
                head = result;
            }            return result;
        }        public void AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode)
        {
            ValidateNode(node);
            ValidateNewNode(newNode);
            InternalInsertNodeBefore(node, newNode);
            newNode.list = this;            if (node == head)
            {
                head = newNode;
            }
        }        public LinkedListNode<T> AddFirst(T value)
        {
            LinkedListNode<T> result = new LinkedListNode<T>(this, value);            
            if (head == null)
            {
                InternalInsertNodeToEmptyList(result);
            }            else
            {
                InternalInsertNodeBefore(head, result);
                head = result;
            }            return result;
        }        public void AddFirst(LinkedListNode<T> node)
        {
            ValidateNewNode(node);            if (head == null)
            {
                InternalInsertNodeToEmptyList(node);
            }            else
            {
                InternalInsertNodeBefore(head, node);
                head = node;
            }
            node.list = this;
        }        public LinkedListNode<T> AddLast(T value)
        {
            LinkedListNode<T> result = new LinkedListNode<T>(this, value);            
            if (head == null)
            {
                InternalInsertNodeToEmptyList(result);
            }            else
            {
                InternalInsertNodeBefore(head, result);
            }            return result;
        }        public void AddLast(LinkedListNode<T> node)
        {
            ValidateNewNode(node);            if (head == null)
            {
                InternalInsertNodeToEmptyList(node);
            }            else
            {
                InternalInsertNodeBefore(head, node);
            }
            node.list = this;
        }
Copy after login

5 Let’s look at it again, clearing all nodes in the linked list, here It is to set all nodes to no longer point to the memory heap, and then wait for GC recycling,

        public void Clear()
        {
            LinkedListNode<T> current = head;            
            while (current != null)
            {
                LinkedListNode<T> temp = current;
                current = current.Next;   
                // use Next the instead of "next", otherwise it will loop forever
                temp.Invalidate();
            }

            head = null;
            count = 0;
            version++;
        }
Copy after login

6 Corresponding to only is to remove a series of interfaces of a certain node, which is similar to adding, and will not be repeated,

Clear calls Invalidate(), and the implementation is very simple:

        internal void Invalidate()
        {
            list = null;
            next = null;
            prev = null;
        }
Copy after login

7 To determine the existence of a node value as value, it calls the

Find method

,

Find method implementation details, similar

API

and FindLast, because it is a two-way linked list, so just traverse the linked list from the end,

8 Let’s look at another copy of the data to

Implementation of array

:

The above is the detailed content of .NET Framework-Doubly Linked List (LinkedList) Code Analysis (Figure). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use the removeLast() method of the LinkedList class to delete the last element in the linked list Use the removeLast() method of the LinkedList class to delete the last element in the linked list Jul 24, 2023 pm 05:13 PM

Use the removeLast() method of the LinkedList class to delete the last element in the linked list. LinkedList is a common data structure in the Java collection framework. It stores elements in the form of a doubly linked list. Through the methods provided by the LinkedList class, we can easily operate on the linked list, such as adding, deleting and modifying elements. In some scenarios, we may need to delete the last element in the linked list. LinkedList class provides removeLas

Java program to add elements to LinkedList Java program to add elements to LinkedList Aug 26, 2023 pm 10:21 PM

LinkedList is a general class of JavaCollectionFramework, which implements three interfaces: List, Deque and Queue. It provides the functionality of the LinkedList data structure, a linear data structure in which each element is linked to each other. We can perform a variety of operations on a LinkedList, including adding, removing, and traversing elements. To add elements to the LinkedList collection, we can use various built-in methods such as add(), addFirst(), and addLast(). We will explore how to use these methods to add elements to a LinkedList. in Java

Reverse doubly linked list grouping by given size using C++ Reverse doubly linked list grouping by given size using C++ Sep 04, 2023 am 09:49 AM

In this problem, we are given a pointer to the head of the linked list and an integer k. In a group of size k, we need to reverse the linked list. For example -Input:1<->2<->3<->4<->5(doublylinkedlist),k=3Output:3<->2<->1<->5<->4 looking for solutions Method In this problem, we will formulate a recursive algorithm to solve this problem. In this method we will use recursion and solve the problem using recursion. Example#include<iostream&

How to use LinkedList.addFirst() method to add elements to the head of a linked list in Java? How to use LinkedList.addFirst() method to add elements to the head of a linked list in Java? Nov 18, 2023 pm 03:51 PM

The LinkedList class in Java provides the addFirst() method to add elements to the head of the linked list. The function of this method is to add an element to the beginning of the linked list and move other elements of the original linked list back. The following is sample code that uses the LinkedList.addFirst() method to add elements to the head of a linked list: importjava.util.LinkedList;publicclassMain{pu

Interpretation of Java documentation: Analysis of the addFirst() method function of the LinkedList class Interpretation of Java documentation: Analysis of the addFirst() method function of the LinkedList class Nov 03, 2023 am 09:09 AM

Interpretation of Java documentation: Functional analysis of the addFirst() method of the LinkedList class. LinkedList is a doubly linked list implementation class in the Java collection framework. It provides a series of methods for adding, deleting and searching operations in the list. Among them, the addFirst() method is one of the important methods in the LinkedList class. This article will provide an in-depth analysis of the functions of the addFirst() method, with specific code examples. addFirst() method

How to convert LinkedList to Array in Java? How to convert LinkedList to Array in Java? Aug 29, 2023 pm 11:09 PM

The toArray() method of the LinkedList class converts the current LinkedList object into an array of object type and returns it. This array contains all the elements in this list in correct order (from first element to last element). It acts as a bridge between array-based and collection-based APIs. So, convert LinkedList to array - instantiate LinkedList class. Populate it using the add() method. Call the toArray() method on the linked list created above and retrieve the array of objects. Converts each element of an array of objects to a string. Example Real-time demonstration of importjava.util.Arrays;importjava.uti

Interpretation of Java documentation: Functional analysis of the addLast() method of the LinkedList class Interpretation of Java documentation: Functional analysis of the addLast() method of the LinkedList class Nov 03, 2023 pm 02:26 PM

Interpretation of Java documentation: Functional analysis of the addLast() method of the LinkedList class. In the Java collection framework, the LinkedList class is a List interface implemented by a doubly linked list. The LinkedList class provides many methods for operating linked lists, including the addLast() method. This article will provide a detailed analysis of the addLast() method of LinkedList and provide specific code examples. The function of the addLast() method is to add the specified element

.NET Deep Dive: Mastering Asynchronous Programming, LINQ, and EF Core .NET Deep Dive: Mastering Asynchronous Programming, LINQ, and EF Core Mar 31, 2025 pm 04:07 PM

The core concepts of .NET asynchronous programming, LINQ and EFCore are: 1. Asynchronous programming improves application responsiveness through async and await; 2. LINQ simplifies data query through unified syntax; 3. EFCore simplifies database operations through ORM.

See all articles