数据结构--顺序表(C语言版)

本文详细介绍了顺序表的概念,包括其在数据结构中的作用,以及静态和动态顺序表的区别。并提供了C语言实现动态顺序表的代码示例,涉及初始化、销毁、插入、删除和查找等操作。
摘要由CSDN通过智能技术生成

(一).顺序表的概念及结构

首先,我们来了解一下什么是数据结构呢?

数据结构是顾明思义就是由数据+结构。

常⻅的数值1、2、3、4.....、教务系统⾥保存的⽤⼾信息(姓名、性别、年龄、学历等 等)、⽹⻚⾥⾁眼可以看到的信息(⽂字、图⽚、视频等等),这些就是数据

那结构是什么呢?当海量的数据没有按照顺序排列当我们查找起来就十分麻烦,例如:

上面这一组图片就能很好的说明结构的重要性当我们在左边圈里去找某一只小羊可想而知找到要花费多少时间,而当我们将每只小羊按照顺序排好序之后, 比如我们要找编号为十的这只小羊,我们就可以通过编号直接找到,这样便省了大量的时间。

故结构就是:当我们想要使⽤⼤量使⽤同⼀类型的数据时,通过⼿动定义⼤量的独⽴的变量对于程序来说,可读性 ⾮常差,我们可以借助数组这样的数据结构将⼤量的数据组织在⼀起,结构也可以理解为组织数据的⽅式。

数据结构:数据结构是计算机存储,组织数据的方式。

 接下来我们来了解一下顺序表的概念是什么呢?

顺序表就是一群数据按照有顺序的方式进行排列,这样便于查找。

顺序表其实就是线性表的其中一种。

首先,我们要来了解线性表的概念,

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是⼀种在实际中⼴泛使 ⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...。

线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。

(二).顺序表的分类

顺序表分为静态顺序表动态顺序表两种:

静态顺序表:

这里就是一个静态顺序表,它的结构体数组大小是确定的,而这样就会导致一个缺点 ,空间给少了就不够用,空间给多了就会造成空间浪费。而我们的动态顺序表就能很好的解决这个问题。

动态顺序表:

这里就是动态顺序表,它相比于静态顺序表就是可动态的申请空间,按需申请空间这样就不会造成空间浪费,也不会出现空间不足的现象。

(三).动态顺序表的实现

上面我们了解了顺序表的概念和顺序表的分类,接下来进入代码阶段实现动态顺序表。

3.1创建结构体 

typedef int SLDatatype;
typedef struct SqList {
	SLDatatype* arr;
	int size;
	int capecity;
}SL;

3.2初始化顺序表

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

3.3销毁顺序表

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

3.4打印顺序表 

//打印代码
void SLPrint(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.arr[i]);
	}
	printf("\n");
}

3.5头插尾插

尾插:

//尾部插入
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	//插入数据之前首先要判断空间够不够
	if (ps->size == ps->capecity)//如果当前空间已经满了的话就用relloc增容
	{
		//使用三目表达式,为使现有空间成倍的增加
		int newcapecity = ps->capecity == 0 ? 4 : 2 * ps->capecity;
		SLDataType* tmp = (SLDataType*)relloc(ps->arr, newcapecity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capecity = newcapecity;
	}
	ps->arr[ps->size] = x;//在顺序表中尾插一个数据;
	ps->size++;
}

 头插:

//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	//插入数据之前首先要判断空间够不够
	if (ps->size == ps->capecity)//如果当前空间已经满了的话就用relloc增容
	{
		//使用三目表达式,为使现有空间成倍的增加
		int newcapecity = ps->capecity == 0 ? 4 : 2 * ps->capecity;
		SLDataType* tmp = (SLDataType*)relloc(ps->arr, newcapecity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capecity = newcapecity;
	}
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

3.6头删尾删

尾删:

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

头删:

//头部删除
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	--ps->size;
}

 

3.7在任意位置插入

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);//断言插入位置是否合理
	//插入数据之前首先要判断空间够不够
	if (ps->size == ps->capecity)//如果当前空间已经满了的话就用relloc增容
	{
		//使用三目表达式,为使现有空间成倍的增加
		int newcapecity = ps->capecity == 0 ? 4 : 2 * ps->capecity;
		SLDataType* tmp = (SLDataType*)relloc(ps->arr, newcapecity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capecity = newcapecity;
	}
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

3.8在任意位置删除

//在任意位置删除
void SLErase(SL* ps, int pos)
{
	assert(pos);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i+1];
	}
	ps->size--;
}

3.9查找数据

//查找数据
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

汇总

SqList.h:

#pragma once
//SeqList.h进行顺序表结构的声明
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//创建结构体
//静态结构体
//struct seqlist 
//{
//	int arr[100];
//	int size;
//};

typedef int SLDataType;
//动态结构体
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效数据的个数
	int capacity;//顺序表的大小
}SL;

//初始化顺序表
void SLInit(SL* ps);
//销毁顺序表
void SLDestroy(SL* ps);
//打印代码
void SLPrint(SL ps);

//头部插⼊删除 / 尾部插⼊删除

//尾部插入
void SLPushBack(SL* ps, SLDataType x);
//头部插入
void SLPushFront(SL* ps, SLDataType x);

//头部删除
void SLPopFront(SL* ps);
//尾部删除
void SLPopBack(SL* ps);
//任意位置插入
void SLInsert(SL* ps, int pos, SLDataType x);
//任意位置删除
void SLErase(SL* ps, int pos);
//查找数据
int SLFind(SL* ps, SLDataType x);

SqList.c:

//进行顺序表的实现
#include"SeqList.h";
//顺序表初始化
void SLInit(SL* s)
{
	s->arr = NULL;
	s->size = s->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps) 
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

//判断内存空间够不够
void SLCheckcapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		//三目表达式
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		//对顺序表进行成倍的扩充
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("ralloc fail!");
			return 1;
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	//首先要判断空间够不够
	
	assert(ps);//断言ps不为空指针
	
	SLCheckcapacity(ps);
	/*ps->arr[ps->size] = x;
	ps->size++;*/
	ps->arr[ps->size++] = x;
}


//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckcapacity(ps);
	for (int i = ps->size;i>0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}
//打印
void SLPrint(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.arr[i]);
	}
	printf("\n");
}
//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->arr[ps->size - 1] = -1;
	--ps->size;
}

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

//任意位置插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckcapacity(ps);
	for (int 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);
	for (int i = pos; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}


//查找数据
int SLFind(SL* ps, SLDataType x) 
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

test.c:

//进行顺序表的测试
#include"SeqList.h";

SLtest01()
{
	SL s1;
	//初始化顺序表
	SLInit(&s1);
	//尾插
    SLPushBack(&s1, 1);
	SLPushBack(&s1, 2);
	SLPushBack(&s1, 3);
	SLPushBack(&s1, 4);
	SLPushBack(&s1, 5);
	SLPrint(s1);
	//头插
	//SLPushFront(&s1, 9); 
	//SLPushFront(&s1, 8);
	//SLPrint(s1);

	//尾删
	//SLPopFront(&s1);
	//SLPrint(s1);

	//头删
	//SLPopBack(&s1);
	//SLPrint(s1);

	//在任意位置插入
	//SLInsert(&s1,2,88);
	//SLPrint(s1);

	//在任意位置删除
	//SLErase(&s1, 2);
	//SLPrint(s1);

	//查找数据
	//int ret = SLFind(&s1, 3);
	//printf("找到了,它的下标为%d\n", ret);



	//销毁顺序表
	SLDestroy(&s1);
}



int main()
{
	SLtest01();

	return 0;
}

好了,各位老铁以上就是我跟大家分享的顺序表的全部内容,觉得小编写的还不错的话,记得给小编留下免费的三连哦!谢谢大家!

  • 25
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值