Table 2 Data Structure linear insertion and deletion algorithm ----

Also said before the data structure is a military adviser level of things, so the first element index is starting from 1!

Today we talk about the insertion and deletion algorithms sequence table ;

/ * Initial conditions: linear sequence table exists L, 1 <= i <= ListLength (L). * / 
/ * Result: inserting a new data element e, L the length before the i + 1 position in L. * / 

The Status ListInsert (SqList * L, int I, elemType E) 
{ 
    int K; 

    IF (L-> == length the MAXSIZE)   // order linear table becomes full 
    {
         return ERROR; 
    } 
    IF (I < . 1 || I > L-> length + . 1 )    // when i is not within the range 
    {
         return ERROR; 
    } 
    IF (i <= L-> length)    // if the data insertion position is not the end of the table 
    {
         / *After the data elements to be inserted is moved backward one position * / 
        for (K = L-> Length- . 1 ; K> = I- . 1 ; K-- ) 
        { 
            L -> Data [K + . 1 ] = L-> Data [ K]; 
        } 
    } 

    L -> Data [I- . 1 ] = E;   // new element into 
    L-> length ++ ; 

    return the OK; 
}

  In fact, many beginners if a direct copy this code is not running, but because of my personal time constraints, I will not write a complete program code, which if Meng want to know all the new program code below, please leave a message, thank you!

      In fact, this algorithm at a glance, and I was thinking we talk about algorithms, as an array of length 50, the current length of 7, so writing insertion algorithm time to pay attention inserted position must be within the scope of the current length, so have a bit of statement

if (i <1 || i> L-> length + 1) // when i is not within the range 
    { 
        return ERROR; 
    }

The second point, for example, you want to insert in the fifth position data, we need to data [5] to change the value of data to be inserted, and the original data [5] and after the element after him step back.

After / * insert location data element is to be moved backward a * / 
        for (= L-K>. 1-length; K> =. 1-I; K--) 
        { 
            L-> Data [K +. 1] = L- > Data [K]; 
        }

The third point, to distinguish the concept of algorithms i starts at 1, but the writing program subscript c is zero, or say that in fact the order of the first element in the table is in the c language

0th element, so you can order unity in c language represented as data [1-1]; i-th element is a generalized representation of [i-1]

L-> data [i-1] = e; // the new element is inserted into

 

2. Delete algorithm

/ * Initial conditions: order linear form L exists,. 1 <= i <= ListLength (L) * / 
/ * Result: Delete L i-th data element, and returns its value E, L the length - 1 * / 
the Status ListDelete (SqList * L, int I, elemType * E) 
{ 
    int K; 

    IF (L-> length == 0 ) 
    { 
        return ERROR; 
    } 
    IF (I < . 1 || I> L-> length) 
    { 
        return ERROR; 
    }

     * L-E => Data [I- . 1 ]; 

    IF (I <L-> length) 
    { 
        for (I = K; K <L-> length; ++ K ) 
        {
            L->data[k-1] = L->data[k];
        }
    }

    L->length--;

    return OK;
}

Finished after insertion algorithm, deletion algorithms we all try to understand to understand, there are comments below question.

The next to speak a single list, thank you!

Guess you like

Origin www.cnblogs.com/zulkar/p/10973163.html
Recommended