自己手写一个线性表List【C风格】

cpp 复制代码
#include <iostream>

//线性表、顺序表List

#define MAX_SIZE 20
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status;//返回状态类型
typedef int ElemType;//元素类型

//结构体
typedef struct 
{
    ElemType data[MAX_SIZE];//数据类型,数组
    int length;//长度
} myList;

//初始化
Status InitList(myList* list)
{
    //如果为空返回
    if (list == NULL)
        return ERROR;
    //不为空,设置为空
    list->length = 0;
    return OK;
}

//清空列表
Status ClearList(myList* list)
{
    //如果为空返回
    if (list == NULL)
        return ERROR;
    //不为空,设置为空
    list->length = 0;
    return OK;
}

//列表是否为空
Status ListIsEmpty(myList* list)
{
    //列表不存在,就是空
    if (list == NULL)
        return TRUE;
    //长度为0,为空
    if (list->length == 0)
        return TRUE;
    //否则就非空
    return FALSE;
}

//获取列表长度
Status ListLength(myList* list)
{
    return list->length;
}

//插入数据
Status ListInsert(myList* list,int index,const ElemType e)
{
    //如果列表为空
    if ((list->length == 0)&&(index == 1))
    {
        list->data[0] = e;
        list->length++;
        return OK;
    }

    //如果列表不为空
    if ((index <= list->length) && (index >= 1) && (list->length < MAX_SIZE))
    {
        for (int i = list->length - 1; i >= index - 1; i--)
        {
            list->data[i + 1] = list->data[i];
        }
        list->data[index - 1] = e;
        list->length++;
        return OK;
    }
    return ERROR;
}

//删除数据
Status ListDelete(myList* list, int index)
{
    //删除中间的元素
    if ((index < list->length) && (index > 0) )
    {
        for (int i = index; i < list->length; i++)
        {
            list->data[i-1] = list->data[i];
        }
        list->length--;
        return OK;
    }

    //删除末尾的元素
    if ((index == list->length) && (index > 0))
    {
        list->length--;
        return OK;
    }
    return ERROR;
}

//遍历元素
Status ListTraverse(myList* list)
{
    for (int i = 0; i < list->length; i++)
    {
        printf("%d-->",list->data[i]);
    }
    printf("\r\n");
    return OK;
}

//访问元素
Status GetElem(myList* list, int index, ElemType* e)
{
    if ((index > 0) && (index < list->length) && (list->length > 0))
    {
        *e = list->data[index - 1];
        return OK;
    }
    return ERROR;
}

int main()
{
    myList list;
    ElemType e;
    Status res;
    int i, j;
    res = InitList(&list);
    printf("初始化后:length = %d\n", list.length);

    //插入元素
    for (i = 0; i < 6; i++)
    {
        res = ListInsert(&list, 1, i);
    }
    ListTraverse(&list);//显示元素

    res = ListIsEmpty(&list);
    printf("list 是否为空?%d(1: 是,0: 否)\n", res);
    ClearList(&list);

    res = ListIsEmpty(&list);
    printf("list 是否为空?%d(1: 是,0: 否)\n", res);

    for (i = 0; i < 11; i++)
    {
        res = ListInsert(&list, 1, i);
    }
    ListTraverse(&list);//显示元素

    ListInsert(&list, 1, 0);
    ListTraverse(&list);//显示元素

    GetElem(&list, 5, &e);
    printf("第5元素是%d\n", e);

    j = list.length;
    res =  ListDelete(&list, j+1);
    if (res == ERROR)
        printf("删除第%d元素失败!\n", j + 1);
    else
        printf("删除第%d元素成功!\n", j);

    res = ListDelete(&list, j);
    if (res == ERROR)
        printf("删除第%d元素失败!\n", j);
    else
        printf("删除第%d元素成功!\n", j);

    ListTraverse(&list);//显示元素

    res = ListDelete(&list, 5);
    if(res == OK)printf("删除第%d元素成功!\n", 5);
    ListTraverse(&list);//显示元素

    return 0;
}
相关推荐
凯子坚持 c1 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
小莞尔1 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔1 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329291 天前
Day03_刷题niuke20250915
c语言
路由侠内网穿透1 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
第七序章2 天前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
l1t2 天前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
l1t2 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
Gu_shiwww2 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步