(数据结构)顺序表

本文介绍了顺序表的概念,包括线性表与顺序表的定义,以及它们与数组的区别。接着详细阐述了动态顺序表的实现,包括初始化、销毁、打印、扩容、尾插、尾删、头插、头删、查找和插入删除操作。同时,提供了C语言的实现代码示例。
摘要由CSDN通过智能技术生成

 

目录

1.顺序表的原理

1.1 什么是线性表

1.2 什么是顺序表

1.3 线性表和数组的区别

2.顺序表的实现(动态的版本)

2.1 分析

2.2 初始化 和 销毁创建的空间

2.3 打印 和 扩容

2.4 尾插 和 尾删

2.5 头插 和 头删

2.6 顺序表查找

2.7 顺序表在pos位置插入x

2.8 顺序表删除pos位置的值

2.9 顺序表的完整实现

3.实际运行状况


1.顺序表的原理

1.1 什么是线性表

线性表是n个具有相同特性的数组元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表,栈,队列,字符串........

线性表在逻辑上是线性结构,也就是说是连续的一条直线,线性结构存储数据时是挨着挨着存储的,但是在物理结构上并不一定是连续的。线性表在物理上存储时,通常以数组和链式结构的形式存储(线性表有两种存储方式,顺序存储和链式存储(不要求连续存放)

 顺序表:

链表:

1.2 什么是顺序表

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,当线性表采用顺序的存储方式存储就称之为顺序表,顺序表是把表中的结点依次存放在内存的一块连续的空间当中,一般情况下采用数组存储,在数组上完成数据的增删查改。

顺序表一般可以分为:1.静态的顺序表(使用定长数组存储元素) 2.动态的顺序表(使用动态开辟的数组存储)

1.3 线性表和数组的区别

在逻辑上,线性表中的数据是依次的存放到一块连续的空间当中,最终在逻辑上是呈现连续的一条直线,而数组虽然也是开辟一块连续的空间,但是它存储的数据可以不是依次存放的,它可以存放到这块连续区域中的任意位置,数组也可以理解成是在一块连续的空间中,一条段了的线。

在物理结构上,线性表它可以采用链式存储的方式去存储 ,因此此时在物理空间上可以是不连续的(不连续的结点用指针联系了起来,在逻辑上去理解时,也可以看成一条线),而数组用栈声明的时候很明显,都是连续的。

2.顺序表的实现(动态的版本)

2.1 分析

 size表示当前存储的元素个数,capacity表示当前开辟的空间总数(通过这2个变量去控制动态开辟空间,以此使顺序表达到动态的开辟空间的效果)

指针a始终指向了动态空间的首地址

2.2 初始化 和 销毁创建的空间

初始化:

void SeqListInit(SeqList* ps)
{
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

 销毁创建的空间:

void SeqListDestroy(SeqList* ps)
{
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
	}
}

2.3 打印 和 扩容

打印:

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

 扩容:

void SLCheckCapacity(SeqList* ps)
{
	//检查空间是否满了,满了则扩容
	if (ps->size == ps->capacity)
	{
		int nemCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, nemCapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = nemCapacity;
	}
}

2.4 尾插 和 尾删

尾插:

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	//检查容量空间,满了则扩容
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//4 或者 2倍的空间(个数)
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newCapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
	}
	//空间每满就存入一个元素到结构体中,size++
	ps->a[ps->size] = x;//尾插
	ps->size++;
}

尾删:

void SeqListPopBack(SeqList* ps)
{
	assert(ps->size > 0);

	ps->size--;
}

2.5 头插 和 头删

头插;

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);

	SLCheckCapacity(ps);//检查扩容
	//挪动数据,有数据时头插
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	//没数据时头插
	ps->a[0] = x;
	ps->size++;
}

头删:

void SeqListPopFront(SeqList* ps)
{
	assert(ps->size > 0);

	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
        ++begin;
	}
	ps->size--;
}

2.6 顺序表查找

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

2.7 顺序表在pos位置插入x

void SeqListInsert(SeqList * ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	SLCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}

2.8 顺序表删除pos位置的值

void SeqListErase(SeqList * ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int begin = pos;
	while (begin < ps->size - 1)
	{
		ps->a[begin] = ps->a[begin + 1];
		++begin;
	}
	ps->size--;
}

2.9 顺序表的完整实现

头文件部分:

#pragma once

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

typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;//指向动态数组的首地址,一个数组指针,一个指向动态数组的指针
	//判断是否要扩容
	int size;//数组的个数
	int capacity;//容量-空间大小
}SeqList;

// 对数据的管理:增删查改 
//初始化
void SeqListInit(SeqList* ps);
//销毁创建的空间
void SeqListDestroy(SeqList* ps);

//打印
void SeqListPrint(SeqList* ps);
//尾插
void SeqListPushBack(SeqList* ps, SLDateType x);
//头插
void SeqListPushFront(SeqList* ps, SLDateType x);
//头删
void SeqListPopFront(SeqList* ps);
//尾删
void SeqListPopBack(SeqList* ps);

// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);

 顺序表功能的实现:

#include"SeqList.h"

//初始化
void SeqListInit(SeqList* ps)
{
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}
//尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	//检查容量空间,满了则扩容
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//4 或者 2倍的空间(个数)
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newCapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
	}
	//空间每满就存入一个元素到结构体中,size++
	ps->a[ps->size] = x;//尾插
	ps->size++;
}
//扩容
void SLCheckCapacity(SeqList* ps)
{
	//检查空间是否满了,满了则扩容
	if (ps->size == ps->capacity)
	{
		int nemCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, nemCapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = nemCapacity;
	}
}
//头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);

	SLCheckCapacity(ps);//检查扩容
	//挪动数据,有数据时头插
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	//没数据时头插
	ps->a[0] = x;
	ps->size++;
}
//打印
void SeqListPrint(SeqList* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
//空间全部释放
void SeqListDestroy(SeqList* ps)
{
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
	}
}
//头删
void SeqListPopFront(SeqList* ps)
{
	assert(ps->size > 0);

	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
        ++begin;
	}
	ps->size--;
}
//尾删
void SeqListPopBack(SeqList* ps)
{
	assert(ps->size > 0);

	ps->size--;
}
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
    return -1;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList * ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int begin = pos;
	while (begin < ps->size - 1)
	{
		ps->a[begin] = ps->a[begin + 1];
		++begin;
	}
	ps->size--;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList * ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	SLCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}

3.实际运行状况

头插:

尾插:

 头删:

尾删:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值