数据结构——动态顺序表(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;
 }
相关推荐
Wnq100729 分钟前
工业场景轮式巡检机器人纯视觉识别导航的优势剖析与前景展望
人工智能·算法·计算机视觉·激光雷达·视觉导航·人形机器人·巡检机器人
天上路人2 小时前
AI神经网络降噪算法在语音通话产品中的应用优势与前景分析
深度学习·神经网络·算法·硬件架构·音视频·实时音视频
好吃的肘子2 小时前
MongoDB 应用实战
大数据·开发语言·数据库·算法·mongodb·全文检索
汉克老师2 小时前
GESP2025年3月认证C++二级( 第三部分编程题(1)等差矩阵)
c++·算法·矩阵·gesp二级·gesp2级
sz66cm3 小时前
算法基础 -- 小根堆构建的两种方式:上浮法与下沉法
数据结构·算法
緈福的街口3 小时前
【leetcode】94. 二叉树的中序遍历
算法·leetcode
顾小玙3 小时前
数据结构进阶:AVL树与红黑树
数据结构
小刘要努力呀!3 小时前
嵌入式开发学习(第二阶段 C语言基础)
c语言·学习·算法
草莓熊Lotso3 小时前
【C语言字符函数和字符串函数(一)】--字符分类函数,字符转换函数,strlen,strcpy,strcat函数的使用和模拟实现
c语言·开发语言·经验分享·笔记·其他
野曙4 小时前
快速选择算法:优化大数据中的 Top-K 问题
大数据·数据结构·c++·算法·第k小·第k大