数据结构(初阶)—— C语言实现顺序表

 

目录

一、顺序表十种接口实现

1.顺序表的初始化

2.顺序表的打印

3.顺序表的扩容函数 

4. 顺序表的尾插

5.顺序表的头插 

6.顺序表的尾删 

7.顺序表的头删 

8.顺序表在pos位置插入指定的数字x

9.顺序表删除pos位置的值 

10.顺序表的查找和销毁 

二、源代码展示

1.SeqList.h 

2.SeqList.c 

3.test.c 


一、顺序表十种接口实现

typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* num;//存放数据
	int capacity;//记录容量
	int size;//记录存储数据的个数
}SL;

//顺序表的初始化
void SeqListInit(SL* pc);

//顺序表的打印
void SeqListPrint(SL* pc);

//顺序表得到尾插
void SeqListPushBack(SL* pc, SLDataType x);

//顺序表的头插
void SeqListPushFront(SL* pc, SLDataType x);

//顺序表的尾删
void SeqListPopBack(SL* pc);

//顺序表的头删
void SeqListPopFront(SL* pc);

//顺序表在pos位置插入指定x
void SeqListInsert(SL* pc, int pos, SLDataType x);

//顺序表删除pos位置的值
void SeqListErase(SL* pc, int pos);

//顺序表的查找
int SeqListFind(SL* pc, SLDataType x);

//顺序表的销毁
void SeqListDestroy(SL* pc);

        顺序表可以采用静态存储和动态存储的方式,一般是采用动态存储的方式,目的在于减少空间的浪费  

1.顺序表的初始化

顺序表的初始化 相对简单,我们定义了有一个顺序表的指针、 容量、数据的个数

//顺序表的初始化
void SeqListInit(SL* pc)
{
	pc->num = NULL;
	pc->capacity = pc->size = 0;
}

2.顺序表的打印

//顺序表的打印
void SeqListPrint(SL* pc)
{
	assert(pc);
	for (int i = 0; i < pc->size; ++i)
	{
		printf("%d ", pc->num[i]);
	}
	printf("\n");
}

3.顺序表的扩容函数 

        顺序表在存储数据时,需要空间的使用,我们这里采用的是动态存储,我们要保证每次存放数据时顺序表的容量是足够的,就需要动态开辟空间

//扩容函数
void SeqListCheckCapacity(SL* pc)
{
	if (pc->size == pc->capacity)
	{
        //当我们第一次插入数据时,顺序表的容量和数据个数都是0,可以采用三目操作符,先给它一块空间
		int newcapacity = pc->size == 0 ? 4 : pc->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(pc->num, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		pc->num = tmp;
		pc->capacity = newcapacity;
	}
}

 4. 顺序表的尾插

//顺序表的尾插
void SeqListPushBack(SL* pc, SLDataType x)
{
	assert(pc);
	//判断容量
	SeqListCheckCapacity(pc);
	pc->num[pc->size] = x;
	pc->size++;
	//SeqListInsert(pc, pc->size, x);这是指定位置插入的函数
	
}

5.顺序表的头插 

//顺序表的头插
void SeqListPushFront(SL* pc, SLDataType x)
{
	assert(pc);
	//判断容量
	SeqListCheckCapacity(pc);
	int end = pc->size - 1;
	while (end >= 0)
	{
		pc->num[end + 1] = pc->num[end];//把每个数向后挪一个位置
		end--;
	}
	pc->num[0] = x;
	pc->size++;
	//SeqListInsert(pc, 0, x);
}

6.顺序表的尾删 

//顺序表的尾删
void SeqListPopBack(SL* pc)
{
	assert(pc);
	pc->size--;
	//SeqListErase(pc, pc->size - 1);指定位置删除函数
}

 直接数据个数减一,让其访问不到

7.顺序表的头删 

//顺序表的头删
void SeqListPopFront(SL* pc)
{
	assert(pc);
	int begin = 1;
	while (begin < pc->size)
	{
		pc->num[begin - 1] = pc->num[begin];
		begin++;
	}
	pc->size--;
	//SeqListErase(pc, 0);
}

8.顺序表在pos位置插入指定的数字x

//顺序表在pos位置插入指定x
void SeqListInsert(SL* pc, int pos, SLDataType x)
{
	assert(pc);
	assert(pos >= 0 && pos <= pc->size);
	SeqListCheckCapacity(pc);
	int end = pc->size - 1;
	while (end >= pos)
	{
		pc->num[end + 1] = pc->num[end];
		end--;
	}
	pc->num[pos] = x;
	pc->size++;
}

        在头插接口中和这里的指定位置 插入功能一样,所以我们就可以在需要调用头插时直接转换为调用这里的函数,下面的删除也是一个道理;

9.顺序表删除pos位置的值 

//顺序表删除pos位置的值
void SeqListErase(SL* pc, int pos)
{
	assert(pc);
	assert(pos >= 0 && pos < pc->size);
	int begin = pos + 1;
	while (begin < pc->size)
	{
		pc->num[begin - 1] = pc->num[begin];
		begin++;
	}
	pc->size--;
}

10.顺序表的查找和销毁 

//顺序表的查找
int SeqListFind(SL* pc, SLDataType x)
{
	assert(pc);
	for (int i = 0; i < pc->size; ++i)
	{
		if (pc->num[i] == x)
		{
			return i;
		}
	}
	return -1;
}

//顺序表的销毁
void SeqListDestroy(SL* pc)
{
	free(pc->num);
	pc->num = NULL;
	pc->capacity = pc->size = 0;
}

二、源代码展示

1. SeqList.h 

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

typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* num;
	int capacity;
	int size;
}SL;

//顺序表的初始化
void SeqListInit(SL* pc);

//顺序表的打印
void SeqListPrint(SL* pc);

//顺序表得到尾插
void SeqListPushBack(SL* pc, SLDataType x);

//顺序表的头插
void SeqListPushFront(SL* pc, SLDataType x);

//顺序表的尾删
void SeqListPopBack(SL* pc);

//顺序表的头删
void SeqListPopFront(SL* pc);

//顺序表在pos位置插入指定x
void SeqListInsert(SL* pc, int pos, SLDataType x);

//顺序表删除pos位置的值
void SeqListErase(SL* pc, int pos);

//顺序表的查找
int SeqListFind(SL* pc, SLDataType x);

//顺序表的销毁
void SeqListDestroy(SL* pc);

2.SeqList.c 

#include "SeqList.h"

//顺序表的初始化
void SeqListInit(SL* pc)
{
	pc->num = NULL;
	pc->capacity = pc->size = 0;
}

//顺序表的打印
void SeqListPrint(SL* pc)
{
	assert(pc);
	for (int i = 0; i < pc->size; ++i)
	{
		printf("%d ", pc->num[i]);
	}
	printf("\n");
}

//扩容函数
void SeqListCheckCapacity(SL* pc)
{
	if (pc->size == pc->capacity)
	{
		int newcapacity = pc->size == 0 ? 4 : pc->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(pc->num, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		pc->num = tmp;
		pc->capacity = newcapacity;
	}
}

//顺序表的尾插
void SeqListPushBack(SL* pc, SLDataType x)
{
	/*assert(pc);*/
	//判断容量
	//SeqListCheckCapacity(pc);
	//pc->num[pc->size] = x;
	//pc->size++;
	SeqListInsert(pc, pc->size, x);
	
}

//顺序表的头插
void SeqListPushFront(SL* pc, SLDataType x)
{
	//assert(pc);
	判断容量
	//SeqListCheckCapacity(pc);
	//int end = pc->size - 1;
	//while (end >= 0)
	//{
	//	pc->num[end + 1] = pc->num[end];//把每个数向后挪一个位置
	//	end--;
	//}
	//pc->num[0] = x;
	//pc->size++;
	SeqListInsert(pc, 0, x);
}

//顺序表的尾删
void SeqListPopBack(SL* pc)
{
	//assert(pc);
	//pc->size--;
	SeqListErase(pc, pc->size - 1);
}

//顺序表的头删
void SeqListPopFront(SL* pc)
{
	//assert(pc);
	//int begin = 1;
	//while (begin < pc->size)
	//{
	//	pc->num[begin - 1] = pc->num[begin];
	//	begin++;
	//}
	//pc->size--;
	SeqListErase(pc, 0);
}

//顺序表在pos位置插入指定x
void SeqListInsert(SL* pc, int pos, SLDataType x)
{
	assert(pc);
	assert(pos >= 0 && pos <= pc->size);
	SeqListCheckCapacity(pc);
	int end = pc->size - 1;
	while (end >= pos)
	{
		pc->num[end + 1] = pc->num[end];
		end--;
	}
	pc->num[pos] = x;
	pc->size++;
}

//顺序表删除pos位置的值
void SeqListErase(SL* pc, int pos)
{
	assert(pc);
	assert(pos >= 0 && pos < pc->size);
	int begin = pos + 1;
	while (begin < pc->size)
	{
		pc->num[begin - 1] = pc->num[begin];
		begin++;
	}
	pc->size--;
}

//顺序表的查找
int SeqListFind(SL* pc, SLDataType x)
{
	assert(pc);
	for (int i = 0; i < pc->size; ++i)
	{
		if (pc->num[i] == x)
		{
			return i;
		}
	}
	return -1;
}

//顺序表的销毁
void SeqListDestroy(SL* pc)
{
	free(pc->num);
	pc->num = NULL;
	pc->capacity = pc->size = 0;
}

3.test.c 

#include "SeqList.h"

void Test1()
{
	SL p;
	SeqListInit(&p);
	//尾插
	SeqListPushBack(&p, 1);
	SeqListPushBack(&p, 2);
	SeqListPushBack(&p, 3);
	SeqListPushBack(&p, 4);
	SeqListPushBack(&p, 5);
	SeqListPrint(&p);
	//头插
	SeqListPushFront(&p, 1);
	SeqListPushFront(&p, 2);
	SeqListPushFront(&p, 3);
	SeqListPushFront(&p, 4);
	SeqListPushFront(&p, 5);
	SeqListPrint(&p);
	//尾删
	SeqListPopBack(&p);
	SeqListPopBack(&p);
	SeqListPrint(&p);
	//头删
	SeqListPopFront(&p);
	SeqListPopFront(&p);
	SeqListPrint(&p);
	//在pos位置插入,我们在下标为2的位置插入20
	SeqListInsert(&p, 2, 20);
	SeqListPrint(&p);
	//删除pos位置的值,我们删除下标为2中的值20
	SeqListErase(&p, 2, 20);
	SeqListPrint(&p);
	
}

void Test2()
{
	SL p;
	SeqListInit(&p);
	//尾插
	SeqListPushBack(&p, 1);
	SeqListPushBack(&p, 2);
	SeqListPushBack(&p, 3);
	SeqListPushBack(&p, 4);
	SeqListPushBack(&p, 5);
	SeqListPrint(&p);
	SeqListPopBack(&p);
	SeqListPrint(&p);
	SeqListPopFront(&p);
	SeqListPrint(&p);
	SeqListPushBack(&p, 10);
	SeqListPrint(&p);
	SeqListPushFront(&p, 5);
	SeqListPrint(&p);

	
}

int main()
{
	//Test1();
	Test2();
	return 0;
}

  • 17
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霄沫凡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值