数据结构-顺序表

SeqList.h 的内容

#ifndef __SEQLIST_H__
#define __SEQLIST_H__
typedef void SeqList;
typedef void SeqListNode;

/*顺序表创建*/
SeqList* SeqList_Creat(int capacity);
/*顺序表删除*/
void SeqList_Destroy(SeqList* list);
/*顺序表清空*/
void SeqList_Clear(SeqList* list);
/*顺序表求长度*/
int SeqList_Length(SeqList* list);
/*顺序表插入*/
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
/*顺序表获取位置上的元素*/
SeqListNode* SeqList_Get(SeqList* list, int pos);
/*删除顺序表的某位置的元素*/
SeqListNode* SeqList_Delete(SeqList* list, int pos);

#endif

SeqList.c

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "SeqList.h"

typedef struct TSeqList
{
    int capacity,length;
    SeqListNode **ArrayNode;
}TSeqList;

/*顺序表创建*/
SeqList* SeqList_Creat(int capacity)
{
    if (capacity < 0)
    {
        printf("func SeqList_Creat() err ");
        return NULL;
    }
    TSeqList *tmp = NULL;
    tmp = (TSeqList*)malloc(sizeof(TSeqList));
    memset(tmp, 0, sizeof(TSeqList));
    if (tmp == NULL)
    {
        printf("func SeqList_Creat() err ");
        return NULL;
    }
    tmp->length = 0;
    tmp->capacity = capacity;
    tmp->ArrayNode = (SeqListNode**)malloc(capacity*sizeof(SeqListNode*));
    memset(tmp->ArrayNode, 0, 10 * sizeof(SeqListNode*));
    return tmp;
}
/*顺序表删除*/
void SeqList_Destroy(SeqList* list)
{
    int ret;
    if (list == NULL)
    {
        ret = -1;
        printf("func SeqList_Destroy() err %d !\n",ret);
        return;
    }
    TSeqList* tlist = list;
    if (tlist->ArrayNode != NULL)
    {
        free(tlist->ArrayNode);
    }
    if (tlist != NULL)
    {
        free(tlist);
    }
}
/*顺序表清空*/
void SeqList_Clear(SeqList* list)
{
    int ret;
    if (list == NULL)
    {
        ret = -1;
        printf("func SeqList_Clear() err %d !\n", ret);
        return;
    }
    TSeqList *tlist = list;
    tlist->length = 0;
    memset(tlist->ArrayNode, 0, tlist->capacity*sizeof(SeqListNode*));
}
/*顺序表求长度*/
int SeqList_Length(SeqList* list)
{
    int ret = 0;
    if (list == NULL)
    {
        ret = -1;
        printf("func SeqList_Length() err %d !\n", ret);
        return;
    }
    TSeqList *tlist = list;
    return tlist->length;
}
/*顺序表插入*/
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
    int ret = 0;
    if (list == NULL || node == NULL || pos < 0)
    {
        ret = -1;
        printf("func SeqList_Length()at '(list == NULL || node == NULL || pos < 0)' err %d !\n", ret);
        return ret;
    }
    TSeqList *tlist = list;
    int *tmp = tlist->ArrayNode;
    /*如果pos在正常范围(0-length)则执行后移插入*/
    if (0 < pos<= tlist->length)
    {
        for (int i = tlist->length; i > pos; i--)
        {
            tmp[i] = tmp[i - 1];
        }
        tmp[pos] = node;
        tlist->length++;
    }
    /*如pos大于length,如何数组满了这替换最后,没满则是最加到最后*/
    if (tlist->length < pos )
    {
        if (tlist->length == tlist->capacity)
        {
            tmp[tlist->length-1] = node;
        }
        if (tlist->length < tlist->capacity)
        {
            tmp[tlist->length] = node;
            tlist->length++;
            printf("");
        }
    }
}
/*顺序表获取位置上的元素*/
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
    int ret = 0;
    if (list == NULL || pos < 0)
    {
        ret = -1;
        printf("func SeqList_Get() err %d", ret);
        return NULL;
    }
    TSeqList* tlist = (TSeqList*)list;
    int *tmp = tlist->ArrayNode;
    /*如果pos在正常范围(0-length)这返回pos 的node*/
    if (0 < pos && pos<= tlist->length)
    {
        return tmp[pos];
    }
    /*如pos大于length*/
    if (tlist->length < pos)
    {
        /*
        if (tlist->length == tlist->capacity)
        {
            return tmp[tlist->length - 1];
        }
        if (tlist->length < tlist->capacity)
        {
            return tmp[tlist->length];
        }
        */
        return tmp[tlist->length - 1];
    }
}
/*删除顺序表的某位置的元素*/
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
    int ret = 0;
    if (list == NULL || pos < 0)
    {
        ret = -1;
        printf("func SeqList_Get() err %d", ret);
        return ;
    }

}

main.c

#include <stdio.h>
#include "SeqList.h"
int main()
{
    SeqList *list = SeqList_Creat(10);
    SeqList_Destroy(list);
    list = SeqList_Creat(10);
    SeqList_Clear(list);
    char *a = "hello";
    SeqList_Insert(list, a, 4);
    int list_len = SeqList_Length(list);
    char *a1 = SeqList_Get(list, 4);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值