【数据结构初阶】——顺序表

本文由@睡觉待开机原创,转载请注明出处。
本内容在csdn网站首发
欢迎各位点赞—评论—收藏
如果存在不足之处请评论留言,共同进步!

在这里插入图片描述

1.数据结构

数据结构的概念:数据结构这个词可以拆分为“数据”和“结构”两个词,所谓数据就是我们存放在内存中的一系列数字而已,结构指的是组织数据的方式,整体而言,数据结构是计算机存储、组织数据的方式。

其中,数组就是一种最基本的数据结构。

2.顺序表

在介绍顺序表之前,我们先来了解一下线性表

线性表

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


线性表的特点:
在逻辑结构上,是线性结构,也就是一条完整的线。
在物理结构上,则是一种不规则(不一定连续)的数据组织形式。


线性表的分类:
线性表包括:顺序表、链表、栈、队列、字符串…
在这里插入图片描述

顺序表的结构

在这里插入图片描述

//静态顺序表
typedef int int_n;
typedef struct seqlist
{
   
	int_n arr_n[100];
	int size_n;
	int capacity_n;
};

//动态顺序表
typedef int int_t;
typedef struct SeqList
{
   
	int_t* arr;
	int size;
	int capacity;
}SL;

结论:由于动态顺序表的灵活优势,因而首选动态顺序表。

3.动态顺序表的实现

下面是一些功能函数声明和顺序表原型都放在.h文件中。
顺序表是由一个原型和一系列接口(功能)组成的:因而我们首先升级一个数组为动态顺序表原型:

//动态顺序表
typedef int int_t;
typedef struct SeqList
{
   
	int_t* arr;
	int size;
	int capacity;
}SL;

实现各种顺序表的功能接口:

//顺序表初始化的功能
void SLinit(SL* psl);
//顺序表尾插
void SLpushback(SL* psl,int_t n);
//顺序表头插
void SLpushfront(SL* psl,int_t n);
//顺序表中间插
void SLinnert(SL* psl, int pos, int_t n);
//顺序表打印
void SLprint(SL* psl);
//顺序表尾删
void SLpopback(SL* psl);
//顺序表头删
void SLpopfront(SL* psl);
//顺序表中间删
void SLpopinnert(SL* psl,int pos);

下面是具体实现一些函数功能接口:

#include"SeqList.h"
void SLinit(SL* psl)
{
   
	psl->arr = NULL;
	psl->size = psl->capacity = 0;
}

void SLcheckcapacity(SL* psl)
{
   
	if (psl->capacity == psl->size)
	{
   
		//刚开始是0,所以需要进行三目操作符
		int newcapacity = psl->capacity == 0 ? 4 : 2 * psl->capacity;
		
		int_t * t = (int_t*)realloc(psl->arr, newcapacity * sizeof(int_t));
		if (t == NULL)
		{
   
			perror("reallloc fail!");
			exit(1);
		}

		psl->arr = t;
		psl->capacity = newcapacity;
	}
}

void SLpushback(SL* psl,int_t n)
{
   
	assert(psl);
	//容量不足,需要先进行判断容量是否充足,在决定是否进行扩容
	SLcheckcapacity(psl);

	//容量充足,则直接插入
	psl->arr[psl->size] = n;
	psl->size++;
}

void SLpushfront(SL* psl, int_t n)
{
   
	assert(psl);
	//扩容问题
	SLcheckcapacity(psl);
	//所有数字往后挪动一位
	int i = 0;
	for (i = psl->size; i > 0; i--)
	{
   
		psl->arr[i] = psl->arr[i - 1];
	}
	//插入
	psl->arr[0] = n;
	psl->size++;
}

void SLinnert(SL* psl, int pos, int_t n)
{
   
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	//扩容问题
	SLcheckcapacity(psl);
	//中间插入,中间往后所有的数字往后挪动一位
	int i = 0;
	for (i = psl->size; i > pos; i--)
	{
   
		psl->arr[i] = psl->arr[i - 1];
	}
	//插入数据
	psl->arr[pos] = n;
	psl->size++;
}

void SLprint(SL* psl)
{
   
	int i = 0;
	for (i = 0; i < psl->size; i++)
	{
   
		printf("%d ", psl->arr[i]);
	}
	printf("\n");
}

void SLpopback(SL* psl)
{
   
	assert(psl->size && psl);
	psl->size--;
}
void SLpopfront(SL* psl)
{
   
	assert(psl && psl->size);
	int i = 0;
	for(i = 1; i<psl->size; i++)
	{
   
		psl->arr[i-1] = psl->arr[i];
	}

	psl->size--;

}

void SLpopinnert(SL* psl, int pos)
{
   
	assert(pos >= 0 && pos < psl->size);
	int i = 0;
	for (i = pos; i < psl->size-1; i++)
	{
   
		psl->arr[i] = psl->arr[i + 1];
	}
	psl->size--;
}

下面是测试代码源文件内容(在test.c文件中):

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

int main()
{
   
	SL sl;
	SLinit(&sl);
	SLpushback(&sl,1);
	SLpushfront(&sl, 1);
	SLpushfront(&sl, 1);
	SLpushfront(&sl, 1);
	SLpushfront(&sl, 1);
	SLprint(&sl);
	SLinnert(&sl, 1, 3);
	SLprint(&sl);

	
	SLpopback(&sl);
	SLprint(&sl);

	SLpopfront(&sl);
	SLprint(&sl);

	SLpopinnert(&sl,3);
	SLprint(&sl);
	return 0;
}

在这里插入图片描述

相关推荐

  1. 数据结构):顺序实战通讯录

    2024-01-22 09:10:04       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 09:10:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 09:10:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 09:10:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 09:10:04       20 阅读

热门阅读

  1. 大模型学习笔记07——大模型之Adaptation

    2024-01-22 09:10:04       37 阅读
  2. 20.包装类

    2024-01-22 09:10:04       26 阅读
  3. 计算矩阵边缘元素之和(c++)

    2024-01-22 09:10:04       36 阅读
  4. 泛型..

    泛型..

    2024-01-22 09:10:04      33 阅读
  5. 编程羔才生-前端访问后端出现read ECONNRESET

    2024-01-22 09:10:04       38 阅读
  6. Oracle pagesize 和 linesize区别

    2024-01-22 09:10:04       31 阅读
  7. kafka入门(十):副本数据同步

    2024-01-22 09:10:04       38 阅读