【数据结构】顺序表的基本实现(C语言实现)

一、线性表概念

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储.

二、顺序表概念

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

三、顺序表接口的实现

1.顺序表的定义

使用结构体来构造一个顺序表

typedef int Datatype;//在后续修改数据类型方便

typedef struct SeqList
{
	Datatype* a;//定义顺序表中元素类型的数组指针
	int size;//当前实际存储数量
	int capacity;//有多大存储空间
}SL;

2.顺序表的初始化

顺序表的初始化是使用动态分配数组空间方式构造一个空的线性表。

void SLInit(SL* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

3.顺序表销毁

因为这里存储数据是使用动态方式开辟的空间,所以结束时需要进行释放,并把其他的值置为0。

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

4.打印顺序表数据

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

5.顺序表扩容(动态)

这里使用动态开辟空间,更加的灵活。后续我们会插入元素,如果空间不够,则使用realloc函数扩容。newcapacitv是扩容后的内存空间,tmp是指向这个·新的空间的指针。如果空间为0,则扩容可以放置4个元素的空间,如果空间已满,则在原来的空间基础上,在增加1倍。

void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)//进行判断是否需要扩容
	{
		int newcapacity=ps->size== 0 ? 4: ps->capacity * 2;
		Datatype* temp = (Datatype*)realloc(ps->a, sizeof(Datatype) *newcapacity);
		if (temp == NULL)//判断是否扩容成功
		{
			perror("realloc");
		}
		ps->capacity = newcapacity;
		ps->a = temp;
	}
}

6.顺序表尾插

这里需要调用一下扩容函数,防止空间不够。

void SLPushBack(SL* ps, Datatype x)
{
	assert(ps);
	SLCheckCapacity(ps);//调用扩容函数
	ps->a[ps->size] = x;
	ps->size++;
}

7.顺序表尾删

需要判断一下size是否为0,当为0时是不能在删除数据的。

void SLPopBack(SL* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		perror("SLPopBack");
	}
	ps->size--;
}

8.顺序表头插

同样是需要判断一下是否需要扩容,在头插时后面的数据都需要向后移一位。

void SLPushFront(SL* ps, Datatype x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size - 1; i >= 0; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->size++;
}

9.顺序表头删

头删只需将后面的数据整体向前移一位,然后size–就可以了。

void SLPopFront(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

10.顺序表任意位置插入(包括头尾操作)

需要找到指定位置,将指定位置及其以后的数据都向后移,需要先进行一下断言,进行扩容判断,在这里我先进行了判断是否为尾插,不是尾插在进行指定位置插入。

void SLInsert(SL* ps, int pos, Datatype x)
{
	assert(ps);
	SLCheckCapacity(ps);
	if (pos > 0 && pos <=ps->size+1)
	{
		if (pos == ps->size)//尾插
		{
			ps->a[ps->size + 1] = x;
			ps->size++;
		}
		else
		{
			for (int i = ps->size - 1; i >= pos - 1; i--)//任意位置插入
			{
				ps->a[i + 1] = ps->a[i];
			}
			ps->a[pos - 1] = x;
			ps->size++;
		}
		
	}
	else
	{
		perror("SLinsert");
	}
}

11.顺序表任意位置删除(包括头尾操作)

判断一下指定位置是否超出范围,然后将指定位置后的数据都向前移一位。

void SLErase(SL* ps, int pos)
{
	assert(ps);
	if (pos > 0 && pos <=ps->size)//判断范围
	{
		for (int i = pos; i <=ps->size; i++)
		{
			ps->a[i - 1] = ps->a[i];
		}
		ps->size--;
	}
	else
	{
		perror("SLerase");
	}
}

12.顺序表查找数据

查找数据,找到数据返回它的下标,如果没有找到返回-1

int SLFind(SL* ps, Datatype x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;//返回下标
		}
	}
	return -1;
}

13.顺序表修改信息

void SLModify(SL* ps, int pos, Datatype x)
{
	assert(ps);
	if (pos > 0 && pos <= ps->size)
	{
		ps->a[pos - 1] = x;
	}
	else
	{
		perror("SLModify");
	}
}

三、顺序表的结构(全部代码)

将顺序表分为三个部分SeqList.h SeqList.c test.c
SeqList.h(顺序表的类型定义、接口函数声明、引用的头文件)
SeqList.c(顺序表接口函数的实现)
test.c(主函数、测试顺序表各个接口功能)

1.SeqList.h

SeqList.h(顺序表的类型定义、接口函数声明、引用的头文件)

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int Datatype;

typedef struct SeqList//构造顺序表
{
	Datatype* a;
	int size;
	int capacity;
}SL;

void SLInit(SL* ps);//初始化
void SLPrint(SL* ps);//打印
void SLCheckCapacity(SL* ps);//扩容
void SLPushBack(SL* ps, Datatype x);//尾插
void SLPopBack(SL* ps);//尾删
void SLPushFront(SL* ps, Datatype x);//头插
void SLPopFront(SL* ps);//头删
void SLDestroy(SL* ps);//销毁
void SLInsert(SL* ps, int pos, Datatype x);//指定位置插入
void SLErase(SL* ps, int pos);//指定位置删除
int SLFind(SL* ps, Datatype x);//查找数据,返回下标
void SLModify(SL* ps, int pos, Datatype x);//修改数据

2.SeqList.c

SeqList.c(顺序表接口函数的实现)

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void SLInit(SL* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

void SLDestroy(SL* ps)
{
	assert(ps);
	ps->capacity = ps->size = 0;
	free(ps->a);
	ps->a = NULL;
}

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

void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newcapacity=ps->size== 0 ? 4: ps->capacity * 2;
		Datatype* temp = (Datatype*)realloc(ps->a, sizeof(Datatype) *newcapacity);
		if (temp == NULL)
		{
			perror("realloc");
		}
		ps->capacity = newcapacity;
		ps->a = temp;
	}
}


void SLPushBack(SL* ps, Datatype x)
{
	assert(ps);
	SLCheckCapacity(ps);//*****(&ps);
	ps->a[ps->size] = x;
	ps->size++;
}

void SLPopBack(SL* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		perror("SLpopback");
	}
	ps->size--;
}

void SLPushFront(SL* ps, Datatype x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size - 1; i >= 0; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->size++;
}

void SLPopFront(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

void SLInsert(SL* ps, int pos, Datatype x)
{
	assert(ps);
	SLCheckCapacity(ps);
	if (pos > 0 && pos <=ps->size+1)
	{
		if (pos == ps->size)//尾插
		{
			ps->a[ps->size + 1] = x;
			ps->size++;
		}
		else
		{
			for (int i = ps->size - 1; i >= pos - 1; i--)
			{
				ps->a[i + 1] = ps->a[i];
			}
			ps->a[pos - 1] = x;
			ps->size++;
		}
		/*for (int i = ps->size - 1; i >= pos-1; i--)
		{
			ps->a[i + 1] = ps->a[i];
		}
		ps->a[pos-1] = x;
		ps->size++;*/
	}
	else
	{
		perror("SLinsert");
	}
}

void SLErase(SL* ps, int pos)
{
	assert(ps);
	if (pos > 0 && pos <=ps->size)
	{
		for (int i = pos; i <=ps->size; i++)
		{
			ps->a[i - 1] = ps->a[i];
		}
		ps->size--;
	}
	else
	{
		perror("SLerase");
	}
}

int SLFind(SL* ps, Datatype x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SLModify(SL* ps, int pos, Datatype x)
{
	assert(ps);
	if (pos > 0 && pos <= ps->size)
	{
		ps->a[pos - 1] = x;
	}
	else
	{
		perror("SLModify");
	}
}

3.test.c

test.c(主函数、测试顺序表各个接口功能)
下面的代码对顺序表的功能进行简单的测试。

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
int main()
{
	SL ps;
	SLInit(&ps);
	SLPushBack(&ps,1);
	//SLPopBack(&ps);
	SLPushFront(&ps,2);
	SLPushFront(&ps, 3);
	SLPushFront(&ps, 4);
	SLPushFront(&ps, 5);
	SLInsert(&ps, 6, 9);
	SLInsert(&ps,1 , 9);
	SLErase(&ps,7);
	SLErase(&ps, 1);
	//SLModify(&ps, 2, 1);
	//SLPopFront(&ps);
	//SLPopFront(&ps);
	SLPrint(&ps);
	//printf("%d", SLFind(&ps, 9));
	return 0;
}
最后:文章有什么不对的地方或者有什么更好的写法欢迎大家在评论区指出
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值