Basic operation sequence table

Introduction: recently played esp8266 and ucos-iii ++ and more learning c, mostly pointer contact structures, linked lists; just look at himself in a data structure (data structure is really very important, must learn, is learning algorithm stepping stone, ha ha ha), personal views in order to add and delete elements table operation, efficiency is relatively low (you want to move a lot of other elements), and I wrote the list before the operation, using a pointer operation, efficiency is much higher a. Well, look at today's study summary of it!

First, the initialization sequence table:

Algorithm steps:

1, L sequence table dynamically allocated to a predetermined spatial array-defined size, so that the base address of this point elem space

2, the current length of the table is set to 0

Pseudocode:

InitList the Status (SqList & L) 
{ 
    // Constructs an empty list L of sequence 
   L.elem = new new elemType [MAXSIZE]; // allocate sequence table with a size of an array space is MAXSIZE 
   IF Exit (the OVERFLOW (L.elem!) ); // memory allocation failure exit 
   L.length = 0 ; // empty list of length 0 
  return the OK; 
}

Second, the value of the operation:

Algorithm steps:

1, the designated location is determined whether the value of a reasonable number i (1 <= i <= L.length), if unreasonable, ERROR is returned.

2, if the value of i is reasonable, then the i-th data element L.elem [i-1] is assigned to the parameter e, it returns the value of the i th transmission data elements by e

It is code for:

Status GetElem(SqList L ,int i,ElemType &e)
{
      if(i<1||i>L.length)   return EEROR;//判断i是否合理
     e=L.elem[i-1];
    return OK;
}

Third, the search operation:

Algorithm steps:

1, starting from the first element, and e are sequentially compared, and if found equal elements e L.elem [i], the search is successful, returns the element index i + 1;

2. If searching through the entire sequence table are not found, then the lookup fails, it returns 0

Pseudocode:

int LocateElem (SqList L, elemType e) 
{ 
     // find the data element value in the sequence table L e, returns the serial numbers 
    for (I = 0 ; I <L.length; I ++ ) 
    { 
       IF (L.elem [I ] == E)   return   I + . 1 ; 
      
    } 
   return  0 ; 

}

Fourth, the insert:

Algorithm steps:

1, the insertion position is determined whether valid I (i legal value range 1 <= i <= n + 1), if valid return ERROR

2, determine the order of the table storage space is full, if full, then it returns ERROR

3, the n th element to the i-th position is sequentially moved backward one position, the i-th position empty (i = n + 1 without movement)

4, to be inserted into the first new element i e position

5, plus a length of the table

Pseudocode:

ListInsert the Status (SqList L &, int i, elemType e) 
{ 
     // i-th position of the new element e inserted in the sequence table, the valid range of values is i =. 1 <i <= + 1'd L.length 
    IF ((i < . 1 ) || (I> + L.length . 1 ))     return ERROR;    // I invalid value 
    IF (== L.length the MAXSIZE)   return ERROR; // storage space is full 
    for (J = L.length- . 1 ; J> = I + . 1 ; J, )   
    { 
       L.elem [J + . 1 ] = L.elem [J]; // element after the insert position and moved back 
       L.elem [I- . 1 ] = E ; // the element e inserted into the first position i
       L.lenfth ++; // table plus a long 
       return the OK; 
     } 
}

Fifth, the delete operation:

Algorithm steps:

1, it is determined whether or not i is deleted method (legal values ​​1 <= i <= n), if not legal return ERROR

2, the i + 1-th to n-th elements are sequentially moved forward one position (i = do not move when n)

3, minus a length of the table

Pseudocode;

ListDelete the Status (SqList L &, int i) 
{ 
     // delete the i-th element in the sequence table, the valid range of values is i. 1 <= i <= L.length 
     IF ((i < . 1 ) || (the I > L.length)     return ERROR;
     for (I = J; J <L.length- . 1 , J ++ ) 
    { 
        L.elem [J - . 1 ] = L.elem [J]; // forward movement 
        - L. length;
         return the OK; 
     } 
}

VI Summary;

        Whether the order table or a single chain, the best way to learn is, in the understanding of basic concepts, it can be used immediately to combat them, so you can deepen your own understanding. Well, now is 00:49 in the morning, showered sleep, tomorrow to continue refueling.

 

Guess you like

Origin www.cnblogs.com/1121518wo/p/11415922.html
Recommended