通用简单版-顺序表

seqlist.h

#pragma once

//给用户的头文件
//顺序表
//顺序表和链表的区别是 :顺序表是申请一段连续的内存
//顺序表可以利用类似于数组的形式访问,即通过下标访问。当然定义的变量类型必须是指针类型的,很方便,当然也可以通过像链表一样的访问。
//单链表嘛,只是将空间分散开了,这样的优点就是动态申请,需要多少就申请多少,一般一次申请一个空间结点,即N=1。
//当然顺不表也可以实现需要多少就申请多少。但一般就是初始申请多少,不够按一定的量继续申请。
//说到底,顺序表访问比较方便,单链表动态申请比较灵活。

typedef void SeqList;

typedef void  SeqListNode;

//创建
SeqList *SeqList_Create(int capacity);

//销毁
void SeqList_Destroy(SeqList *list);

//清空
void SeqList_Clear(SeqList *list);

//长度
int SeqList_Length(SeqList*list);

//插入
SeqListNode *SeqList_Insert(SeqList *list, SeqListNode *node, int pos);

//获取
SeqListNode *SeqList_Get(SeqList *list, int pos);

//删除
SeqListNode *SeqList_Delete(SeqList *list, int pos);

seqlist.c

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "seqlist.h"
//内部实现

typedef struct _tag_SeqList
{
    int capacity; //容量
    int length; //记录当前使用多少
    unsigned int *node;  //相当于  int  node[0];
}TSeqList;


//创建
SeqList *SeqList_Create(int capacity)
{
    TSeqList *ret = (TSeqList *)malloc(sizeof(TSeqList)+sizeof(unsigned int )*capacity);

    if (ret == NULL)
    {

        return NULL;
    }
    //初始化内存
    memset(ret, 0, sizeof(TSeqList) + sizeof(unsigned int)*capacity);

    ret->length = 0;

    ret->capacity = capacity;
    ret->node = ret + 1;  //node节点等于首地址偏移到一一个/
    return ret;
}

//销毁
void SeqList_Destroy(SeqList *list)
{
    TSeqList *ret = (TSeqList *)list;
    if (ret != NULL)
        free(ret);
}

//清空
void SeqList_Clear(SeqList *list)
{

    TSeqList *ret = (TSeqList *)list;
    if (ret != NULL)
    {
        ret->length = 0;
    }

}

//长度
int SeqList_Length(SeqList*list)
{


    TSeqList *ret = (TSeqList *)list;
    if (ret != NULL)
    {
        return ret->length;
    }
    return 0;

}


//插入
SeqListNode *SeqList_Insert(SeqList *list, SeqListNode *node, int pos)
{

    TSeqList *ret = (TSeqList *)list;

    if (ret == NULL || node == NULL || pos<0)
    {
        return NULL;
    }

    if (ret->length < pos)
    {
        pos = ret->length;
    }
    for (int i = ret->length; i > pos; i--)
    {
        //把元素往后移动
        ret->node[i] = ret->node[i - 1];
    }

    ret->node[pos] = (unsigned int)node;//新插入的

    ret->length++;

    return node;
}

//获取
SeqListNode *SeqList_Get(SeqList *list, int pos)
{

    TSeqList *ret = (TSeqList *)list;

    if (ret == NULL || pos<0)
    {
        return NULL;
    }

    if (ret->length < pos)
    {
        pos = ret->length;
    }

    return ret->node[pos];

}

//删除
SeqListNode *SeqList_Delete(SeqList *list, int pos)
{
    TSeqList *ret = (TSeqList *)list;

    if (ret == NULL || pos<0)
    {
        return NULL;
    }

    if (ret->length < pos)
    {
        pos = ret->length;
    }
    SeqListNode *node = (SeqListNode *)ret->node[pos];

    for (int i = pos; i < ret->length; i++)
    {

        ret->node[i] = ret->node[i + 1];        
    }
    ret->length--;
    return node;
}

main.c

#include "seqlist.h"
#include "stdlib.h"
#include "stdio.h"



typedef struct _Teacher
{
    SeqListNode *head;
    int age;
}Teacher;

void main()
{
    SeqList *list = SeqList_Create(10);
    if (list == NULL)
        return ;


    Teacher t1, t2, t3;
    t1.age = 10;
    t2.age = 20;
    t3.age = 30;
    SeqList_Insert(list, (SeqListNode*)&t1,0);
    SeqList_Insert(list, (SeqListNode*)&t2, 0);
    SeqList_Insert(list, (SeqListNode*)&t3, 0);


    for (int i = 0; i < 6; i++)
    {
        Teacher *temp = (Teacher *)SeqList_Get(list, i);
        if (temp != NULL)
        {
            printf("Teacher age:%d \n",temp->age);
        }
    }
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值