C # sequence table,

Source: Internet
Author: User

C # sequence table,

1. Data is the carrier of the external world and can be processed by computers. 2. Data elements are the basic unit of data, such as the name, gender, and class of a student.

3. A data object is a collection of data, such as human beings and dogs. It is a data object and contains data elements.

4. data types, such as integer, string, and struct,

5. Data structures are generally divided into four types. 5.1 sets, such as HashSet <T>, are mainly designed for high-performance set operations (intersection, union, and difference set)

Next we will talk about HashSet <>.

5.2 linear structure data elements from one-to-one relationships such as arrays, lists, stacks, and Queue

5.3 tree structure, such as binary tree. The graphic structure. Let's talk about it later.

On the first day, we learned the sequence table and saved the linear table by placing the elements in the table one by one into the sequence storage unit. This is the sequence storage of the linear table. A sequential table is a sequence table that stores data elements in a sequential bucket with an address.

// Implement the sequence table public class SeqList <T> //: IList <T> {private int maxsize; // capacity of the ordered table public int Maxsize {get {return maxsize ;}} private T [] data; // array used to store the private int last data element in the ordered table; // indicates the position subscript public int Last {get {return last ;}set {last = value ;}} of the Last element of the sequence table ;}} // indexer public T this [int index] {get {return data [index] ;}set {data [index] = value ;}} public SeqList (int size) {data = new T [siz E]; maxsize = size; last =-1;} // The length of the sequence table. Because the array is a 0-base array, that is, the minimum index of the array is 0, the length of the sequence table is the last addition of 1 to the index of the last element in the number // group. Public int GetLength () {return last + 1;} // Clear public void Clear () {last =-1;} public bool IsEmpty () {if (last <0) {return true;} return false;} // determines whether the sequence table is full public bool IsFull () {if (last = maxsize-1) // element subscript starts from 0 {return true;} return false;} // Add the new element public void Appled (T item) {if (! IsFull () {data [++ last] = item;} else {Console. writeLine ("the sequence table is full and cannot be added") ;}// time complexity analysis of inserting a data element at the position of the I data element of the sequence table/* algorithm: the insert operation on the sequence table is mainly used to move data. an element is inserted at the position I, and the position from ai to an needs to be moved backwards, A total of n-I + 1 elements need to be moved, and the value range of I is 1 ≤ I ≤ n + 1. When I is equal to 1, the maximum number of elements to be moved is required, n; when I is n + 1, no elements need to be moved. If the probability of insertion at the position I is pi, the number of times the flat moving data elements is n/2. This indicates that the insert operation on the sequence table requires moving half of the data elements in the table on average. Therefore, the time complexity of the insert operation is O (n ). */Public void Insert (T item, int I) {if (I <1 | I> last + 1) {Console. writeLine ("invalid location"); return;} if (IsFull () {Console. writeLine ("the sequence table is full and cannot be added"); return;} if (I = last + 2) // you can determine whether to insert {data [last + 1] = item ;} the elements after else {// I move back for (int j = last; j> = I-1; -- j) {data [j + 1] = data [j];} // Insert a new data element to the I-1 position data [I-1] = item ;} + + last;} // Delete the I data element of the sequence table/* and the value range of I is 1 ≤ I ≤ N. When I is equal to 1, the maximum number of elements to be moved is n-1. When I is n, no elements need to be moved. If the probability of deletion at the position I is pi, the average number of moving data elements is (n-1)/2. This indicates that the Delete operation on the sequence table requires moving half of the data elements in the table on average. Therefore, the time complexity of the Delete operation is O (n) */public T Delete (int I) {T temp = default (T); // default is the default keyword if (IsEmpty () {Console. writeLine ("the sequence table is empty"); return temp;} if (I <1 | I> last + 1) {Console. writeLine ("invalid location"); return temp;} if (I = last + 1) {data [last --] = temp ;} else {temp = data [I-1]; for (int j = I; j <= last; ++ j) {data [j] = data [j-1] ;}} -- last; Return temp;} // obtain the I-th data element of the sequence table public T GetElem (int I) {if (IsEmpty () | (I <1) | (I> last + 1) {Console. writeLine ("the sequence table is empty or the location is invalid! "); Return default (T);} return data [I-1];} // find the data element with the value in the sequence table public int Locate (T value) {if (IsEmpty () {Console. writeLine ("the sequence table is empty"); return-1 ;}for (int j = 0; j <last; ++ j) {if (data [j]. equals (value) {return j ;}} return-1 ;}/ * algorithm time complexity analysis: the main calculation of value-Based lookup in the sequence table is comparison, the number of comparisons depends on the position of the given value in the table and the table length. When the given value is equal to the first data element, the number of comparisons is 1. When the given value is equal to the last element, the number of comparisons is n. Therefore, the average number of comparisons is (n + 1)/2, and the time complexity is O (n) */public void Reverse () {T temp = default (T ); int len = GetLength (); for (int I = 0; I <= len> 1; ++ I) {temp = data [I]; data [I] = data [len-I]; data [len-I] = temp ;}}}

 

List <T> indicates the mandatory Type List of objects accessed through indexes. Provides operations such as search, sorting, insertion, and deletion.

By learning linear tables, we know that List <T> implements the ordered table function, but List <T> is a linked List that dynamically stores data. This is the disadvantage of ordered tables, tomorrow we will study single linked list

I hope to learn from each other more. The following is a brief introduction to List <>

Function: The most common purpose of generics is to create a list class for a generic set. The data type of a list item may be int, string, or other, if the processing method for the List class is the same, you do not need to specify the Data Type in advance, which is left to be specified when the list class is instantiated. It is equivalent to taking the data type as a parameter, so that code can be reused to the maximum extent to protect the type security and improve performance. General Usage of List: System. collections. genericpublic class List <T>: IList <T>, Icollection <T>, IEnumerable <T>, IList, Icollection, IenumerableList <T> is the generic equivalent class of the ArrayList class, this class uses an array of sizes that can be dynamically increased as needed to implement IList <T> generic interface (1) declaration List <T> mlist = new List <T> (); eg: string [] Arr = {"a", "B", "c"}; List <string> mlist = new List <string> (Arr); (2) add an element List. add (T item) eg: mlist. add ("d"); (3) Add the set element eg: string [] Arr2 = {"f", "g ". "h"}; mlist. A DdRange (Arr2); (4) add an element Insert (int index, T item) eg: mlist at the index position. insert (1, "p"); (5) traverse the type of element foreach (T element in mlist) T in the List, which is the same as that declared by mlist {Console. writeLine (element);} eg: foreach (string s in mlist) {Console. writeLine (s);} (6) Delete the element List. remove (T item) deletes a value such as: mlist. remove ("a"); List. removeAt (int index); Delete the element eg: mlist whose subscript is index. removeAt (0); List. removeRange (int index, int count); starts with subscript index and deletes c Ount element eg: mlist. removeRange (); (7) determines whether an element is listed in the List. contains (T item) returns true or false. For example: if (mlist. contains "(" g ") Console. writeLine ("g in the List"); else mlist. add ("g"); (8) sort the List of elements in the List. by default, Sort () is an element. Each letter is in the ascending order of eg: mlist. sort (); (9) returns the List of elements in the List in reverse order. reverse () can be used with List. use Sort () with (10) List to clear the List. clear () eg: mlist. clear (); (11) obtain the List of the number of elements in the List. count () returns the int value eg: mlist. count (); List advanced, powerful method (1) List. findA Ll method: retrieve all elements that match the conditions defined by the specified predicate. class program {static void Main (stirng [] args) {student stu = new student (); stu. name = "arron"; List <student> students = new List <student> (); students. add (stu); students. add (new student ("candy"); FindName myname = new FindName ("arron"); foreach (student s in students. findAll (new Predicate <student> (myname. isName) {Console. writeLine (s) ;}} public class student {public String Name {get; set;} public student () {} public override string ToString () {return string. format ("Name: {0}", name) ;}} public class FindName {private string _ Name; public FindName (string Name) {this. _ name = Name;} public bool IsName (student s) {return (s. name = _ name )? True: false;} (2) List. the Find method searches for elements that match the conditions defined by the specified Predicate, and returns the First Matching Element eg: // Predicate in the entire List to delegate the method, if the object passed to it matches the condition defined by the delegate, the method returns true. The elements of the current List are passed to the Predicate delegate one by one and moved before the List, from the first element to the end of the last element, when a matching item is found, the processing stops. The first method is delegated to the lambda expression: eg: string listFind = mlist. find (name => {if (name. length> 3) return true; return false;}); the second method is delegated to the function eg: public bool ListFind (string name) {if (name. length> 3) {return true;} return False;} the results of the two methods are the same (3) List. the FindLast method public T FindLast (Predicate <T> match); determines whether each element in the List matches the conditions defined by the specified Predicate. The usage is the same as that of List. Find. (4) List. TrueForAll method: determines whether each element in the List matches the condition defined by the specified predicate. Public bool TrueForAll (Predicate <T> match); (5) List. take (n): the return value of the first n rows is IEnumetable <T>. The T type is the same as that of List <T>. g.: IEnumerable <string> takeList = mList. take (5); foreach (string s in takeList) {Console. writeLine ("element in takeList:" + s);} at this time, takeList stores the first five (6) lists in mList. where Method: retrieve all elements that match the conditions defined by the specified predicate. Similar to the List. FindAll method. E. g.: IEnumerable <string> whereList = mList. where (name => {if (name. length> 3) {return true;} else {return false ;}}); foreach (string s in subList) {Console. writeLine ("element in subList:" + s);} in this case, subList stores all elements with a length greater than 3 (7) List. removeAll: removes all elements that match the conditions defined by the specified predicate. Public int RemoveAll (Predicate <T> match); E. g.: mList. removeAll (name => {if (name. length> 3) {return true;} else {return false ;}}); foreach (string s in mList) {Console. writeLine ("element in mList:" + s);} in this case, mList stores the elements whose length is greater than 3. References http://www.cnblogs.com/ustc_msra_ase/articles/1890395.html

 

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.