C language - linear table

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LIST_SIZE 100
#define LIST_INCREMENT 10
typedef int Datatype ;
typedef struct{
    Datatype* data;
    int length;
    int listsize;
}seqlist;
/ * InitList (& L)
Initial conditions: None
Result: linear configuration of an empty table. Error successful return 0 * -1 /
int InitList(seqlist *L)
{
    L->data = (Datatype *)malloc(sizeof(Datatype)*LIST_SIZE);
    if (L->data == NULL)
        return -1;
    L->length = 0;
    L->listsize = LIST_SIZE;
    printf("initial finish\n");
    return 0;
}
/ * DestroyList (& L)
Initial conditions: linear form L already exists
Result: Destruction linear form L. Error successful return 0 * -1 /
int DestroyList(seqlist *L)
{
    while (!L->data)
    free(L->data);
    L->length = 0;
    printf("destroy finish\n");
    return 0;
}
/ * ListEmpty (L)
Initial conditions: linear form L already exists
Result: if the list L is empty, returns 0, otherwise -1 * /
int ListEmpty(seqlist L)
{
    if (L.length == 0)
    {
        printf("list is empty\n");
        return 0;
    }
    else
        return -1;
}
/ * ListLength (L)
Initial conditions: linear form L already exists
Result: Returns the length of the table, otherwise return -1 * /

int ListLength(seqlist L)
{
    return L.length;
}
/ * Getelem (L, i, & e)
Initial conditions: linear form L already present 1 = <i <= LIST_SIZE
Result: Returns the value L i-th element by e successful return 0 error return -1 * /
int getElem (seqlist L, I int, the Datatype * E)
{
    IF (I <0 || I> LIST_SIZE)
    {
        the printf ( "position error \ n-");
        return -1;
    }
    * E = L.data [I];
    return 0;
}
/ * Locateelem (L, e)
Initial conditions: linear form L already exists
Result: The return L e equal to the first sequence number and, if the element does not exist, then -1 * / return
int Locateelem (seqlist L, E DataType)
{
    int i; // traverse the entire list, if not found until the end of the return -1
    for (I = 0; I <L.length; I ++)
    {
        IF (L.data [I] == E)
            return I +. 1;
    }
    -1 return;
}
/ Priorelem * (L, cur_e, & pre_e)
initial conditions: linear form already exists
result: if the elements cur_e is L, and is not the first, then use its predecessor elements pre_e returned, otherwise error -1 returns success * 0 /
int Priorelem (seqlist L, the Datatype cur_e, the Datatype pre_e *)
{
    int I = 0;
    IF (cur_e == L.data [0])
        return -1;
    I = Locateelem (L, cur_e);
    IF (I == -1)
        return -1;
    the else
    {
        * pre_e L.data = [I - 2];
        return 0;
    }
}
/ * Nextelem (L, cur_e, & next_e)
Initial conditions: linear table already exists
Result: If cur_e is L the elements, and not the last one, with next_e returns its successor element, otherwise an error -1 successful Back * 0 /
int Nextelem (seqlist L, the Datatype cur_e, the Datatype next_e *)
{
    int I = 0;
    IF (cur_e == L.data [ListLength (L) -1])
        return -1;
    I = Locateelem (L, cur_e);
    IF (I == -1)
        return -1;
    the else
    {
        * next_e = L.data [I];
        return 0;
    }
}
/ * ListInsert (& L, i, E)
Initial conditions: linear form L already exists
Result: inserting L i th position before a new data table length plus 1, return 0 on success, -1 error * /
int ListInsert (seqlist L *, I int, the Datatype E)
{
    IF (I <. 1 || I> L-> listsize)
    {
        the printf ( "position error \ n-");
        return -1;
    }
    IF (L-> length> L-=> listsize)
        L-> Data = (the Datatype *) realloc (L-> Data, (+ LIST_INCREMENT LIST_SIZE) * the sizeof (the Datatype));
    ! IF (L-> Data)
    {
        the printf ( "realloc error \ n- ");
        return -1;
    }
    the Datatype Q * = NULL;
    Q & L-=> Data [I -. 1]; // inserted position
    Datatype * p = NULL;
    for (p = & L-> data [L-> length - 1]; p> = q; p -) // this order from the last position in front of the value assigned to a rear position
    {
        * (P +. 1) * P =;
    }
    * Q = E;
    L-> length ++;
    L-> listsize + = LIST_INCREMENT;
    return 0;

}
/ * Listdelete (& L, i, & E)
Initial conditions: the presence of a linear list L
Result: delete the serial number L of the element i, and the value E by the back, successful return 0, -1 error * /
int Listdelete(seqlist *L, int i, Datatype *e)
{
    if (i<1 || i>L->listsize)
    {
        printf("postion error:\n");
        return -1;
    }
    Datatype *q = NULL;
    Datatype *p = NULL;
    q = &L->data[i - 1];//删除元素位置
    *e = *q;
    p = &L->data[L->length - 1];//表尾位置
    while (q <= p)
    {
        *q = *(q + 1);
        q++;
    }
    L->length--;
    return 0;
}
int main(void)
{
    seqlist L;
    InitList(&L);
    int i,k=0;
    Datatype e;
    Datatype ee;
    Datatype eee;
    Datatype eeee;
    for (i = 1; i <=LIST_SIZE+1; i++)
    {
        if (!ListInsert(&L, i, i*i))
            printf("第%d个数%d插入列表\n",i, L.data[i-1]);
    }
    printf("the length of list is  %d\n", ListLength(L));

    K =. 5;
    getElem (L, K-. 1, & E);
    the printf ( "% d number d is% d \ n-", K, E);
    IF (ListEmpty (L))
        the printf ( "List IS Not empty \ n-");
    the printf (" number% d is% d \ n-", 25, Locateelem (L, 25));
    Priorelem (L,. 4, & EE);
    the printf ("% previous element of d yes yes% d \ n-",. 4, EE);
    Nextelem (L, 256, & Eee);
    the printf ("% after an element of d yes yes% d \ n-", 256, Eee);
    Listdelete (& L,. 1, & EEEE);
    the printf ( "delete element is D% \ n-', EEEE);
    the printf (" The length of List D iS% \ n-', ListLength (L));
    DestroyList (& L);
    getchar ();
    return 0;
}

Guess you like

Origin www.cnblogs.com/wbwhy/p/11904201.html