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);
}
标签智能推荐:
linux 下怎么看postgresql安装到哪个目录了?
psql-Upostgres-c'SHOWconfig_file'
C++学习--学习网点收集
C语言中文网:http://c.biancheng.net/cplus/
特殊字符转译工具类
=newStringBuilder();for(inti=0;i<s.length();++i){charc=s.charAt(i);if(c=='\\'||c=='+'||c=='-'||c=='!'||c=='('||c==')'||c==':'||c=='^'||c=='['||c==']'||c=='"'||c=='{'||c=='}'||c=='~'||c=='*'||c=='?'
在linux中查看python中下载的模块位置
python3-c"importrest_framework;print(rest_framework)"
Awesome
本文更新于2021-11-03。目录C/C++DockerGitGoIDE/EditorJavaScriptLinuxMySQLNetworkNginxNode.jsRedisSupervisorC/C++DockerGitGoIDE/EditorJavaScriptLinuxMySQLNetworkNginxNode.jsRedisSupervisor
Es
arge":2,"precursorMass":1348.52,"result":"TyrocidineC","SMILES":"O=C(O)CCC1NC(=O)C(NC(=O)C3NC(=O)C(NC(=O)C(N)C(C)SCC(NC1(=O))C(=O)NCC(=O)NC2C(=O)NC(C(=O)NC(C(=O)NC(C(=O)NC(C(=O)NC(C(=O)O)CS(=O)C2(C))C
sorted多条件排序
b':3,'c':'c'},{'a':2,'b':3,'c':'c'},{'a':1,'b':2,'c':'a'},{'a':3,'b':4,'c':'s'}]sorted(l,key=lambdax:x['b'])#根据单个条件排序[{'a':1,'b':2,'c':'a'},{'a':1,'b':3,'c':'c'},{'a':2,'b':3,'c':'c'},{'a':3,'b':4,'c'
C/C++ Qt 图形化开发
C/C++Qt基础通用组件应用 C/C++QtQChart绘图组件应用 C/C++QT使用Thread线程库 C/C++QT文件与目录操作 C/C++QT简单命令行版网络通信 QTQFileSystemWatcher文件监控 QTSQL操作SQLite数据库 QT使用QtCharts绘制图形 C/C++QT实现自定义对
偶像的世界
/**drivers/usb/core/usb.c**(C)CopyrightLinusTorvalds1999*(C)CopyrightJohannesErdfelt1999-2001*(C)CopyrightAndreasGal1999*(C)CopyrightGregoryP.Smith1999*(C)CopyrightDetiFliegl1999(newUSBarchitecture)*(
python-赋值变量符
hon-赋值变量符1.python-赋值变量符赋值操作变量操作符描述示例=赋值,将=左侧的结果赋值给等号左侧的变量a=10b=20+=加法赋值c+=a等价c=c+a-=减法赋值c-=a等价c=c–a*=乘法赋值c*=a等价c=c*a/=除法赋值c/=a等价c=c/a//=整除赋值c//=a等价c=c//a%=取余赋值c%=a等价c=c%a**=幂赋值c=a等价c=ca2.示例示例#!/usr/bi