[] Data structure of sequence table

 

 

1, a linear table definition

Linear table is a linear structure . Characteristics of linear structure is a linear relation, the data elements "arranged one after" between data elements. A linear type of data elements in the table is the same, or a linear structure is composed of a linear list of data elements of the same type. Examples of the linear form in a lot of practical problems, such as the case of student information table is a linear table: Type of the data element type students table; a string is a linear table: type table for the character data elements, and many more.
In summary, the linear form is defined as follows: a linear table having the same data type n (n> = 0) is a finite sequence of data elements , usually referred to as: (a1, a2, ... ai -1, ai, ai + 1, ... an)
where n is the length of the table, n = 0 sometimes referred to as an empty table. It exists order between adjacent elements in the table.

  • It referred to the presence of only the first data element
  • Exists only one is referred to the last data element
  • Except the first one, each data element in the set are only a precursor
  • Except the last addition, each data element in the collection are only a successor

 

2, the basic operation of the linear form

Computed data structure is defined in the logical structure level, the specific implementation calculation is based on the storage structure, so the basic operation of the linear form defined below as part of the logical structure of the specific implementation of each operation is only determined after completion of the storage structure to the linear form.

There is the basic operation on the linear form:

Initialization ⑴ linear form: Init_List (L)
Initial conditions: an empty linear configuration: Table L is present operation result

⑵ seek length linear form: Length_List (L)
Initial conditions: TABLE L exists
Result: returns the number of elements contained in the linear form

⑶ take bucket: Get_List (L, i)
the initial conditions: the presence of L and Table 1 <= i <= Length_List ( L)
Result: return address value or a linear list L i-th element

⑷ by value lookup: Locate_List (L, x), x is a given data element.
Initial conditions: the presence of a linear list L
Result: Find the value of x in the data element in the list L, which returns the result number or address of that element first appears is x in L called the search is successful; otherwise, x L data element value is not found, return a special value indicates lookup failed.

⑸ insert: Insert_List (L, i, x )
initial conditions: the presence of a linear list L, the correct position (1 <= i <= n + 1, n is the length of the table prior to insertion).
Result: inserting a new element is a linear list L x of the i-th position, so that the original serial number i, i + 1, ..., n number of data elements becomes i + 1 is, i +2, ..., n + 1, the original table into the table length = length + 1.

⑹ deletion: Delete_List (L, i)
the initial conditions: the presence of a linear list L, 1 <= i <= n .
Result: linear list L delete the corresponding data element i, so that the number is deleted i + 1, i + 2, ..., n is the number of elements becomes i, i + 1, ..., n-1, a new table length = length of the original table 1.


Note that:
the basic operation on a data structure 1, not its entire operation, but the basic operation is common, but each of the basic operations when implemented may derive a series of related structures according to different storage the operations to. Such as linear lookup table in the chain will have to find storage structure by serial number; Again insertion operation, the new element may be inserted into position x and the like, all possible nor necessary to define its operation set after reader control basic operations on certain data structures, other operations can be accomplished by basic operations, or directly to achieve.
2. The definition of each operation in the above is just a linear list L abstract logical hierarchy structure of linear form, it has not been related to the storage structure, so that each operation can not hierarchy logical structure of certain specific programming language write specific algorithm, and the algorithm achieved only after the establishment of the storage structure.

 

 

3 and sequentially stores the calculation, the linear form of - sequence table

Sequential storage means is a linear table in memory with the address of each element of a continuous linear memory space to store the order table, this table is stored in the form of a linear stored call sequence table. Because the memory address space is linear, thus implementing logical relationships between data elements is a simple, natural and adjacent with adjacent physical. Figure 2.1.
Memory address a1 is set Loc (a1), each data element d representing storage locations, the address of the i-th data element is:

Loc(ai)=Loc(a1)+(i-1)*d 1<=i<=n

This means that as long as the known sequence table start address and end address of each data element is the number of occupied cells can find the address of the i-th data elements, the sequence table having a characteristic which is the number of data elements by random access.

In programming languages, one-dimensional arrays in memory occupied storage space is a set of contiguous memory area, therefore, be represented by one-dimensional array data storage area sequence table is an ideal fit. Considering the operation of the linear form insertion, deletion operation, i.e., the length of the table variable, therefore, the need to design the capacity of the array is large enough, provided with: data [MAXSIZE] is represented, wherein MAXSIZE is defined according to a practical problem integer large enough, a linear table data [0] data from the stored sequential order, but the actual number of elements in current linear table may not reach more than MAXSIZE, so a required current linear variable last record of the last table position of one element in the array, i.e., a pointer from the last effect, always pointing to the last element of a linear table, therefore, when the table empty last = -1. Detailed description of such storage ideas can be diverse. As can be:

datatype data[MAXSIZE];
int last;

Such a table indicates the order shown in Figure 2.1. Table length last + 1, the data elements are stored in the data [0] to data [last] in. So easy to use, but sometimes inconvenient management.

to sum up:

Characterized in that the sequence table, occupying contiguous memory locations in the memory, can be simply understood as a sequential list is expanded array.

Only required dynamic allocation of memory occupied by the sequence table means in practical applications.

And the array is at compile time, pre-allocated memory unit specifies the size, and therefore will be given in the following code segment compile time.

Int len = 10 ;
Four Arr [Lane];

The order of the table and have all the characteristics of the data: The index can be accessed directly, convenient insert and delete elements (since the mobile require subsequent elements).

 

 

 4, and the table would be (C language)

Definition structure

typedef int SeqType; // storage unit type 

typedef struct { 
SeqType * elem; // memory base address 
int length; // current length 
int listsize; // storage capacity currently allocated (in sizeof (ElemType) units) 
} SqList ;

// structures in the body, there are three elements: base memory address space, similar to the first address of the array; current length, the number of records stored in the active cell sequence table; currently allocated storage capacity, the sequential list, up to the number of memory cells contained . When the sequential list all memory cells have been used, prior to insertion of the next element, it is necessary to add the storage unit. This is an array do not have the characteristics.

* Note: The definition of a type storage unit SeqTypeis provided to enable proper time sequence table and more data types, the use of modified SeqTypetypes can.

Create a sequence table

/**
 * 创建顺序表
 */
SqList createList_sq() {
    //SqList list;
    //return list;

    SqList* list = (SqList*)malloc(sizeof(SqList));
    return *list;
}

// Here are two ways to create the code sequence table, one is occupied by the system memory allocation list, one is its own dynamically allocated memory, you need to manually run the program before the release of the memory space occupied.

Initialization sequence table

/ * * 
 * Table initialization sequence 
 * Returns 1 indicates successful initialization 
 * returns 0 for initialization fails 
 * / 
int initList_sq (SqList & L) { // only in C ++ reference present will have 
    L.elem = (SeqType *) the malloc ( the sizeof (SeqType) * LIST_INIT_SIZE);
     IF (! L.elem)
         return  0 ; // memory allocation fails, the storage space is insufficient 
    L.length = 0 ; // a sequence table is empty 
    L.listsize = LIST_INIT_SIZE; // a sequence table, the maximum number of memory cells 
    return  . 1 ; 
}

, Attribute value of the initialization sequence table storage unit // allocation sequence table.

Insert elements

/ * * 
 * Insert sequence tables 
 * subscript is inserted into the end of the negative 
 * / 
int insertList_sq (SqList L &, int index, SeqType Val) {
     IF (index> L.length) { // stored in the table exceeds the actual sequence table the length of 
        the printf ( " insert index beyond the actual length of the sequence table " );
         return  0 ; 
    } 
    IF (index < 0 ) // index is negative, is inserted into the end 
        index = L.length;
     IF (= L.length L.listsize =) { // sequence table storage unit is full have 
        the printf ( " memory cell of the sequence table is full, continue to allocate a new storage unit. " );
        SeqTypeNewBase = * (SeqType *) realloc (L.elem, 
                (L.listsize + LISTINCREMENT) * the sizeof (SeqType)); // continue allocating storage unit 
        IF (! NewBase) { 
            the printf ( " allocate memory failure " );
             return  0 ; 
        } 
        L.elem = newBase; 
        L.listsize + = LISTINCREMENT; 
    } 
    // find the proper insertion position, rearward movement of the back of the index element 
    for ( int I = L.length; I> index; i-- ) { 
        L.elem [I] = L.elem [I - . 1]; // move rearwardly 
    } 
    L.elem [index] = Val; // insert elements 
    L.length ++ ;
     return  . 1 ; 
}

// insert elements into the specified location. Prior to insertion, it is necessary to judge whether the sequential list has been full, and then add the storage unit as needed, the last inserted element.

Removing elements

/ * * 
 * Delete the specified element 
 * it Returns the specified element 0 can not find, delete failed. 
 * Returns 1 find the elements to be deleted, deleted successfully. 
 * / 
Int removeList_sq (SqList & L, SeqType Val) {
     int index = - . 1 ; // record matching the subscript 
    for ( int I = 0 ; I <L.length; I ++ ) {
         IF (L.elem [I ] == Val) {
             // find the matching val, ends the loop 
            index = I;
             BREAK ; 
        } 
    } 
    IF (index < 0 )
         return  0 ;
     for (; index < L.length - 1; index++) {
        L.elem[index] = L.elem[index + 1];
    }
    L.length--;
    return 1;
}

// delete the specified elements, you need to find the next mark. Behind the mobile node sequentially index, modifying the value of length.

/ * * 
 * The subscript specified node is deleted, and returns the value of the element 
 * subscript 0 returns the table length exceeds the sequence, deletion fails. 
 * Returns 1 subscript correct, remove elements, and transferred to the deleted element value elem 
 * / 
int removeList_sq (SqList L &, int index, SeqType & elem) {
     IF (index> = L.length) // subscript out of order the length of the table 
        return  0 ; 
    index = index < 0 L.length: index;? // subscript indicates remove the last node negative 
    elem = L.elem [index];
     for ( int I = index; I <L.length - . 1 ; I ++ ) { 
        L.elem [I] = L.elem [I + . 1 ]; 
    }
    L.length--;
    return 1;
}

// get the first element to the specified target, assigned to elem, then the mobile node sequentially behind subscript. Last Modified length value.

The order of destruction Table

/ * * 
 * Destruction order table 
 * / 
void destoryList_sq (SqList & L) {
     Free (L.elem); // free memory 
    L.length = 0 ; 
    L.listsize = 0 ;
 //   Free (& L); 
}

// Key release sequence table storage unit. If the order table itself is dynamic memory allocation, you need to manually released.

Header file defines

/ * 
 * Sqlist.h 
 * linear sequence storage table 
 * / 

#ifndef SQLIST_H_ 
#define SQLIST_H_ #define LIST_INIT_SIZE 50
 #define LISTINCREMENT 10 
typedef int SeqType; // storage unit type 
typedef struct { 
    SeqType * elem; // memory address space group int length; // current length int listsize; // storage capacity currently allocated (in sizeof (elemType) units) } SqList; / * * 
 * Create order table * / 
SqList createList_sq (); / * * 
 * initialization sequence table * /




    
    



 


 
int initList_sq (SqList & ); 

/ * * 
 * insert sequence table 
 * / 
int insertList_sq (SqList &, int index, SeqType); 

/ * * 
 * insert sequence table (position end) 
 * / 
int insertList_sq (SqList & , SeqType) ; 

/ * * 
 * position of the element in order to remove the specified table index from 0 
 * / 
int removeList_sq (SqList &, int , & SeqType ); 

/ * * 
 * in order to delete the specified table of elements 
 * / 
int removeList_sq ( & SqList , SeqType);
 / * * 
 * destruction order table 
 * / 
void destoryList_sq (SqList & );

#endif /* SQLIST_H_ */

 

Guess you like

Origin www.cnblogs.com/-wenli/p/11228876.html