【数据结构】顺序表

结构

cpp 复制代码
typedef int SLData;
const int INIT_CAPACITY = 10;
typedef struct SeqList
{
	int size;
	int capacity;
	SLData* arr;
}SL;

扩容逻辑

cpp 复制代码
void CheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		SLData* tmp = (SLData*)realloc(ps->arr, sizeof(SL) * (ps->capacity * 2));
		if (tmp == NULL)
		{
			perror("realloc fail");
		}
		ps->arr = tmp;
		ps->capacity *= 2;
	}
}

初始化和销毁

cpp 复制代码
void SLInit(SL* ps)
{
	ps->capacity = INIT_CAPACITY;
	ps->arr = (SLData*)malloc(sizeof(SL) * (ps->capacity));
	ps->size = 0;
}
void SLDestory(SL* ps)
{
	free(ps->arr);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

尾插尾删

cpp 复制代码
void PushBack(SL* ps, SLData x)
{
	assert(ps != NULL);
	CheckCapacity(ps);
	ps->arr[ps->size++] = x;
}
void PopBack(SL* ps)
{
	assert(ps != NULL);
	if (ps->size == 0) return;
	ps->size--;
}

头插头删

cpp 复制代码
void PushFront(SL* ps, SLData x)
{
	assert(ps != NULL);
	CheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->arr[end + 1] = ps->arr[end];
		end--;
	}
	ps->arr[0] = x;
	ps->size++;
}
void PopFront(SL* ps)
{
	assert(ps != NULL);
	if (ps->size == 0) return;
	int end = 1;
	while (end != ps->size)
	{
		ps->arr[end - 1] = ps->arr[end];
		end++;
	}
	ps->size--;
}

指定位置插入删除

cpp 复制代码
void Insert(SL* ps, int pos, SLData x)
{
	assert(ps && pos < ps->size);
	CheckCapacity(ps);
	int cur = ps->size-1;
	while (cur >= pos)
	{
		ps->arr[cur + 1] = ps->arr[cur];
		cur--;
	}
	ps->arr[pos] = x;
	ps->size++;
}
void Erase(SL* ps, int pos)
{
	assert(ps != NULL && pos < ps->size);
	int cur = pos;
	while (cur <= ps->size - 2)
	{
		ps->arr[cur] = ps->arr[cur + 1];
		cur++;
	}
	ps->size--;
}

查找数据位置和打印

cpp 复制代码
int find(SL* ps, SLData x)
{
	assert(ps != NULL);
	int cur = 0;
	while (cur != ps->size)
	{
		if (ps->arr[cur] == x) return cur;
		cur++;
	}
	return -1;
}

void Print(SL* ps)
{
	assert(ps != NULL);
	int pos = 0;
	while (pos != ps->size)
	{
		printf("%d-> ", ps->arr[pos]);
		pos++;
	}
	printf("NULL\n");
}

测试用例

cpp 复制代码
void test1()
{
	SL s;
	SLInit(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PushBack(&s, 5);
	Print(&s);
	PopBack(&s);
	PopBack(&s);
	PopBack(&s);
	Print(&s);
	PushFront(&s, 1);
	PushFront(&s, 2);
	PushFront(&s, 3);
	Print(&s);
	PopFront(&s);
	PopFront(&s);
	PopFront(&s);
	PopFront(&s);
	PopFront(&s);
	PopFront(&s);
	Print(&s);
	PushFront(&s, 1);
	PushFront(&s, 2);
	PushFront(&s, 3);
	Print(&s);
	Insert(&s, 1, 999);
	Print(&s);
	Erase(&s, 1);
	Erase(&s, 1);
	Erase(&s, 1);
	Print(&s);

	printf("%d\n", find(&s, 3));
}
相关推荐
Дерек的学习记录10 小时前
C++:入门基础(下)
开发语言·数据结构·c++·学习·算法·visualstudio
程序员酥皮蛋13 小时前
hot 100 第二十四题 24.回文链表
数据结构·链表
仟濹16 小时前
【算法打卡day7(2026-02-12 周四)算法:BFS and BFS】 3_卡码网107_寻找存在的路线_并查集
数据结构·算法·图论·宽度优先
数智工坊16 小时前
【数据结构-排序】8.2 冒泡排序-快速排序
数据结构
芝士爱知识a17 小时前
[2026深度测评] AI期权交易平台推荐榜单:AlphaGBM领跑,量化交易的新范式
开发语言·数据结构·人工智能·python·alphagbm·ai期权工具
芝士爱知识a17 小时前
【FinTech前沿】AlphaGBM:重塑期权交易的智能分析引擎——从原理到实践
数据结构·数据库·人工智能·alphagbm·期权
EE工程师18 小时前
数据结构篇 - 循环队列
数据结构
俩娃妈教编程18 小时前
洛谷选题:P1420 最长连号
数据结构·算法
二年级程序员19 小时前
单链表算法题思路详解(上)
c语言·数据结构·c++·算法