Common table structures and data structure of data structures

Source: Internet
Author: User

Common table structures and data structure of data structures

Recently, I thought it was necessary to combine my blog park with my accumulation in recent years. some common tables in Net are summarized once. On the other hand, I hope to help you with some problems. I hope you can correct them if they are incorrect, thank you !!!

Array Structure of the table (for one-dimensional arrays ):

Overview:Array isFixed LengthThe table structure. Its elements must be stored inMemory space.

Function Description:It provides table creation, clearing, copying, and element access and sorting and search functions (because of its fixed length, it cannot add or delete elements)

Create an array: you can create an array by initializing the array or assigning values to the subscript.

Clear Array: this function is mainly implemented by the static method of the Array class. You can clear the elements in the specified range and reset them to the initial value.

Copy an Array: The quiet copy implemented by the static method of the Array class. You can copy the source ArrayElements within the specified rangeCopySpecified position in the target array

Element search: Implemented by static methods of the Array class, elements support forward search, response search, the position of elements in the array, the specified elements to be searched, sequential search, and binary search (note: elements in a table to be searched using the binary method must be sorted)

Element sorting: Implemented by the static method of the Array class. The Array elements are re-ordered using a fast sorting algorithm.

Element access: Implemented by the instance method. The element's position in the table is randomly accessed by the subscript accessor GetValue and SetValue methods.

1 // create an array: Initialize the Array (created by initialization) 2 int [] arr_int = new int [] {1, 2, 3, 4, 5 }; 3 // array output result: {1, 2, 3, 4, 5} 4 5 // create an array: Create an array 6 int [] array_int = new int [2] by assigning a value to the next mark; 7 array_int [0] = 1; 8 array_int [1] = 1; 9 // Array output result: {1, 2} 10 // clear Array: 12 Array. clear (arr_int, 0, 1); 13 // array output result: {0, 2, 3, 4, 5} 14 15 // copy the array: 16 // var copyArr_int = new int [] {}; // if it is copied to an Array without a fixed length, 17 var copyArr_int = new int [5]; 18 Array will be reported directly. copy (arr_int, copyArr_int, arr_int.Length); 19 // Array output result: {0, 2, 3, 4, 5} 20 21 // Array search: 22 var number = Array. find (arr_int, e => e = 3); // search for the specified Element 23 var index = Array. indexOf (arr_int, 2); // returns a 24 var lastvalue = Array. lastIndexOf (arr_int, 3); // reverse search 25 var index1 = Array. findIndex (arr_int, e => e = 2); // The Position of the search element in the Array is 26 27 Array. sort (arr_int); 28 var binIndex = Array. binarySearch (arr_int, 0); // element 29 30 in the binary search table // Array sorting: 31 Array. sort (arr_int ); 

Application scenarios:When the data we operate meets the following conditions, we can use this data structure.

1. fixed number of elements (Fixed Array length)

2. No need to dynamically add or delete Elements

3. Random Access by elements in a table

Performance description: 

Element access: Because we can only access array elements through subscript, GetValue, and SetValue, the access difficulty of this method is 0 (1). Note: because additional packing operations may occur when GetValue and SetValue methods are used for random access, we recommend that you use the following method to access data.

Element sorting: sort by quick sorting. the time complexity of this operation is O (N * log N)

Element search: There are two types of search for elements: sequential search and binary search. If sequential search is used, the time complexity of the operation is O (N). If binary search is used, the time complexity of the operation is O (log N)

Array Structure of the table (ArrayList Array):

Overview:ArrayListshi stores all elements by maintaining an array of _ items object types.

Function Description:It allows you to create, clear, and copy tables, and access, search, add, delete, and sort table elements.

Create a table: instantiate _ items, and copy the imported element set to _ items.

Clear: clears data in _ items by calling the Array. Clear method.

CopyTo: Copies the elements within the specified range in the table to the specified position in the target table, which is achieved by calling the Array. Copy method.

Element access (this []): You can only use the element location in the table to randomly access the element through the index.

Add element: you can Add a new element to the end of the table, or Insert it to the specified position (Insert ); you can add a group of new elements to the end of the table (AddRange) or insert them to the specified position (InsertRange)

Delete an element: You can Remove a specified element from a table, or delete an element at a specified position in the table (RemoveAt ), you can also delete the elements in the specified range in the table (RemoveRange)

Sorting of elements (Sort): Quickly sorts elements in a table so that users can use the binary method to search for elements.

Element search: You can perform sequential search (IndexOf and LastIndexOf, by calling Array. indexOf and Array. lastIndexOf method implementation); you can also use binary search (BinarySearch, call Array. binarySearch method implementation)

Performance description

Element access: although the indexer is used externally for Random Access to elements, it actually uses the array subscript to access the elements of _ items, therefore, the time complexity of this operation is O (1)

Element addition: although it provides the element addition function for the external, it only specifies the value assignment of the array element for the internal,

If an element (Add) is added at the end of the table, it simply assigns a value to the specified array element. Therefore, the time complexity of this operation is O (1 ); if you Insert an element (Insert) in the middle of the table, you need to move the elements after the specified position in the array backward, and then assign a new value to the array element at the specified position, the time complexity of this operation is O (N)

As long as there is a new element, there will be a resizing problem. ArrayList creates a new array with a larger size, copies all existing elements to the specified position in the new array, and then saves the new elements to the array.

Delete element: when the specified Element in the table is deleted, move the elements after the deleted element in _ items forward, that is, all the elements in _ items must be concentrated in the front of the array, and no empty element exists in the middle. Therefore, the time complexity of this operation is O (N)

Sort elements: Use the quick sort method to re-sort the elements in the table. The time complexity of this operation is O (N * log N)

Element search: If sequential search is used, the time complexity of the operation is O (N). If the table elements have been sorted, if binary search is used, the time complexity of this operation is O (log N)

 

ApplicationScenario:ArrayList is applicable when the data to be operated meets the following conditions:

1. Random Access to elements in a table;

2. dynamically add and delete elements;

3. The data types of each element cannot be compatible;

4. Ability to sort elements;

 

List of table generic sets <T>

Overview

It implements a strong table structure that can dynamically add or delete elements, and maintains an array _ items of strong T types internally to store all elements, therefore, it only provides the method of accessing elements through the element location.

It provides the same functions as internal implementation and ArrayList. The only difference is that ArrayList is relatively weak and has no type security guarantee; list <T> is strongly typed with type security.

ArrayList: It operates on an object array. Therefore, all data added to it must be converted to the object type first. This requires packing for the value type, this also means that ArrayList may store various types of data, and the data is not necessarily compatible.

List <T>: It operates on an array of the specified type T, which not only avoids the packing operation of the value type, but also provides type security, all data added to it must be compatible with the T type.

Function Description:For more information, see ArrayList.

Performance description:Refer to the ArrayList Performance description.

Application scenarios:The Use Cases of List <T> and ArrayList are only different-whether the compatibility of the Data Types of each element can be ensured: if the data types of each element can be guaranteed, List <T> is used, and ArrayList is not guaranteed.

 

Table linked list Structure

Overview:The listlist <T> provides a table structure implemented by using a two-way linked list. A group of worker listnode <T> nodes are maintained internally, each node stores its own list, prev, next, and items ). In a table in the form of a linked list, there are two concepts: node and element. Although they are associated, they are different. An element is the real data that needs to be stored in the table structure, but it does not contain any information that can access other elements, so it is impossible to implement a table in the form of a linked list; the node not only stores the real data to be stored in the table structure, but also stores the information for accessing other nodes. It can be seen from the above: elements are the real data that needs to be stored in the table structure, while nodes are the carriers of these elements and the basic units that constitute the linked list.

Because the linked list is composed of nodes, the access to table elements must be indirectly accessed through the nodes where the elements are stored, and cannot be directly accessed. Because each node records the memory addresses of the previous and next nodes, these nodes do not need to be stored in the continuous memory space.

Function Description:It allows you to create, clear, and copy tables, and to access, add, delete, and search table elements.

Create a table: instantiate the table, and create corresponding nodes based on the input elements to add the newly created table.

Clear a table: Clear all nodes in the table.

Table replication: Copies the elements stored in all nodes in the table to the specified position of the specified array.

Element access: you must first obtain the node for storing the element through the element search, and then access the element through the node. However, the access to the First or Last element of the linked list is not so cumbersome. You only need to use the First or Last attributes to directly obtain the First or Last node for storing them.

Element addition: adding an element is actually adding a node. When adding an element, you can either input the element to be added or the node containing the new element, however, the same node cannot be added to different linked lists. You can add an element to the table header (AddFirst), add the element to the end of the table (AddLast), or insert the element before or after the specified Element (AddBefore ).

Delete an element: deleting an element is actually deleting a node. When deleting an element, You can import the element to be deleted or the node to be deleted (this node must belong to this table ). When you input an element to be deleted, the system first searches for the node where the element is stored and then deletes it. In addition to deleting a specified element, you can also delete special elements: the first element (RemoveFirst) or the last element (RemoveLast ).

Element search: searches for nodes that store the specified element from the table. You can search either forward or reverse (FindLast ).

Performance description

Element search: nodes in the linked list can only be accessed sequentially, so the time complexity of this operation is O (N ).

Element access: the structure of the linked list determines that it cannot support Random Access (I .e., it cannot directly access the elements through the element location ), therefore, the random list <T> does not provide random access.

Because the First or Last node of the linked list can be directly obtained through the First or Last attribute, therefore, the time complexity for accessing the first or last element in the linked list is O (1 ).

All other elements must first search for the node where the element is stored, and then access the element through the searched node. Therefore, the time complexity of this operation is O (N ).

Addition of elements: the operation of adding elements in the linked list is relatively simple. You only need to modify the prev of the next and next nodes of the previous node and the values of the prev and next nodes that store the element. The time complexity of this operation is O (1 ).

Element deletion: The operation of deleting elements in the linked list is relatively simple. You only need to modify the prev of the next and next nodes of the previous node and reset the next, prev, and list of the deleted nodes. The time complexity of this operation is O (1 ).

Application scenarios:When the data to be operated meets the following conditions, it is more suitable to use the shortlist <T>:

1. Frequent addition and deletion of elements;

2. Generally, only all elements in the table are accessed sequentially, and few elements need to be randomly accessed;

3. You do not need to sort the elements in the table.

Application instance:If you want to run this strength, you can remove the redis Cache

Using HB. cache; using System. collections. generic; using System. linq; using System. runtime. serialization; namespace HB. test {/// <summary> ///// </summary> class Program {/// <summary> //// </summary> /// <param name = "args"> </param> static void Main (string [] args) {try {// obtain the linked list var documentLinkeList = GetDocumentLinkeList (); // obtain the first var first = documentLinkeList of the linked list. first; // obtain the last var last = documentLinkeList of the linked list. last; // obtain the specified var document = documentLinkeList in the linked list based on the query conditions. firstOrDefault (e => e. sequence = 1); var documentNote = documentLinkeList. find (document); // block the thread Console. readKey ();} catch (Exception ex) {Log4netHelper. debug <Program> ("exception:", ex) ;}/// <summary> // obtain the two-way linked list of the document. // remarks: redis cache is added /// </summary> /// <returns> </returns> private static Response List <Document> GetDocumentLinkeList () {var key = "DocumentLinkeList: Test "; optional list <Document> documentLinkeList = null; ExchangeRedisHelper cache = new ExchangeRedisHelper (); if (cache. containsKey (key) {documentLinkeList = cache. getFromBinary <shortlist <Document> (key) ;}else {List <Document> documentList = new List <Document> (); documentList. add (new Document () {Title = "one", Content = "Sample_one", Priority = 8, Sequence = 1}); documentList. add (new Document () {Title = "two", Content = "Sample_two", Priority = 8, Sequence = 2}); documentList. add (new Document () {Title = "three", Content = "Sample_three", Priority = 8, Sequence = 3}); documentList. add (new Document () {Title = "four", Content = "Sample_four", Priority = 8, Sequence = 4}); documentList. add (new Document () {Title = "five", Content = "Sample_five", Priority = 8, Sequence = 5}); documentList. add (new Document () {Title = "six", Content = "Sample_six", Priority = 8, Sequence = 6}); documentList. add (new Document () {Title = "seven", Content = "Sample_seven", Priority = 8, Sequence = 7}); documentList. add (new Document () {Title = "eight", Content = "Sample_eight", Priority = 8, Sequence = 8}); documentList. add (new Document () {Title = "nine", Content = "Sample_nine", Priority = 8, Sequence = 9}); documentList. add (new Document () {Title = "ten", Content = "Sample_ten", Priority = 8, Sequence = 10}); documentLinkeList = new canonical list <Document> (documentList ); var expireSeconds = (int) DateTime. now. addDays (1 ). subtract (DateTime. now ). totalSeconds; cache. setToBinary (key, documentLinkeList, expireSeconds);} return documentLinkeList ;}} /// <summary> /// the elements stored in the linked list are of the Document type. /// </summary> [Serializable] [DataContract] public class Document {// <summary> /// document title /// </summary> [DataMember (Order = 1)] public string Title {get; set ;}/// <summary> /// document content //</summary> [DataMember (Order = 2)] public string Content {get; set ;}/// <summary> /// document priority /// </summary> [DataMember (Order = 3)] public byte Priority {get; set ;}/// <summary> /// sorting field /// </summary> [DataMember (Order = 4)] public int Sequence {get; set ;}}}

Related Article

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.