【C语言数据结构】线性表二:线性表的顺序表示和实现(代码)

顺序表的概念/数据结构部分请见上一节,这里直接贴出书上的算法和代码

需要注意的是这里的realloc函数的用法,参考MAN手册就好

另外接口只保证了编译通过,请自行写测试用例测试

#ifndef __HEADER_H__
#define __HEADER_H__ 100
#include 
#include 
#include 
#include 
#include 
#include 

#define DEBUG 100
#ifdef DEBUG
#define PRINT_INFO(format,...) printf(format,##__VA_ARGS__)
#else
#define PRINT_INFO(format,...)
#endif

#define PRINT_ERR(format,args...) printf(format,##args)
#define SUCCESS 0
#define FAILURE -1

/*type define*/
typedef struct
{
    int* elem;
    int length;
    int listsize;
} sqlist;

/*func declaration*/
void print_sqlist(sqlist* L);//print
int init_sqlist(sqlist* L);//init
int insert_sqlist(sqlist* L, int pos, int value);//insert
int delete_sqlist(sqlist* L, int pos);//delete elem
void destroy_sqlist(sqlist* L);//destroy
#endif /*__HEADER_H__*/

【C语言数据结构】线性表二:线性表的顺序表示和实现(代码)_第1张图片

int init_sqlist(sqlist* L)
{
    /*CHECKPOINTER*/
    assert(L != NULL);

    L->elem = (int*)malloc(LIST_INIT_SIZE * sizeof(int));
    if(L->elem == NULL)
    {
        PRINT_ERR("[init_sqlist]malloc failed!\n");
        return FAILURE;
    }
    PRINT_INFO("[init_sqlist]malloc success!\n");
    L->length = 0;
    L->listsize = LIST_INIT_SIZE;
    return SUCCESS;
}

【C语言数据结构】线性表二:线性表的顺序表示和实现(代码)_第2张图片

/*pos counted from 1*/
int insert_sqlist(sqlist* L, int pos, int value)
{
    int* p_start = NULL;
    int* q_end = NULL;
    int* temp = NULL;

    /*CHECKPOINTER*/
    assert(L != NULL);

    /*check params*/
    if(pos < 1 || pos > L->length + 1)
    {
        PRINT_ERR("[insert_sqlist]invalid pos:%d\n", pos);
        return FAILURE;
    }

    if(L->length == L->listsize)
    {
        PRINT_INFO("[insert_sqlist]sqlist full, need to expand!\n");
        temp = L->elem;
        L->elem = (int*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(int));//realloc
        if(L->elem == NULL)
        {
            PRINT_ERR("[insert_sqlist]realloc failed!\n");
            free(temp);
            temp = NULL;
            return FAILURE;
        }
        PRINT_INFO("[insert_sqlist]realloc success!\n");
        temp = NULL;
        L->listsize += LISTINCREMENT;
    }

    for(p_start = &L->elem[pos - 1],q_end = &L->elem[L->length- 1]; q_end >= p_start; q_end--)
    {
        *(q_end + 1) = *q_end;
    }
    *p_start = value;
    L->length += 1;
    return SUCCESS;
}

【C语言数据结构】线性表二:线性表的顺序表示和实现(代码)_第3张图片

【C语言数据结构】线性表二:线性表的顺序表示和实现(代码)_第4张图片

/*pos counted fom 1*/
int delete_sqlist(sqlist* L, int pos)
{
    int* p_start = NULL;
    int* q_end = NULL;

    /*CHECKPOINTER*/
    assert(L != NULL);

    /*check param*/
    if(pos < 1 || pos > L->length)
    {
        PRINT_ERR("[delete_sqlist]invalid pos:%d\n", pos);
        return FAILURE;
    }

    if(L->length == 0)
    {
        PRINT_ERR("[delete_sqlist]empty sqlist, cannot delete elem!\n");
        return FAILURE;
    }

    for(p_start = &L->elem[pos - 1], q_end = &L->elem[L->length - 1]; q_end > p_start; p_start++)
    {
        *p_start = *(p_start + 1);
    }
    L->length -= 1;
    return SUCCESS;
}
void destroy_sqlist(sqlist* L)
{
    /*CHECKPOINTER*/
    assert(L != NULL);

    if(L->elem != NULL)
    {
        free(L->elem);
        L->elem = NULL;
    }
    L->length = 0;
    L->listsize = 0;
    return;
}
void print_sqlist(sqlist* L)
{
    int i = 0;

    /*CHECKPOINTER*/
    assert(L != NULL);

    for(i = 0; i < L->length; i++)
    {
        PRINT_INFO("L->elem[%d] = %d\n", i, L->elem[i]);
    }
    return;
}

 

你可能感兴趣的:(数据结构,C语言基础)