当前位置:网站首页>C语言顺序表(源码)
C语言顺序表(源码)
2022-08-10 03:25:00 【@Main.】
目录
插入的三种方法:

删除的三种方法:

静态顺序表
初始化
//数据结构
#define MAX_SIZE 10
typedef int type;
typedef struct seqlist {
type arr[MAX_SIZE];
int count;
}Seq;
//初始化数据
void Init(Seq* list)
{
memset(list->arr, 0, sizeof(int) * MAX_SIZE);
list->count = 0;
}尾插
//尾插
void PushBack(Seq* list,type x)
{
if (list->count == MAX_SIZE)
{
printf("Error,ñ\n");
return;
}
list->arr[list->count] = x;
list->count++;
}打印表
//打印表
void Print(Seq* list)
{
int i = 0;
for (i = 0; i < list->count; i++)
{
printf("%d ", list->arr[i]);
}
}动态顺序表
初始化
//定义一个表
typedef int type;
typedef struct Seqlist {
type* arr;
int count;
int capacity;
}Seq;
//初始化表
void Inint(Seq* list)
{
list->arr = NULL;
list->count = 0;
list->capacity = 0;
}扩容
//扩容
void Enlarge(Seq* list)
{
//先判断是否放满
if (list->count == list->capacity)
{
//动态增大容量
int newcapacity = list->capacity == 0 ? 4 : list->capacity * 2;
//扩大内存
type* pa = (type*)realloc(list->arr, newcapacity * sizeof(type));
if (pa == NULL)
{
printf("开辟失败\n");
return;
}
list->arr = pa;
list->capacity = newcapacity;
}
}头插
//头插
void PushFront(Seq* list, type x)
{
//先把前面的所有数据向后移动一次
int end = list->count - 1;
while (end-- >= 0)
{
list->arr[end + 1] = list->arr[end];
}
//把x放到头位置
list->arr[0] = x;
list->count++;
}尾插
//尾插
void PushBack(Seq* list,type x)
{
list->arr[list->count] = x;
list->count++;
}中间任意位置插
//中间任意位置插
void PushInsert(Seq* list,int pos,int x)
{
//判断是否已放满
//可以调用扩容函数
//这里假设已经调用
//判断,目标位置不能大于现有数量
assert(pos <= list->count);
//先将从该位置的数据全部依次向后移一位(从头开始)
int end = list->count - 1;
while (end >= pos)
{
list->arr[end + 1] = list->arr[end];
end--;
}
//插入
list->arr[pos] = x;
list->count++;
}头删
//头删
void DelFront(Seq* list)
{
int start = 1;
//后面的数据依次往前移(从头开始)
while (start <= list->count)
{
list->arr[start - 1] = list->arr[start];
start++;
}
list->count--;
}尾删
//尾删
void DelBack(Seq* list)
{
//直接将最后一个数据扔掉
list->count--;
}中间任意位置删
//任意位置删
void DelInsert(Seq* list, int pos)
{
//判断一下要删除的位置在不在已存的数据列内,否则没有意义
assert(pos <= list->count);
int start = pos + 1;
while (start <= list->count)
{
list->arr[start - 1] = list->arr[start];
start++;
}
list->count--;
}打印表
//打印表
void Print(Seq* list)
{
int i = 0;
for (i = 0; i < list->count; i++)
{
printf("%d ", list->arr[i]);
}
}
释放空间
//释放空间
void Free(Seq* list)
{
//一定要记得是:先释放再置空,否则会成野指针,永远丢失空间
free(list->arr);
list->arr = NULL;
list->count = 0;
list->capacity = 0;
}
边栏推荐
猜你喜欢
随机推荐
BFF避坑指南
笔试题记录
微信小程序相互跳转如何携带参数
goland json.Marshal导致&变成\u0026
新零售社交电商APP系统平台如何打造公域+私域流量?
js阻止事件冒泡方案
量化投资学习——在FPGA上运行高频交易策略
shell文本编辑awk
树的介绍、树的定义和基本术语、二叉树的定义和性质、二叉树的顺序表示与实现和链式表示与实现以及树的遍历方法以及两种创建方式
Neo4J 与 Cypher 查询语言基础
How does a new tester do functional testing?Test thinking is really important
如何使用腾讯字体,已经在什么场合下可以使用该字体?TTTGB-Medium
It's almost 35, still "did a little"?What happened to the test workers who had been in the industry for a few years?
使用flink-sql写入mysql的时候,只指定插入的字段,但是会报错id字段错误,没有默认值,创
proxy代理服务
Dynamic Web Development Fundamentals
Difference between netstat and ss command
uni-app自定义导航栏
如何快速成为一名软件测试工程师?测试员月薪15k需要什么技术?
MongoDB 常用查询语句









