[C #] basic collection - Stack Stack list LinkList ordered list SortedList

Stack: it is a last in first out (LIFO) container

Stack push () method to add elements in the stack, acquired recently added element with Pop () method.

In Queue <T> class is similar, Stack <T> class implements IEnumerable <T> and ICollection interface

Count returns the number of elements in the stack

Push to add an element in the top of the stack

Pop delete an element from a stand still, and returns that element, if the stack is empty, throws an InvalidOperationException

Peek returns the top of the stack elements, but does not delete it

Contains Determines whether an element is in the stack, and if so, it returns true.

List

LinkedList <T> is a doubly linked list whose elements point to its front and rear elements.

Such an element can move forward to the next traverse through the entire list. By moving the front element to traverse the entire list may be reversed.

Advantage of the list: the list of elements into an intermediate position, using the list will be very fast.

When inserting an element, from a need to modify elements on the Next and Previous references cited next element, so that they reference the inserted element.

In List <T> class, when an element is inserted, the latter need to move all the elements of the element.

List drawback: the list element can only access one by one, it takes a long time to find elements located in the middle or end of the list

The list can not be stored only element in the list. When the storage element, the list must also store the next element of each element and an element of information. This is LinkedList <T> element contains the reason LinkedListNode <T> type.

Use LinkedListNode <T> class can be obtained the next element of the list and the previous element. LinkedListNode <T> defines the properties List, Next, Previous, and Value.

Before and after the node associated with the node List property returns the LinkedList <T> objects, Next and Previous properties to traverse the list, the current access node. Value of the return element associated with the node, which is a type T.

Members LinkedList <T> class definition can access the first and last elements (First and Last) linked list.

Insertion element in the specified location (AddAfter (), AddBefore (), AddFirst (), AddLast () method)

The element Removes the specified location (Remove (), RemoveFirst (), RemoveLast () method)

From the beginning of the list (Find () method) or end (FindLast ()) to start the search element.

 Ordered list

If the desired set key based on the need to sort, can be used SortedList <TKey, TValue> class.

This class sorted according to the key element. The set values ​​and can use any type of keys.

IComparer <Tkey> interface objects, the interface element is used to sort the list

This can Add () method and the indexer to add elements to the list. As a key index needs to index parameter.

If the key already exists, Add () method throws a ArgumentException exception type.

If the index uses the same key, it replaces the old value with the new value.

If you try to use the index to access an element, but the pass key does not exist, it will throw a

KeyNotFoundException types of exceptions. To avoid this anomaly, you can use the ContainsKey () method,

If the key is present in the bed set, this method returns true. Can call the TryGetValue () method, which attempts to obtain the specified key. If the key corresponding to the specified value does not exist, the method will not throw an exception, this method returns true return type is contrary is false key value corresponding to the presence of an ordered list bool value. The first parameter is the method the second parameter is a key value (if there is a corresponding value is out of the value.) out T value

 

Guess you like

Origin www.cnblogs.com/SignX/p/11272910.html
Recommended