The linked List is a form of data storage in the data structure. We often use the List <T>, ArrayList, Hashtable, and other container classes. During the access operation, the Array is used for storage, listDictionary and dictionary list <T> are saved in the form of a linked list instead of Array.
Advantages and disadvantages of linked list
Take ListDictionary as an example. In the source code, you cannot see the Array type variable, instead ofDictionaryNodeYou can view the source code of the class and find that only one key, one value, and oneDictionaryNodeType of next variable,DictionaryNodeThe Code is as follows::
private class DictionaryNode{ public object key; public ListDictionary.DictionaryNode next; public object value;}
When adding data, the next variable of the current node is directly assigned to a new node, so that a node deducts a node and the chain form is available.
When searching data in the Linked List, such as callingContains(Object key): the bool method, which needs to be traversed from the first node of the linked List and matched one by one. Therefore, the time complexity is O (n). Compared with List <T> and ArrayList, there is no big difference in query efficiency.
What are the advantages of linked lists?The answer is: Memory saving.
As mentioned in previous articles, during initialization of linear and hash tables, an internal Array is set to a default size. The initial value of List <T> is 4, and that of Hashtable is 11, when you encounter insufficient capacity when adding data, the current array will be expanded by two times, which will inevitably cause waste. Instead of storing arrays and connecting nodes, the linked list occupies the memory of several nodes. Compared with linear tables and hash tables, the linked list does not waste much space.
In addition to space saving, the linked list also has the advantageData insertion flexibility.
Unfortunately, this is not reflected in listdictionary. Each time you add data, listdictionary traverses the entire linked list to ensure that no duplicate nodes exist. As a result, each addition must be cyclically executed, the time complexity of adding and querying data is O (n), which is much slower than linear and hash tables.
HybridDictionary-combines the characteristics of linked lists and hash tables to foster strengths and circumvent weaknesses
In. in the collection container of. net, there is a class named hybriddictionary, which makes full use of the advantages of hashtable's high query efficiency and listdictionary's less memory space. It has built-in hashtable and listdictionary containers, the internal logic for adding data is as follows:
If the data volume is less than 8, hashtable is null and the data is saved using listdictionary.
If the data volume is greater than 8, instantiate hashtable, transfer the data to hashtable, and set listdictionary to null.
The code for the add method of hybriddictionary is as follows:
Public void add (Object key, object Value) {If (this. hashtable! = NULL) {This. hashtable. Add (Key, value);} else if (this. List = NULL) {This. List = new listdictionary (this. caseinsensitive? Stringcomparer. ordinalignorecase: NULL); this. list. add (Key, value);} else if (this. list. count + 1)> = 9) {// when the data volume is greater than 8, call this method to instantiate hashtable, transfer data, and clear list this. changeover (); this. hashtable. add (Key, value);} else {This. list. add (Key, value );}}
The HybridDictionary class further illustrates the characteristics of the ListDictionary linked list: Compared with Hashtable, it occupies less memory. However, as the data volume increases, the query efficiency is far lower than Hashtable.
Generic linked list-generic list <T>
The shortlist is a generic linked list and also accessed by nodes. The node type isLinkedListNode<T>, Unlike the ListDictionary node,LinkedListNode<T> There are two directions: next and prev, indicating that the lateral list <T> is a two-way linked list, while the ListDictionary is a one-way linked list. The Code is as follows:
public sealed class LinkedListNode<T>{ // Fields internal T item; internal LinkedList<T> list; internal LinkedListNode<T> next; internal LinkedListNode<T> prev; ......}
In addition to saving memory space, another advantage of the linked list is the flexibility of data insertion, which is fully reflected in the shortlist <T>. There are four ways to add data in different locations, insert Chain headers, chain tails, Before nodes, and after nodes respectively.
There are two insert modes for each insert method:
1. Insert directlyLinkedListNode<T>, No return value.
2. Insert a value of the T type directly, and return the node after the insertion is complete.
There are eight data insertion methods in four locations and two modes. using these methods, you can flexibly control the data sequence in the Link Table when adding data, this advantage is incomparable between a linear table and a hash table. The Code is as follows:
public LinkedListNode<T> AddAfter(LinkedListNode<T> node, T value);public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode);public void AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode);public LinkedListNode<T> AddBefore(LinkedListNode<T> node, T value);public void AddFirst(LinkedListNode<T> node);public LinkedListNode<T> AddFirst(T value);public LinkedListNode<T> AddLast(T value);public void AddLast(LinkedListNode<T> node);
In addition, because the forward list <T> is a two-way linked list, it provides two query methods for data query: "from front to back" and "from Back to Back, therefore, although the time complexity of the linked list is O (n) In theory, the query efficiency can be improved by combining these two methods based on your grasp of the sequence when inserting data.
Public writable listnode <T> Find (T value); // check the public writable listnode <T> FindLast (T value) from the beginning to the end; // check the result from the back to the end.
Conclusion
Compared with linear tables and hash tables, the linked list saves memory space.
ListDictionary is not recommended when you add data to a time series table with low efficiency and large data volumes and frequent insertion.
Generic linked list shortlist <T> provides great flexibility in adding data sequence while saving memory space. In addition, generics avoid the advantages of packing and unpacking, when you need to use a linked list, you should give priority to generic linked lists.