C # Data Structure linear table (order table IList Array)

Source: Internet
Author: User

Linear table

Linear tables are the simplest, most basic, and most commonly used data structure. Data element 1 is a relationship to 1, which is a positional relationship.

Characteristics

(1) There are no data elements before and after the first element and the last element, the remaining elements in the linear table are neighbors, and there are elements before and after them.

(2) The elements in the linear table are finite (List), and the data types in the linear table are the same.

(3) Linear table representation method l={a1,a2,a3,a4.......an},l= (D,r)

(4) Each element has a precursor and a successor, the first element only the successor, the last element only the precursor.

Instance

For example: An integer of 1~100 is a linear table

{"Zhangsan", "Lisi", "Wangwu"}

......

A. Sequential table

All operations are based on a linear structure based on which the linear table interface is represented as follows:

public interface ilistds<t>

{

int GetLength (); Request length

void clear ();//Empty Linear table

BOOL IsEmpty ();//Determine if empty

void Append (T item);//Append element

void Insert (T item,int i);//Insert Element

T Delete (int i);//delete element

T Getelem (int i);//Get element

int Locate (T value);//Find Element

void Reverse ();//Inverted

}

Operation

1. Insert operation:

30 before inserting the data table does not change, when the 30 data into the first position, the back of the data need to move backwards, then the inserted

The higher the data, the more times you need to move, and if i=0 needs to move backwards n positions, if it is inserted into the last, then

The order table does not need to move, if the position I of the inserted data is pi, then the number of positions that need to be moved is N/2, moving in the order table

Half of the data, the time complexity of the insert operation is 0 (n).

2. delete operation: Sequential table delete operation is consistent with insert operation, time is wasted on mobile data, time complexity is 0 (n)

3. read operations: Read directly into the order table values such as ilistds[i] element, then the time complexity of 0 (1) only need to take once.

4. Find operation: Find the data location I need to find, and then determine whether the value is consistent, this is similar to the insertion and deletion, time complexity: 0 (N)

5. Inverted operation: Inverted operation only need to compare the first and second elements and the n-i elements, the time complexity is also: 0 (n)

B. Single linked list

Compared to the sequential table, the logical same data is also the same on the physical address, it is very convenient to find the data in a position on the sequential table, which is the

Advantage, then the problem comes, in the sequential table when inserting or deleting a data often need to move the rest of the data, then this will affect the efficiency

, Next, learn about another storage structure for the linear table----chained storage (Linked strorage), a linear table called (Linked

list), the operation of the single-linked list does not need to move other data elements, but it also loses The advantages of the go-to-order table can be stored randomly.

From the two graphs we can see a difference in the structure and order table of a single linked list

A single-linked list implementation method:

Pulic class Linkedlist<t>:ilistds<t>

{

Pulic node<t> head;//Properties

Pulic LinkedList ();//constructor

Pulic getlength ();//Get length

Pulic clear ();//Empty

pulic bool IsEmpty ();//Determine if it is empty

pulic void Append (T item);//Append

pulic void Insert (T item,int i);//i position Insertion

pulic void Delete (T item);//delete element

Pulic T Getelem (int i);//Get single-linked list I elements

pulic int i location (T item);//Find position

pulic void Reserver ();//Inverted

}

Operation

1. Get the length

The length of the single-linked list and the sequential table is inconsistent in the past because the single-linked list is a sequential space, and the single-linked list

Starting from the beginning of the reference header, a node is convenient for a node until the end of the table. So here's the problem: just introducing the single-linked list physical space is not

even do you want to continue? This author personally understands that the initialization data is physically contiguous, and the newly inserted data is not arranged according to the physical address, so the single chain

Table is also to facilitate the entire content to calculate the length, time complexity of 0 (n).

2. Clear Operation

A purge operation means that all nodes of a single-linked list make the single-linked list empty, at which point the header refers to the head as null. The algorithm for emptying a single-linked list is as follows:

pulic void Clear ()

{

Head=null;

}

It is important to note that once the single-linked list is emptied, the space occupied by the original node is not retained, and the garbage collector recycles and the order table is not the same

Is that the sequential table is contiguous space, and the space allocated by the array is still preserved.

3. Additional Operations

The additional operation is to facilitate all elements in a single linked list, and then add elements at the end of the list, with a time complexity of: 0 (N);

There are advantages and disadvantages of sequential storage and chain storage for linear tables, and how linear tables are stored depends on where they are used. If you do not need to do it frequently in a linear table

Insert and delete, only is to do a lookup, then the linear table should be stored sequentially, and if the linear table needs to be inserted and deleted frequently rather than being frequently searched

, the linear table should chain-store.

C combined with one-way linked list, there must also be a two-way list, the two-way list is also the basic operation above, and then think about the related issues: time complexity problem.

D Loop list: The circular link table is connected to the top end of a single linked list and a doubly linked list Last.next=>header.head

Linear tables in C #

Said C # Linear table that is the list, in 1.1 provides a non-generic interface IList, the interface is an object, non-generic IList is from the ICollection

Interface inherited, is the interface of all linear tables, it took so long list to find that IList is a linear structure, IList is divided into three categories: read-only, large

Small immutable, variable in size.

read-only IList: cannot be modified, inserted, or deleted.

IList with constant size: cannot be inserted or deleted in the table, but items in the table can be modified.

Variable size ilist: can be manipulated, can be inserted or deleted

The non-generic IList interface declaration is as follows:

Interface ilist:icollenciton,ienumberable

{

Common attributes

bool Isfiexedsize{get;}//Read only if IList has a fixed size

bool Isreadonly{get;}//Read only if IList is read-only

Object This[t index] {get;set;}//indexer gets a type

int Add (object value);

void Clear ();

int IndexOf (object value);

BOOL contains (ojbject value);

void Insert (index,object value);

void Remove ();

void RemoveAt ();

}

Some collections in the. NET framework implement the IList interface, such as Arraylist,listdictionary,stringcollection,string Dictionary.

In. NET linear tables, sequential storage takes an array, and chained storage is: IList interface.

To be continued .....

C # Data Structure linear table (order table IList Array)

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.