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

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

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

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

#ifndef __HEADER_H__
#define __HEADER_H__ 100
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <strings.h>

#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__*/

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;
}

/*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;
}

/*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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值