Data Structures and Algorithms - linear table

1. Concept

Linear table can be seen as an abstract concept, it can be used as an abstract data type, a linear table is a collection of certain elements is also recorded in a sequential relationships between elements. It corresponds to an abstract class, only the definition.

 

2. realization

1. order table

Order of the table basically is very simple: the order of elements in the table is stored in a large enough contiguous storage sections, the first element into the start position of the storage area, the remaining elements stored in sequential order, logical relationship between the storage element by element represented physical location zone (represented by an implicit relationship between elements)

Sequence table layout in memory:

1. The realization of the basic operation order table

 

 

  1. Create and access operations

When creating an empty table, need to allocate a storage element, the recording capacity of the table, and element count is set to 0, the complexity is O (1)

  1. Simple determination operation

 

 

Analyzing the table is full or empty table is easily accomplished, both the complexity of O (1)

  1. Access to set the standard elements of i

It does not depend on the number of elements in the table, and therefore O (1) operations

  1. Traversal operation

Sequential access to elements in the table, during traversal traversal reaches the recording position, and then calculates the position of the element, the element can be obtained. Obtaining each element of complexity is O (1), to traverse the entire table so complexity is O (n).

  1. Find the position of a given element d

This operation is called retrieval or lookup, using traversal operation sequence comparison, the time complexity of O (n).

  1. Find a given element d position after position k first appears

5 with the same, except that after traversing from the position k.

  1. Adding elements

Added at the end of the table and delete are simple, time complexity is O (1). Add and remove other positions will trouble some required data operation back movement position, moving forward or backward, the time complexity is O (len-i), i is the operative position.

  1. Removing elements

Remove the trailing end of the element is simple, time complexity is O (1), the general position of the deleted O (n), based on the delete O (n) conditions.

to sum up:

Advantages: Access by location O (1) time, compact storage elements in the table, in addition to table elements, only O (1) space to store a small amount of auxiliary information.

Disadvantages: the continuous storage region storing elements in the table, if the table is large, requires a large contiguous memory space, once the size of the storage block does not fluctuates as insertion and deletion of data, there will be plenty of free units exist, resulting in waste.

 

2. The structure of sequence table

Two basic implementation

    1. Unitary structure
    2. Separate structure

 

  1. Integral analysis

Achieve more compact, relevant information together, strong integrity, easy to manage.

After you create a fixed size storage area

  1. Separate analysis

Table only stores the information related to the entire table, the actual elements placed in another storage area separate elements of objects, through links to relevant even basic table, such a table object size uniform, but a table requires two separate objects implementation, creation and management complexity. The biggest advantage is achieved separate brought a new possible, in the case where the same identifier, an element for changing the object storage area, that is to say the capacity of the table can be changed.

      • Apply for a larger storage area
      • Copy the table in existing element to the new storage area
      • Replace the original elements of the storage area with the new elements of the storage area
      • The actual adding new elements

In a time when the expansion if the 10 elements expand storage location, then large amounts of data inserted, replacing the need to keep the storage position, the elements frequently copied migration. If every time the expansion of the current storage doubled when large data time, will cause a lot of wasted storage space. So when using expansion strategy also needs to weigh.

 

3.python the list

Basically

    1. Based subscript efficient access and update
    2. Allow any added elements, but at the same table to join the process id

solution

    1. Since the element need O (1) time to access, and to maintain the order of the elements, this table technique only continuous sheet
    2. Required to accommodate any number of elements, it must be able to replace the element storage region, so only the use of separate technology.

The actual strategy

    1. When establishing a small empty table or a table, the system allocates a storage area capable of accommodating 8 elements, if they change over 4 times as large as a storage area
    2. If the capacity of the table when 50,000, change double the capacity storage area

 

4. A brief summary of the sequence table

  1. The most important feature is the positioning element O (1) time to access, update. Many simple operation efficiency is relatively high.
  2. The most troublesome problem of efficiency is to add, delete and other operations
  3. Require continuous storage space
  4. The structure is not flexible enough

 

2. Link Tables

    1. Single list

In such a structure, in order to grasp a table, just use a variable that holds a reference to the first node table

    • Consists of a single linked list table specific node
    • Each node is an object that has its own identity, also known as the node link
    • Contact between nodes established by the order of one-way links
    • None the end of the list only one in the field is set to link the last node.
2.1.1. Basic Operations
    • Create an empty list: just put the appropriate header variable is set to an empty link.
    • Delete list: Discard all the nodes in the list, python in the table just to correct assignment to None, python interpreter will automatically reclaim unused storage.
    • Determining whether empty: comparing the header value of the variable value and air links
    • To determine whether full: general list will not be filled until the program runs out of storage space for all
    • The first end is inserted:
      • Create a new node and stored data
      • The original link list head node links into new fields next node
      • Header variables modified to point to the new node
    • The general case is inserted
      • Locate the insert position
      • Implementation of the first end of the three-step operation
    • Delete the first element of the table: just modify the header pointer, so that it points to the second node
    • Delete under normal circumstances: find the location of a node is located before the element, next fields later renamed the elements of a node link
    • Scan, locate and traverse: Since the list is only a single direction of the link, the link is only at the beginning of the header in his fists, so everything is checked in the table of contents can only start from the head table, links gradually along the table, the process called scanning.
      • Press the standard orientation
      • By positioning elements

 

    • Operational complexity
      • Create an empty table: O (1)
      • Delete the table: the python is in O (1)
      • Analyzing empty: O (1)
      • Adding or deleting elements:
      • The first end added: O (1)
      • Tail was added: O (n)
      • Join or: O (n), the average and worst cases are
      • Request table length: the need to scan the entire, length of the draw, the length may be recorded as a data component table, O (1)

 

Guess you like

Origin www.cnblogs.com/jiaojianglong/p/11260919.html