顺序表的创建和基本操作

#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct _tag_SeqList
{
    int capacity;//容量
    int length;//长度
     int * node;//地址
}TSeqList;//创建头结点的元素

typedef void SeqList;//定义不返回类型的表
typedef void SeqListNode;
//创建顺序表
SeqList* SeqList_create(int capacity)
{
    int ret;
    TSeqList* temp = NULL;
    temp = (TSeqList*)malloc(sizeof(TSeqList));//给头结点分配空间(动态分配)
    if (temp == NULL)
    {
        ret = 1;
        printf("func SeqList_create() error:%d\n", ret);
        return NULL;

    }
    memset(temp, 0, sizeof(TSeqList));
    temp->capacity = capacity;//为顺序表分配空间
    temp->length = 0;
    temp->node = (int*)malloc(sizeof(void*)*capacity);//分配一个指针数组
        if (temp->node == NULL)
        {
            ret = 2;
            printf("func SeqList_create() error:%d\n", ret);
            return NULL;
        }
    return temp;
    //求顺序表的容量
}int SeqList_Capacity(SeqList* list)
{
    TSeqList * temp = NULL;
    if (list == NULL)
    {
        return 0;
    }
    temp = (TSeqList *)list;
    return temp->capacity;
}
//获取顺序表的长度
int SeqList_Length(SeqList* list)
{
    TSeqList * temp = NULL;
    if (list == NULL)
    {
        return ;
    }
    temp = (TSeqList *)list;
    return temp->length;
}
//插入元素
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
    int i;
    TSeqList * temp = NULL;
    if (list == NULL || node == NULL)
    {
        return -1;
    }
    temp = (TSeqList *)list;
    if (temp->length >= temp->capacity)
    {
        return -2;
    }
    if (pos > temp->length)
        pos = temp->capacity;
    for (i = temp->length;i > pos;i--)
    {
        temp->node[i] = temp->node[i - 1];

    }
        temp->node[i] = (int)node;
        temp->length++;
        return 0;


}
//删除元素
SeqList* SeqList_delete(SeqList* list, int pos)
{
    int i;
    TSeqList *tlist = NULL;
    SeqListNode *temp = NULL;
    tlist = (TSeqList *)list;
    if (list == NULL || pos<0 || pos>tlist->capacity)
    {
        printf("Seqlist_delete()erroror\n");
        return NULL;
    }
    temp = (SeqListNode *)tlist->node[pos];
    for (i = pos + 1;i < tlist->length;i++)
    {
        tlist->node[i - 1] = tlist->node[i];
    }
    tlist->length--;
}
//查找元素
SeqList* SeqList_Get(SeqList* list,int pos)   
{
    TSeqList *tlist = NULL;
    SeqListNode * temp = NULL;
    tlist = (TSeqList *)list;
    if (list == NULL || pos<0 || pos>=tlist->capacity)
    {
        printf("Seqlistnode _get(error\n");
        return NULL;
    }
    temp = (SeqListNode*)tlist->node[pos];
    return temp;
}
//清空顺序表
void SeqList_clear(SeqList*list)
{
    TSeqList *temp = NULL;
    if (list == NULL)
    {
        return ;
    }
    temp = (TSeqList *)list;
    temp->length = 0;
    memset(temp->node, 0, (temp->capacity * sizeof(void*)));
        return;
}
//销毁顺序表
void SeqList_destory(SeqList*list)
{
    TSeqList* temp = NULL;
    if (list == NULL)
    {
        return;
    }
    temp = (TSeqList *)list;
    if (temp->node != NULL)
    {
        free(temp->node);
    }
    free(temp);
    return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CC楠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值