数据结构顺序表的实现

SeqList.c

#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"

//顺序标初始化
void SLInit(SL*ps)
{
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
//尾插
void SLPushBack(SL* ps, SLDateType x)
{
//		SLCheckCapacity(ps);
//
//	//正常插入
//	ps->a[ps->size] = x;
//	ps->size++;
 SLInsert(ps, ps->size, x);


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

}
//销毁空间
void SLDestroy(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;

}
//尾删
void SLPopBack(SL* ps)
{
	//有没有置零的可以
	//ps->a[ps->size - 1] = 0;
	//ps->size--;

	//只有这个过量删减发现再插入程序崩溃数组越界不会自己检测
	//ps->size--;

	//1
	//if (ps->size > 0)
	//{
	//	ps->size--;
	//}
	//2
	/*assert(ps->size > 0);
	ps->size--;*/
	SLErase(ps, ps->size - 1);
}
//检测扩容
void SLCheckCapacity(SL* ps)
{
	//满了/空
	if (ps->size == ps->capacity)
	{
		//扩容*2
		//int newcapacity = ps->capacity * 2;
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		//判断是否成功扩容
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}
//头插
void SLPushFront(SL* ps, SLDateType x)
{
	//SLCheckCapacity(ps);
	挪动数据
	//int end = ps->size - 1;
	//while (end >= 0)
	//{
	//	ps->a[end + 1] = ps->a[end];
	//	--end;
	//}
	//ps->a[0] = x;
	//ps->size++;
	SLInsert(ps, 0, x);

}
//头删
void SLPopFront(SL* ps)
{
	/*assert(ps->size > 0);
	int begain = 1;
	while (begain<ps->size)
	{
		ps->a[begain - 1] = ps->a[begain];
		begain++;
	}*/
	SLErase(ps, 0);

}
//查找--给值找位置
SLDateType SLFind(SL* ps, SLDateType x)
{
	 int i = 0;
	
	for ( i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			printf("第%d个 ", i);
			//continue;
			
			return i;
		}
	}
	
	return -1;
}
//插入--任意位置(但要满足顺序表规则)
void SLInsert(SL* ps, int pos, SLDateType x)
{
	assert(pos <= ps->size && pos >= 0);//不能比0还小比size还大
	//检查内存
	SLCheckCapacity(ps);
	//挪动数据
	int end = ps->size - 1;
	while (end > pos-1)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
//删除任意位置
void SLErase(SL* ps, int pos)
{
	assert(pos >= 0 && pos < ps->size);
	int begain = pos+1;
	while (begain<ps->size)
	{
		ps->a[begain - 1] = ps->a[begain];
		begain++;
	}
	ps->size--;
}

SeqList.h

#pragma once
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
//头文件
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//
typedef int SLDateType;
//宏

//全局变量

//动态顺序表
typedef struct SeqList
{
	SLDateType* a;//起始位置
	int size;//存了多少个
	int capacity;//容量
}SL;

//函数声明
//顺序标初始化
void SLInit(SL*ps);
//尾插
void SLPushBack(SL* ps, SLDateType x);
//打印
void SLprintf(SL* ps);
//释放/销毁
void SLDestroy(SL* ps);
//尾删
void SLPopBack(SL* ps);
//检测扩容
void SLCheckCapacity(SL* ps);
//头插
void SLPushFront(SL* ps, SLDateType x);
//头删
void SLPopFront(SL* ps);
//查找--给值找位置
SLDateType SLFind(SL* ps, SLDateType x);
//插入--任意位置(但要满足顺序表规则)
void SLInsert(SL* ps, int pos, SLDateType x);
//删除任意位置
void SLErase(SL* ps, int pos);
#endif

test.c

#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void testSeqList()
{
	SL s1;
	SLInit(&s1); 
	SLPushBack(&s1, 1);
	SLPushBack(&s1, 2);
	SLPushBack(&s1, 3);
	SLPushBack(&s1, 4);
	SLPushBack(&s1, 5);
	SLprintf(&s1 );
	SLPushFront(&s1, 8);
	SLPushFront(&s1, 8);
	SLPushFront(&s1, 8);
	SLPushFront(&s1, 8);
	SLprintf(&s1);
	SLInsert(&s1, 1, 4);
	SLInsert(&s1, 1, 4);
	SLInsert(&s1, 1, 4);
	SLprintf(&s1);
	


	SLDestroy(&s1);


}
int main()
{

	testSeqList();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值