Shenzhen Ka Wah Road _12 _ architect advanced data structure analysis

I. Introduction

Data structure is stored in the computer, data is organized. Data structure refers to the presence of one or more data elements of the particular relationship between each set. Typically, the data structure may be carefully selected to bring higher operational or storage efficiency.

1、Array

Array is the most simple data structure. Which has the following characteristics:

  1. Arrays are stored in a continuous memory.
  2. The contents of the array are the same type.
  3. Arrays can be accessed directly through the index.

Array An array of creation:

1 int size = 5;2 int[] test = new int[size];

When will create a new array  CLR managed heap to allocate a contiguous memory space to bloom quantity of size, type of the declared type of array elements. If the type is a value type, then there will be a size unboxed value of that type is created. If the type is a reference type, then there will be a size corresponding type of reference is created.

Because it is in contiguous memory storage, so it's very fast indexing speed, access time is an element that is constant regardless of the number of elements in the array, and assign and modify elements is simple.

String [] = test2 new new  String [ . 3 ]; // Assignment

test2[0] = "chen";

test2[1] = "j";

test2 [ 2 ] = "D" ; // Modify

test2[0] = "chenjd";

But there are advantages, then it must be accompanied by drawbacks. Because it is stored contiguously, so the new element is inserted between the two elements becomes inconvenient. And, like the code above shows, when you declare a new array, you must specify its length, this will there is a potential problem, and that is when we declare the length is too long, will obviously be a waste of memory, when we declared too short length of time, the risk that overflow. This makes writing code like speculation, very small man can dislike such behavior! In view of this shortcoming, the following grand launch ArrayList.

2.ArrayList

  To address you must specify the length of an array is created and stored only drawback of the same type and the introduction of data structures. ArrayList is part under the System.Collections namespace, so it must be introduced to use System.Collections. As mentioned above, ArrayList solve some of the shortcomings of the array.

  1. Without specifying its length when declaring ArrayList, which is due to the length of the ArrayList object is in accordance with the data stored therein to the dynamic growth and reduced.
  2. ArrayList can store different types of elements. This is because it would ArrayList elements as Object to deal with. Accordingly, addition of elements of different types are allowed.
ArrayList的操作:
ArrayList test3 = new ArrayList();//新增数据
test3.Add("chen");
test3.Add("j");
test3.Add(25);//修改数据
test3[2] = 26;//删除数据
test3.RemoveAt(2);

  He said so a bunch of "advantage", but also the talk about the shortcomings of it. Why give "advantages" marked the quotes it? That reason is because ArrayList can store different types of data is due to all types as Object to do deal with, that is to say ArrayList elements are actually of type Object, spicy what the problem came.

  1. ArrayList not type safe . Because of the different types are processed as Object to do, the situation is likely type mismatch occurs when using ArrayList.
  2. As hereinbefore complaints, did not occur when the packing storage array value types, but all types since the ArrayList as the Object, is inevitably inserted when value types during will occur packing operation, split occurs when the value of the index box operation. You may be able to do?

Note: Why is frequently unnecessary boxing and unboxing can not bear it? Listen small every man slowly come: the so-called packing (Boxing): is the value of the object instance to the type of conversion (Baidu Encyclopedia). So unboxing: reference type is to be converted to a value type slightly (or from Baidu Encyclopedia). Below is an ~ chestnut

// packing, assigning a value of type String FanyoyChenjd to the object. int   info =  1989;   Object  obj = ( Object ) info;  

// unpacking, to extract values from the Obj of info Object  obj =. 1;
int  info = ( int ) obj;

Then the conclusion? Well, let every man's very little reference to Baidu Encyclopedia low again. It is apparent from the principle, when packing, is a new generation of reference objects, which have lost time, which is resulting in reduced efficiency.

3、List<T>

  In order to address the shortcomings of type ArrayList unsafe unpacking and packing, so there are a generic concept, introduced as a new array type. Array is often used in the type of work. And ArrayList very similar length can be flexible to change, the biggest difference is that when you declare List collection, we also need for the type of object within a set of data declarations List, and Array and this is very similar, in fact, List <T> Internal use the Array to achieve.

List<string> test4 = new List<string>();  
//新增数据  test4.Add(“Fanyoy”);  
test4.Add(“Chenjd”);  
//修改数据  
test4[1] = “murongxiaopifu”;  
//移除数据  
test4.RemoveAt(0);  

The greatest advantage is to do so

  1. Ensuring type safety.
  2. Also canceled the operation boxing and unboxing.
  3. It combines the advantages of Array provides quick access to the advantages of flexibility and length ArrayList change.

4、LinkedList<T> 

That is the list. And the maximum array is different from the above list that is stored in the sort memory may be discontinuous. This is due to the chain by one point to the next element to element arrangement, it may not be able to access the index. Figure

Since the biggest feature list is stored in the memory space is not necessarily continuous, then the list relative to the largest array of advantages and disadvantages are obvious.

  1. Inserted into the list or delete nodes without adjusting the capacity of the structure. Because the store itself is not continuous but by the decisions of each object pointer, so add elements and remove elements should have an advantage over the array.
  2. List for add new elements required in an orderly sort of situation, there is also an array of get to do comparison, for example, to add a new element somewhere in the middle of the array, you may need to move a lot of moving elements, and for the list in terms of possible just point to several elements of change only.
  3. There are advantages disadvantages, because it is not necessarily arranged in series in the memory space, the access time can not use the index, but to start from scratch node, a node until the next successive traverse to find the target. So when you need quick access to objects, arrays, certainly an advantage.

  In summary, the list for the number of elements is not fixed , needs to node often changes in the situation.

5、Queue<T> 

Que in Queue <T> this data structure, the first element will be inserted first to be deleted; otherwise the last inserted element will be removed last, so queue also known as "first in first out" (FIFO-first in first out) in linear form. To achieve access to the Queue <T> Enqueue and Dequeue by using these two methods.

Some need to pay attention to:

  1. FIFO scenario.
  2. By default, Queue <T> The initial capacity of 32, a growth factor of 2.0.
  3. When using the Enqueue, it determines whether the queue length is sufficient, if less, based on the growth factor to increase the capacity, for example, when the initial 2.0, then 2-fold increase of the capacity of the queue.
  4. Lackluster.

  About Queue <T> to use, there are respective upper MSDN example .

Stack<T>

6、Stack<T> 

And Queue <T> In contrast, when it is desired to use LIFO order (LIFO) data structure, we need to use Stack <T> a.

  Some need to pay attention to:

  1. LIFO scene.
  2. The default capacity of 10.
  3. Use pop and push to operate.
  4. Lackluster.

  Similarly, here you can see an example of a large number of Stack <T> of.

Dictionary<K,T>

7、Dictionary<K,T>

       Dictionary this thing, like a small but very, very ordinary man. After Tell me what they think they would be in the dictionary is not very liked, create a dictionary can be entered, and throwing things, add, delete, access is called a fast Zile. But until recently little man can read a big article of God, that she remembered the phrase "What a good thing that you have accounted for ye make it." So in the end what is hidden behind dictionary fog, then poke heavy fog, whether is the truth? Let's hear next points. . . Etc., should let us examine the dictionary it below.

  Mentioned dictionary would have to say Hashtable hash tables and Hashing (hash, also called hashes), since the dictionary implementation is the implementation of the hash table, but the dictionary is type-safe , that is when creating when the dictionary, you must declare the type of key and the item, which is the first dictionary with a hash table of difference . About content hash table of recommended look at this blog hash table . About Hash, simply it is a kind of a message of arbitrary length to a fixed length compression, such as a school student number range from 00000 to 99999, a total of five numbers, if each number corresponds to an index of words, then the index is 100 000, but if we use 3 as an index, the index range becomes 000 to 999, of course, the case of conflict, the situation is the hash collision (hash Collisions) a. Pulling away, on the specific implementation principle is to see a small man can recommend the essay blog, of course, that big thing I read blog turn word is quite dazzling. . .

  Back to Dictionary <K, T>, we operate in the dictionary advantage of a variety of time have enjoyed, its weaknesses Where is it? For le, it is space. Space for time, the more memory overhead to meet our pursuit of speed. When you create a dictionary, we can pass a capacitance value, but the actual use of capacity is not the value. But the use of "not less than the minimum prime number as the value of its actual capacity is used, the minimum is 3." (Zhao), when the actual capacity has not directly implemented index, but by creating an additional 2 array to achieve indirect indices, namely int [] buckets and Entry [] entries two arrays (that is, in fact, saved buckets index entries array), here it is the second dictionary and hash table difference, remember Ha Greek conflict? Yes, the second difference is that policy processing hash conflict is different ! Dictionary will use additional data structures to deal with hash collision, which is one of the array just mentioned buckets barrels, and buckets length is the actual length of the dictionary, because the buckets that mapped the location of each dictionary, then buckets each element which is a list of the hash to the same storage element, storage and redistribution.

 

Therefore, the situation we face is that, even if we create an empty dictionary, then accompanied by two array of length 3. So when little data processing, or carefully use a dictionary as well, using arrays in many cases also acceptable.

to sum up

 

Array

Determining the number of elements to be processed and may require the use of subscripts considered, but we recommend the use of List <T>

ArrayList

Not recommended, recommended to use List <T>

List <T> generic List

The number of elements to be processed is generally recommended to use uncertain

LinkedList<T>

List for the number of elements is not fixed, where the need for frequent changes in node, the second end may be increased or decreased

Queue<T>

FIFO case

Stack<T>

LIFO case

Dictionary<K,T>

Operation requires key-value pairs fast

2. The use of several common data structures

Published 37 original articles · won praise 3 · Views 6308

Guess you like

Origin blog.csdn.net/huan13479195089/article/details/105023864