C语言实现Java中的ArrayList

C语言实现Java中的ArrayList

1. 定义结构体

typedef struct ArrayList
{
    int *array;
    int size;
} ArrayList;

//ArrayList中包含一个数组和一个size属性,size用来指示array中的数据数量。

2. ArrayList初始化

void init(ArrayList *list)
{
    list->size = 0;
    list->array = NULL;
}

// 初始化,让list的array为NULL,size为0。传入参数为ArrayList对象
//(这里的对象指的是ArrayList结构体,以指针的类型传入。)

3. 添加元素

bool add(ArrayList *list, int element)
{
    if (list->size == 0)
    {
        static int arr[10];
        list->array = arr;
    }

    list->array[list->size] = element;
    list->size++;
    return True;
}
/*
添加元素:传入ArrayList的指针和一个元素,如果list的size为0的话,让list中的array
		指向一个真正的数组,然后将元素按顺序放到该数组中。
*/

4. 插入元素

/* 
	在指定位置插入元素
	传入参数:ArrayList指针,索引index,元素element
	插入方式,按照数组插入方式,在x位置插入,则将x —— (size-1)位置的元素全部向后移动
*/
bool insert(ArrayList *list, int index, int element)
{
    if (index < 0 || index > list->size)
    {
        printf("Insert index is not defined.\n");
        return False;
    }
    if (index == list->size)
    {
        add(list, element);
        return True;
    }
    for (int i = list->size - 1; i >= index; i--)
    {
        list->array[i + 1] = list->array[i];
    }
    list->array[index] = element;
    list->size++;
    return True;
}

5. 删除元素(按照位置)

/*
	按照索引删除元素
	参数:ArrayList指针,索引index
*/
bool del_index(ArrayList *list, int index)
{
    if (index < 0 || index >= list->size)
    {
        printf("Del index is not defined");
        return False;
    }
    for (int i = index; i < list->size; i++)
    {
        list->array[i] = list->array[i + 1];
    }
    list->size--;
    return True;
}

6. 获取元素索引

/*
	获取元素位置
	参数:ArrayList指针,元素element
	返回值:找到的第一个元素的索引,如果数组中没有,则返回-1
*/
int get_ele(ArrayList *list, int element)
{
    int flag = 0;
    for (int i = 0; i < list->size; i++)
    {
        if (list->array[i] == element)
        {
            flag = 1;
            return i;
        }
    }
    if (!flag)
    {
        return -1;
    }
}

7. 删除元素(按照元素)

/*
	删除ArrayList中出现的所有该元素
	参数:ArrayList指针,元素element
	
	使用递归方式,如果获取该元素位置为-1,则跳出递归,否则,通过索引删除(获取元素位置)
	即:del_index(list,get(element));
*/

bool del_ele(ArrayList *list, int element)
{
    if (get_ele(list, element) == -1)
    {
        return True;
    }
    del_index(list, get_ele(list, element));
    del_ele(list, element);
}

8. 按索引获取元素

/*
	获取该索引上的元素
	参数:ArrayList指针,索引index
*/

int get(ArrayList *list, int index)
{
    return list->array[index];
}

9. 打印ArrayList

/*
	打印传入的ArrayList中的所有元素。
	参数:ArrayList指针
*/

void print_list(ArrayList *list)
{
    if (list->size == 0)
    {
        printf("size:%d\n", list->size);
        return;
    }
    int n = 0;
    printf("\n--------------\n");
    for (int i = 0; i < list->size; i++)
    {
        n++;
        printf("%d\t", *(list->array++));
        if (n % 10 == 0)
        {
            printf("\n");
        }
    }
    printf("\n---------------\nsize:%d", list->size);
}

总结:

#include <stdio.h>
#include <stdlib.h>

#define True 1
#define False 0

typedef int bool;


typedef struct ArrayList
{
    int *array;
    int size;
} ArrayList;

void init(ArrayList *list)
{
    list->size = 0;
    list->array = NULL;
}

bool add(ArrayList *list, int element)
{
    if (list->size == 0)
    {
        static int arr[10];
        list->array = arr;
    }

    list->array[list->size] = element;
    list->size++;
    return True;
}

bool insert(ArrayList *list, int index, int element)
{
    if (index < 0 || index > list->size)
    {
        printf("Insert index is not defined.\n");
        return False;
    }
    if (index == list->size)
    {
        add(list, element);
        return True;
    }
    for (int i = list->size - 1; i >= index; i--)
    {
        list->array[i + 1] = list->array[i];
    }
    list->array[index] = element;
    list->size++;
    return True;
}

bool del_index(ArrayList *list, int index)
{
    if (index < 0 || index >= list->size)
    {
        printf("Del index is not defined");
        return False;
    }
    for (int i = index; i < list->size; i++)
    {
        list->array[i] = list->array[i + 1];
    }
    list->size--;
    return True;
}

int get_ele(ArrayList *list, int element)
{
    int flag = 0;
    for (int i = 0; i < list->size; i++)
    {
        if (list->array[i] == element)
        {
            flag = 1;
            return i;
        }
    }
    if (!flag)
    {
        return -1;
    }
}

bool del_ele(ArrayList *list, int element)
{
    if (get_ele(list, element) == -1)
    {
        return True;
    }

    del_index(list, get_ele(list, element));
    // for (int i = 0; i < list->size; i++)
    // {
    //     if (list->array[i] == element)
    //     {
    //         del_index(list, i);
    //         break;
    //     }
    // }
    del_ele(list, element);
}

int get(ArrayList *list, int index)
{
    return list->array[index];
}

void print_list(ArrayList *list)
{
    if (list->size == 0)
    {
        printf("size:%d\n", list->size);
        return;
    }
    int n = 0;
    printf("\n--------------\n");
    for (int i = 0; i < list->size; i++)
    {
        n++;
        printf("%d\t", *(list->array++));
        if (n % 10 == 0)
        {
            printf("\n");
        }
    }
    printf("\n---------------\nsize:%d", list->size);
}
posted @   zxinlog  阅读(307)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示