2nd. Linear table "C # data structure and algorithm"

Source: Internet
Author: User
Tags table definition

(1Except for the data element in the first position, there is only one data element in front of the location of the other data element;2except for the data element at the last position, there is only one element behind the location of the other data element. That is, the data element is arranged one after the other. Therefore, the linear table can be imagined as a data structure of a sequence of data elements. 2.1.1The interface of the linear table definition linear table (list) linear table is shown below.  Public InterfaceIlistds<t> {      intGetLength ();//Request Length      voidClear ();//Clear Operation      BOOLIsEmpty ();//determine if a linear table is empty      voidAppend (T item);//Additional Actions      voidInsert (T Item,inti);//Insert OperationT Delete (inti);//Delete OperationT Getelem (inti);//take a TABLE element      intLocate (T value);//Find by ValueIn order to differentiate itself from the interface IList in the. NET Framework, "DS" is appended to the IList and "DS" represents the data structure. 2.2Sequential Table2.2.1in a computer, the simplest and most natural way to save a linear table is to put the elements in the table one by one into the sequential storage unit, which is the sequential storage of the linear table (Sequence Storage). Sequential storage of linear tables is a sequential table of linear tables (Sequence list) stored in this way by storing the data elements of a linear table sequentially in memory with a contiguous space of addresses .2.1is shown.        The sequential table is characterized by adjacent data elements in the table that are stored in the memory location. 0 1...-1I ... n-1... maxsize-1A1 A2 ... ai-1Ai ai+1..... an ...  The memory footprint of an array in the C # language is a contiguous set of storage areas, so the array has random access characteristics.    Therefore, arrays are inherently characterized by data storage areas that represent sequential tables. Since any linear table can be inverted, the operation can be declared as a member method of the Ilistds interface, and then implemented in each linear table class. The declaration of the inverted method reverse in the Ilistds interface is as follows: Public InterfaceIlistds<t>{...voidReverse ();} "Example2-2"There is a sequential table of data type integer La and Lb whose data elements are sorted in ascending order from small to large, and an algorithm is written to merge them into a single table LC, which requires data elements in the Lc to be in ascending order (C # language version)2.2Sequential tablePanax Notoginsengcolumn. Algorithm idea: Scan the data elements of LA and lb sequentially, compare the values of the current data elements of LA and LB, assign the data elements of the smaller values to the LC, until one of the sequential tables is scanned, and then assign the remaining data elements in that sequential table to the LC. The capacity of the Lc should be able to accommodate the length added by the two tables of La and Lb. An algorithm for merging two tables in ascending order C # is implemented as follows: Publicseqlist<int> Merge (seqlist<int> La, seqlist<int>Lb) {Seqlist<int> Lc =Newseqlist<int> (la.maxsize +lb.maxsize); inti =0; intj =0; intK =0; //There are data elements in all two tables     while((I <= (La.getlength ()-1)) && (J <= (Lb.getlength ()-1)))    {    if(La[i] <Lb[j]) {Lc.append (la[i++]); }    Else{lc.append (lb[j++]); }    }    //There are also data elements in table a     while(I <= (La.getlength ()-1) {lc.append (la[i++]); }    //There are also data elements in table B     while(J <= (Lb.getlength ()-1) {lc.append (lb[j++]); }    returnLc; The time complexity of the algorithm is O (M+N), M is the table length of La, and n is the table length of Lb. 2.3a single-linked list of sequential tables stores individual data elements in a linear table in sequential order of storage cells, logically adjacent data elements are also adjacent to the physical location. Therefore, it is convenient to find data elements in any one location in a sequential table, which is an advantage of sequential storage. However, when inserting and deleting sequential tables, it is necessary to implement them by moving data elements, which affects the efficiency of operation. This section describes another storage structure for linear tables-chained storage (Linked Storage), which is called a linked list (Linked list). Linked lists do not require logically contiguous data elements to be adjacent to the physical storage location, so there is no need to move data elements when inserting and deleting the linked list, but it also loses the advantage of having the sequential table randomly stored. 2.4Other Linked list2.4.1The single linked list described earlier in the doubly linked list allows direct access to its successor from one node, so the time complexity of finding a direct successor is O (1). However, to find the direct precursor node of a node, you can only start iterating through the nodes from the table's header reference. If Next of a node equals that node, then that node is the direct precursor to that node. In other words, the time complexity of finding the direct precursor node is O (n), and n is the length of the single linked list. Of course, we can also store the address of the direct predecessor node in the reference domain of the node instead of the address of the direct successor node. In this way, the time complexity of finding the direct precursor node is only O (1), but the time complexity of finding the direct successor node is O (n). If you want to find direct precursor nodes and direct successor nodes, the time complexity is O (1), then, you need to set two reference fields in the node, an address that holds the direct predecessor node, called Prev, the address of a direct successor, called Next, so that the linked list is a doubly linked list (doubly Linked list). Node structure of doubly linked list2.15is shown. Prev Data Next Diagram2.15Node structure of doubly linked list
A tong single-linked list problem
listnode* root = &listnode (0); ListNode* Curnode = root; for (int1; i++) { curnode->next = New ListNode (i); = Curnode->next; } struct ListNode { int val; *Next; ListNode (int x): Val (x), Next (NULL) {}};

2nd Linear table "C # Data structures and algorithms"

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.