C # data structure linear table (sequence table linked list IList array ),

Source: Internet
Author: User

C # data structure linear table (sequence table linked list IList array ),

 

 

Linear table

 

 

A linear table is the simplest, most basic, and most commonly used data structure. 1-to-1 Relationship of data elements, which is a positional relationship.

 

 

Features

 

 

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

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

(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 has a successor, and the last element only has a precursor.

 

 

Instance

 

 

Example: 1 ~ The integer 100 is a linear table.

{"Zhangsan", "lisi", "wangwu "}

......

 

 

A. Sequence Table

 

 

All operations are performed based on the linear structure. The linear table interface is shown as follows:

 

 

 

 

Public interface IListDS <T>

{

Int GetLength (); // evaluate the length

Void Clear (); // clears the linear table.

Bool IsEmpty (); // determines whether it is null.

Void Append (T item); // Append Element

Void Insert (T item, int I); // Insert an element

T Delete (int I); // Delete an element

T GetElem (int I); // gets the element

Int Locate (T value); // search for elements

Void Reverse (); // Invert

}

 

 

 

 

 

Operation

 

 

1.Insert operation:

 

 

 

 

30. The data table remains unchanged before data is inserted. When the 30 data enters and exits to the position I, the following data needs to be moved backward.

The higher the data, the more times the data needs to be moved. If I = 0, n positions need to be moved backward. If it is inserted to the last one

The sequence table does not need to be moved. If the probability of the inserted data location I is Pi, the number of locations to be moved is n/2.

If half of the data is inserted, the time complexity of the insert operation is 0 (n ).

 

 

 

2.Delete operation: the delete operation of the sequence table is the same as the insert operation. The time is wasted on moving data, and the time complexity is also 0 (n)

 

 

3.Read operation: directly read the values in the sequence table, such as the IListDS [I] element. The time complexity is 0 (1) And you only need to take one operation.

 

 

4.Search operation: Find the data location I, and then determine whether the values are consistent. This is similar to insert and delete. Time Complexity: 0 (n)

 

 

5. Inverted operation: you only need to swap the I and I elements with the n-I elements before and after the inverted operation. The time complexity is also: 0 (n)

 

 

 

 

B. Single-linked list

 

 

Compared with the sequence table, the data with the same logic is also the same on the physical address. It is very convenient to find the data at a location in the sequence table. This is the sequence table's

The advantage is that when you insert or delete a data in a sequence table, you need to move the remaining data, which will affect the efficiency.

Next, let's take a look at another storage structure of a linear table-Linked Strorage.

List), operations on a single-chain table do not need to move other data elements, but also lose the advantages of sequential tables that can be stored randomly.

 

 

 

 

From the two figures, we can see a difference between the structure of a single-chain table and the sequence table.

Single-chain table implementation method:

 

 

 

 

Pulic class Sequence List <T>: IListDS <T>

{

Pulic Node <T> Head; // attribute

Pulic shortlist (); // Constructor

Pulic GetLength (); // get the length

Pulic Clear (); // Clear

Pulic bool IsEmpty (); // determines whether it is null.

Pulic void Append (T item); // Append

Pulic void Insert (T item, int I); // Insert at the I position

Pulic void Delete (T item); // Delete an element

Pulic T GetElem (int I); // obtain the I-th element of a single-chain table

Pulic int I Location (T item); // locate

Pulic void Reserver (); // Invert

}

 

 

 

Operation

 

 

1.Get Length

 

 

The length of a single-chain table is different from that of a sequence table in the past, because a single-chain table is a continuous sequence space, while a single-chain table

Starting from the beginning, one node and one node are convenient until the end of the table. The problem is: the physical space of a single-chain table is not the same.

Is it continuous? I personally understand that the initial data is physically continuous, and the newly inserted data is not arranged according to the physical address, so

The table still needs to facilitate the entire content to calculate the length. The time complexity is 0 (n ).

 

 

 

2.Clear operation

 

 

To clear a single-chain table, all nodes in the single-chain table are empty. The header reference is null. The algorithm for clearing a single-chain table is as follows:

 

 

 

Pulic void Clear ()

{

head=null;

}

 

 

 

Note that after a single-chain table is cleared, the space occupied by the original node is not retained, but the Garbage Collector recycles the table differently from the sequence table.

The sequential table is a continuous space, and the space allocated by the array is retained.

 

 

 

3.Additional operations

 

 

The additional operation is to facilitate all the elements in the single-link table, and then add the elements at the end of the linked list. The time complexity is: 0 (n );

The sequential storage and chain storage of linear tables have their own advantages and disadvantages. The storage of linear tables depends on the application scenario. If you do not need to do this frequently in a linear table

Insert and delete, only for search, then the linear table should be stored in sequence; if the linear table needs to be inserted and deleted frequently, not frequently

, The linear table should be stored in a chain.

 

 

 

 

 

C Combined with a one-way linked list, there must also be a two-way linked list. The two-way linked list also has the above basic operations, and then think about the related problems: time complexity.

 

D. Cyclic list: the cyclic list is connected at the beginning and end of a single-chain table and a two-way linked list. Last. Next => Header. Head

 

 

 

 

Linear table in C #

 

 

The C # linear table is the List. In 1.1, the non-generic interface IList is provided. The item in the interface is object, and the non-generic IList is from ICollection.

The interface is inherited from all linear table interfaces. After using this long List, we can find that IList is a linear structure. IList is divided into three types: Read-only, large

Small immutable, variable size.

 

 

Read-Only IList:Cannot be modified, inserted, or deleted.

 

IList with the same size:You cannot insert or delete a table, but you can modify the items in the table.

 

Variable-size ILIST:Yes, you can insert or delete

 

 

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;} // The indexer obtains 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, and String Dictionary.

 

 

 

In. NET linear tables, sequential Storage Uses arrays, while chained storage uses the IList interface.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

To be continued .......

 

 

 

 

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.