数据结构——动态顺序表(DEV C++版本)

之前写的动态顺序表是在vs2022中完成的,为了照顾没有vs2022的家人,发布一篇DEV版本

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct {
	int *arr;
	int size;
	int capacity;
}SL;
SL s;
void init(SL* ps)
{
	ps->arr=NULL;
	ps->capacity=0;
	ps->size=0;
 } 
 void print(SL* ps)
 {
 	int i=0;
 	for(i=0;i<ps->size;i++)
 	{
 		printf("%d  ",ps->arr[i]);
	 }
	 printf("\n");
 }
 void check(SL* ps)
 {
 	int newcapacity=ps->capacity==0?4:2*ps->capacity;
 	if(ps->size==ps->capacity)
 	{
 		int* temp=(int*)realloc(ps->arr,sizeof(int)*2*ps->capacity);
 		if(temp==NULL)
 		{
 			printf("error\n");
 			exit(-1);
		 }
		 else
		 {
		 	ps->arr=temp;
		 	ps->capacity=newcapacity;
		 }
	 }
 }
 void pushback(SL* ps,int x)
 {
 	
 	check(ps);
 	ps->arr[ps->size]=x;
 	ps->size++;
 }
 void pushfront(SL* ps,int x)
 {
 	check(ps);
 	int end=ps->size-1;
 	while(end>=0)
 	{
 		ps->arr[end+1]=ps->arr[end];
 		end--;
	 }
	 ps->size++;
	 ps->arr[0]=x;
 }
 void deleteback(SL* ps)
 {
 	ps->size--;
 }
 void deletefront(SL* ps)
 {
 	int tail=1;
 	while(tail<ps->size)
 	{
 		ps->arr[tail-1]=ps->arr[tail];
 		tail++;
	 }
	 ps->size--;
 }
 void pushrandom(SL* ps,int index,int x)
 {
 	assert(index<=ps->size);
 	check(ps);
 	int end=ps->size;
 	while(end>index)
 	{
 		ps->arr[end]=ps->arr[end-1];
 		end--;
	 }
	 ps->arr[index]=x;
	 ps->size++;
 }
 void deleterandom(SL* ps,int index)
 {
 	assert(index<ps->size);
 	int begin =index;
 	while(begin<=ps->size-1)
 	{
 		ps->arr[begin]=ps->arr[begin+1];
 		begin++;
	 }
	 ps->size--;
 }
 void updata(SL* ps,int index,int x)
 {
 	ps->arr[index]=x;
 }
 void test()
 {
 	init(&s);
 	pushback(&s,2);
 	pushback(&s,1);
 	pushback(&s,1);
 	pushfront(&s,5);
 	pushfront(&s,8);
 	pushfront(&s,9);
 	print(&s); 
 	deleteback(&s);
 	deleteback(&s);
 	deletefront(&s);
 	deletefront(&s);
 	print(&s);
 	pushrandom(&s,1,5);
 	print(&s);
 	pushrandom(&s,1,9);
 	print(&s);
 	pushrandom(&s,1,8);
 	print(&s);
 	pushrandom(&s,1,2);
 	print(&s);
 	pushrandom(&s,1,3);
 	print(&s);
 	deleterandom(&s,1);
 	print(&s);
 	deleterandom(&s,1);
 	print(&s);
 	deleterandom(&s,1);
 	print(&s);
 	deleterandom(&s,1);
 	print(&s);
 	deleterandom(&s,1);
 	print(&s);
 	updata(&s,0,2);
 	print(&s);
 }
 int main()
 {
 	test();
 	free(s.arr);
 	return 0;
 }
相关推荐
??tobenewyorker10 分钟前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode
蓝澈112118 分钟前
迪杰斯特拉算法之解决单源最短路径问题
java·数据结构
贾全32 分钟前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天1 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
满分观察网友z1 小时前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
Natsume17101 小时前
嵌入式开发:GPIO、UART、SPI、I2C 驱动开发详解与实战案例
c语言·驱动开发·stm32·嵌入式硬件·mcu·架构·github
shaun20012 小时前
华为c编程规范
c语言
MeshddY2 小时前
(超详细)数据库项目初体验:使用C语言连接数据库完成短地址服务(本地运行版)
c语言·数据库·单片机
森焱森2 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天2 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid