【c语言】动态顺序表的实现

很基础的函数代码。

相关代码:

SeqList.h头文件的代码:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

//动态顺序表:按需申请
typedef int SLtype;

typedef struct SeqList
{
	SLtype* atr;
	int size;//有效数据个数
	int capacity;//空间容量
}SL;

//顺序表的初始化
void SLInit(SL* ps);

//顺序表的销毁
void SLDestroy(SL* ps);

//顺序表的打印
void SLPrint(SL* ps);

//插入数据之前先判断空间够不够:顺序表的扩容
void SLCheckCapacity(SL* ps);

//顺序表的尾插
void SLPushBack(SL* ps, SLtype x);

//顺序表的头插
void SLPushFront(SL* ps, SLtype x);

//顺序表的尾删
void SLPopBack(SL* ps);

//顺序表的头删
void SLPopFront(SL* ps);

//顺序表指定位置插入
void SLInsert(SL* ps, int pos, SLtype x);

//顺序表指定位置删除
void SLErase(SL* ps, int pos);

//顺序表的查找
int SLFind(SL* ps, SLtype x);

SeqList.c源文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

//顺序表的初始化
void SLInit(SL* ps)
{
	ps->atr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

//顺序表的销毁
void SLDestroy(SL* ps)
{
	assert(ps);
	if (ps->atr != NULL)
	{
		free(ps->atr);
	}
	ps->atr = NULL;
	ps->size = ps->capacity = 0;
}

//顺序表的打印
void SLPrint(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", *(ps->atr  + i));
	}
	printf("\n");
	printf("size:%d\n",ps->size );
	printf("capacity:%d\n", ps->capacity);

}

//插入数据之前先判断空间够不够:顺序表的扩容
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLtype* ptr = (SLtype*)realloc(ps->atr, sizeof(SLtype) * newcapacity);
		if (ptr != NULL)
		{
			ps->atr = ptr;
		}
		ps->capacity = newcapacity;
		ptr = NULL;
	}
	
}


//顺序表的尾插
void SLPushBack(SL* ps, SLtype x)
{
	assert(ps);
	//插入数据之前先判断空间够不够:扩容
	SLCheckCapacity(ps);
	//ps->atr[ps->size] = x;
	//ps->size++;
	ps->atr[ps->size++] = x;

}

//顺序表的头插
void SLPushFront(SL* ps, SLtype x)
{
	assert(ps);
   //插入数据之前先判断空间够不够:扩容
	SLCheckCapacity(ps);
	int i = 0;
	for ( i = ps->size ; i > 0 ; i-- )
	{
		ps->atr[i] = ps->atr[i - 1];//atr[1]=atr[0];
	}
	ps->atr[0] = x;
	ps->size++;
}


//顺序表的尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	ps->size--;
}

//顺序表的头删
void SLPopFront(SL* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size - 1; i++)
	{
		ps->atr[i] = ps->atr[i + 1];//atr[size - 2]=atr[size - 1]
	}
	ps->size--;

}


//顺序表指定位置插入
void SLInsert(SL* ps, int pos, SLtype x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
 //插入数据之前先判断空间够不够:扩容
	SLCheckCapacity(ps);

	int i = 0;
	for (i = ps->size ; i>pos ; i--)
	{
		ps->atr[i] = ps->atr[i - 1];//atr[pos+1]=atr[pos]
	}
	ps->atr[pos] = x;
	ps->size++;
}

//顺序表指定位置删除
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int i = 0;
	for (i = pos;i<ps->size; i++)
	{
		ps->atr[i] = ps->atr[i + 1];//atr[size-1]=atr[size]
	}
	ps->size--;
}


//顺序表的查找
int SLFind(SL* ps, SLtype x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (x == ps->atr[i])//找到了
		{
			printf("找到了!坐标是:%d\n", i);
			return i;
		}
	}
	printf("找不到该坐标。\n");
	return -1;

}


int main()
{
	SL s1;
	SLInit(&s1);//初始化

	//增删查找
	//运用想要的功能......

	SLDestroy(&s1);//销毁

	return 0;
}

有错误欢迎指出,下一篇博客见~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值