数据结构:顺序表的实现

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//学号:12023244754 张莹莹 软云一班 
//定义动态顺序表
typedef int SLDatatype;
typedef struct SeqLlist {
	SLDatatype* arr;
	int capacity;
	int size;
}SL;

//初始化
void SLInit(SL* ps);

//销毁
void SLDestory(SL* ps);

//尾插入数据
void SLPushBack(SL* ps, SLDatatype x);

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

//头插
void SLPushFront(SL* ps, SLDatatype x);

//删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);

//在指定位置之前插入数据
void SLInsert(SL* ps, SLDatatype x, int pos);

//删除指定位置的数据
void SLErase(SL* ps, int pos);

//查找数据
int SLFind(SL* ps, SLDatatype x);

//初始化
//错误1:使用了未初始化的局部变量“s”
//void SLInit(SL s)--值传递
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

//判断空间是否充足
void SLCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity) {
		//增容0*2=0
		//若capacity为0,给个默认值,否则*2
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDatatype* tmp = (SLDatatype*)realloc(ps->arr, newCapacity * sizeof(SLDatatype));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}

//尾插
void SLPushBack(SL* ps, SLDatatype x) {
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;
}

//打印操作
void SLPrint(SL* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

//头插
void SLPushFront(SL* ps, SLDatatype x) {
	assert(ps);
	//判断空间是否足够
	SLCheckCapacity(ps);
	//数据整体后移动1位
	int i;
	for (i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	//下标为0的位置空出来
	ps->arr[0] = x;
	ps->size++;
}

//销毁
void SLDestory(SL* ps)
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

//删除
void SLPopBack(SL* ps) {
	assert(ps);
	assert(ps->size);
	ps->arr[ps->size - 1] = 0;
	ps->size--;
}

void SLPopFront(SL* ps) {
	assert(ps);
	assert(ps->size);
	//数据整体向前挪动一个位置
	int i = 0;
	for (i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//在指定位置之前插入数据(空间足够才能插入数据)
void SLInsert(SL* ps, SLDatatype x, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	//pos之后的数据向后移动一位
	int i;
	for (i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

//删除指定位置的数据
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <=ps->size);
	//pos之后的数据整体向前移动1位
	int i;
	for (i = pos ; i < ps->size; i++)
	{
		ps->arr[i-1] = ps->arr[i];
	}
	ps->size--;
}

//查找数据
int SLFind(SL* ps, SLDatatype x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] = x)
		{
			return i;
		}
	}
	//没有找到,返回一个无效的下标
	return -1;
}

void test()
{
	SL s;
	SLInit(&s);
	SLPushBack(&s, 1);
	SLPushBack(&s, 2);
	SLPushBack(&s, 3);
	SLPushBack(&s, 4);//1 2 3 4
	SLPrint(&s);
	SLPushFront(&s, 1);
	SLPushFront(&s, 2);
	SLPrint(&s);//2 1 1 2 3 4 
	SLPopBack(&s);
	SLPopBack(&s);
	SLPrint(&s);//2 1 1 2

	//头删除
	SLPopFront(&s);
	SLPrint(&s);// 1 1 2

	//插入 
	SLInsert(&s, 11, 0);
	SLInsert(&s, 22, s.size);
	SLPrint(&s);//11 1 1 2 22

	//SLErase指定位置删除
	SLErase(&s,4);

	SLPrint(&s);// 11 1 1 22

	//查找数据
	int find = SLFind(&s, 2);
	if (find >= 0)
	{
		printf("找到了\n");
	}
	else
	{
		printf("没有找到\n");
	}

	//销毁 
	//SLDestory(&s);
}
int main()
{
	test();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值