Order table insert, delete algorithm using C language to achieve

#include<stdio.h>

#include<stdlib.h>

// dynamically allocated sequential storage structure -------- ----------- linear form

int LIST_INIT_SIZE = 100; // Initial sequence table storage size

int LISTINCREMENT = 10; // allocate sequence table storage increments

typedef int ElemType; // data type is an integer sequence table element

// definition of storage structure: the sequence table

typedef struct {

       ElemType * elem; // store linear array table element

       int length; // table length

       int listsize; // storage capacity

       int incrementsize; // increment extension

} SqList;

// -------- substantially linear operation table -----------------------------

// Auxiliary functions: display sequence table elements

void print_Sq (SqList L) {

       for(int i=0; i<L.length; i++)  printf("%5d", L.elem[i]);

       printf("\n");

}

@ 1 operation: initialization sequence empty list L

void InitList_Sq(SqList *L, int initsize, int incresize) {// 算法2.4

  L-> elem = (ElemType *) malloc (initsize * sizeof (ElemType)); // allocate space array

  L-> length = 0; // 0 initial value table length

  L-> listsize = initsize; // initial dimension tablespace

  L-> incrementsize = incresize;

}

// operation 2: The order of destruction algorithm 2.8 L // table

void DestroyList_Sq(SqList *L) {

  free (L-> elem); // table storage array released

       L-> elem = NULL; // pointer to an array of empty and the length of a table, indicating the size of the array variable

       L->length=0;

       L->listsize=0;

}

@ 3 operation: In order list L before inserting a new element e i-th position

void ListInsert_Sq (SqList *L,  int i,  ElemType  e) {

if(i < 1 || i > L->length+1)

        return ERROR; // i values ​​are not legitimate

       // dynamic expansion of space, every lack of space, the space length is increased LISTINCREMENT

    if(L->length == L->listsize)

    {

        ElemType * increspace = (ElemType *)realloc(L->elem,(L->listsize + LISTINCREMENT) * sizeof(ElemType));

        if(! increspace)

            exit(OVERFLOW);

        L->elem = increspace;

        L->listsize += LISTINCREMENT;

    }

       for(int j=L->length-1;j>i-1;j--)

       {

              L->elem[j+1]=L->elem[j];

       }

       L->elem[i-1]=e;

       L->length++;

       return OK;

}

@ Computation 4: Remove the i-th order element in the list L, the output variable returns its value e

void ListDelete_Sq (SqList *L,  int i,  ElemType*  e) { //算法2.7

       *e=L->elem[i-1];

       printf ( "% of element d is deleted:% d \ n", i, * e);

       for(int j=i-1;j<L->length;j++)

       {

              L->elem[j]=L->elem[j+1];

       }

       L->length--;

}

5 @ calculation: found value equal to the i-th element in the sequence table L e, the sequence returns to its position, otherwise 0

int LocateElem_Sq (SqList  L,  ElemType  e) { //算法2.5

for(int i=0;i<L.length;i++)

       {

              if(L.elem[i]==e)

                     return i+1;

       }

       return 0;

}

Guess you like

Origin www.cnblogs.com/11forevercute/p/11025825.html