顺序表(数据结构入门的开端)

文章目录

这篇文章讲述的是我在顺序表学习上的见解,希望对大家有所帮助!

注:学会这篇文章的内容可以学习学生管理系统这篇文章(就是有关登记的东西它都可以做到)

顺序表

概念与结构

概念:顺序表是⽤⼀段物理地址连续的存储单元依次存储数据元素的线性结构,⼀般情况下采⽤数组

存储。

顺序表和数组的区别?

顺序表的底层结构是数组,对数组的封装,实现了常⽤的增删改查等接⼝

分类

静态顺序表

概念:使⽤定⻓数组存储元素

静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费

动态顺序表

动态顺序表的实现

SeqList.h
点我展开

c 复制代码
#define INIT_CAPACITY 4
 typedef int SLDataType;
 // 动态顺序表 -- 按需申请
 typedef struct SeqList
{
 SLDataType* a;
 int size; // 有效数据个数
 int capacity; // 空间容量
 }SL;
//初始化和销毁
 void SLInit(SL* ps);
 void SLDestroy(SL* ps);
 void SLPrint(SL* ps);
 //扩容
 void SLCheckCapacity(SL* ps);
 //头部插⼊删除 / 尾部插⼊删除
 void SLPushBack(SL* ps, SLDataType x);
 void SLPopBack(SL* ps);
 void SLPushFront(SL* ps, SLDataType x);
 void SLPopFront(SL* ps);

 //指定位置之前插⼊/删除数据
 void SLInsert(SL* ps, int pos, SLDataType x);
 void SLErase(SL* ps, int pos);
 int SLFind(SL* ps, SLDataType x);
顺序表的基础用法

接下来我将讲述顺序表在VS2026中的写法

我把它分为了SeqList.h、SeqList.c、test.c来完成

代码示列

SeqList.h
点我展开

c 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

//定义动态顺序表的结构
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* arr;
	int size;      //有效数据个数
	int capacity;  //空间容量
}SL;

//typedef struct SeqList SL;
//打印顺序表
void SLPrint(SL* PS);
//初始化
void SLInit(SL* ps);
//销毁
void SLDestroy(SL* ps);
//尾插
void SLPushBack(SL* ps,SLDataType x);
//头插
void SLPushFront(SL* ps,SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头删
void SLPopFront(SL* ps);
//指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x);
//删除POS位置的数据
void SLErase(SL* ps, int pos);
//查找
int SLFind(SL* ps,SLDataType x);

SeqList.c
点我展开

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

//初始化
void SLInit(SL* ps)
{
  ps->arr = NULL;
  ps->size = ps->capacity = 0;
}
//销毁
void SLDestroy(SL* ps)
{
  assert(ps);
  if(ps->arr)
  free(ps->arr);
  ps->arr = NULL;
  ps->capacity = ps->size = 0;
}
void SLCheckCapacity(SL* ps)
{
  if (ps->capacity == ps->size)
  {
     int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
     //增容或创建空间
    //realloc第二个参数,单位是字节
  SLDataType* temp = (SLDataType*)realloc(ps->arr,newCapacity * sizeof(SLDataType));
  if (temp == NULL)
  {
    perror("realloc fail");
    exit(1);
  }
  ps->arr = temp;
  ps->capacity = newCapacity;
  }

}
void SLPushBack(SL* ps, SLDataType x)
{
  assert(ps);
  SLCheckCapacity(ps);
  ps->arr[ps->size++] = x;
}
//顺序表打印
void SLPrint(SL* ps)
{
  for (int i = 0; i < ps->size; i++)
  {
    printf("%d ", ps->arr[i]);
 }
 printf("\n");
}
void SLPushFront(SL* ps, SLDataType x)
{
  assert(ps != NULL);
  SLCheckCapacity(ps);
  for (int i = ps->size; i > 0; i--)
  {
    ps->arr[i] = ps->arr[i - 1];
  }
  ps->arr[0] = x;
}
//尾删
void SLPopBack(SL* ps)
{
  assert(ps);
  --ps->size;
}
//头删
void SLPopFront(SL* ps)
{
  assert(ps);
  for (int i = 0; i < ps->size; 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);
  //pos及之后的数据整体向后挪动一位
  for (int i = ps->size; i > pos; i--)
  {
    ps->arr[i] = ps->arr[i - 1];
  }
  ps->arr[pos] = x;
  ++ps->size;
}
//删除POS位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size );
for (int i = pos; i < ps->size - 1; i++)
{
 ps->arr[i] = ps->arr[i + 1];
}
--ps->size;
}

test.c
点我展开

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void test01()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	//SLPushBack(&sl, 4);
	//SLPushFront(&sl, 1);
	//SLPopBack(&sl);
	//SLPrint(&sl);
	//SLPopBack(&sl);
	//SLPrint(&sl);
	//SLPopFront(&sl);
	//SLPrint(&sl);
	//SLPopFront(&sl);
	//SLPrint(&sl);
	//SLInsert(&sl, 2, 3);
	//SLErase(&sl,0);
	SLPrint(&sl);
	SLDestroy(&sl);
}
int main()
{
	test01();
	return 0;
}

接下来我将讲解这些代码中重要的知识点

顺序表的形成
c 复制代码
//定义动态顺序表的结构
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* arr;
	int size;      //有效数据个数
	int capacity;  //空间容量
}SL;

为了方便写之后的代码我们把int(其它类型都可以),struct SeqList

用typedef 取名字

给int typedef 是因为如果我们要写字符串类型的循序表可以吧int 换成 char

而struct SeqList typedef是因为方便书写

typedef看不明白结构体文章里有

我们看到结构体里面有int* arr 、 int size 、int capacity

第一个表示指针给它扩容就可以放东西了(可以想象成数组扩容后)

size : 我们后面放有效数据的时候有几个数据size就显示几

capacity: 后面扩容或给容量时用得着

SLPrint(SL* PS)

不用断言assert(只有要改变里面的内容时才要断言顺序表的头指针)

代码意图:把顺序表里的内容全部清空

SLDestroy(SL* ps)

assert:断言ps是否为空

不为空才能指针访问

列如:ps->arr

要用头文件#include<assert.h>才能使用assert

然后把里面的内容清空

讲一下

if(ps->arr != NULL)

free(ps->arr)

如果ps->arr不为空

需要清空里面的内容

为空就不要管它

SLCheckCapacity(SL* ps)

目的:若是指针里没有内容给予一个空间,若是空间满了就给空格扩容2倍

要用扩容函数、exit要头文件#include<stdlib.h>

实现代码逻辑:根据size是否与capacity相等来判断是给容还是扩容

最后记得更新capacity

还有要给一个SLDataType* temp因为断言失败会找不到指针
注意:学习的时候不会就问AI或啥的被自己搞

SLPushBack(SL* ps, SLDataType x)

SLCheckCapacity(ps)有空间不用管不够就扩

实现因为下标是从0开始ps->arr[ps->size]上无数据且左边为最后一个数据则把x赋值到ps->arr[ps->size]就完成了尾插

ps->arr[ps->size++]是因为加入了一个数据有效值要加1

SLPushFront(SL* ps, SLDataType x)

这里要讲到后移

后移我们要从最后一个数开始移位置

for (int i = ps->size; i > 0; i--)

{

ps->arr[i] = ps->arr[i - 1];

}

这里i = ps->size是因为要把最后一个数移到下标ps->size上(实现所有数后移)

然后把 ps->arr[0] = x

最后给有效值ps->size++

注意:有些自定义函数我就不讲解了,我就写我认为重要的

SLPopFront(SL* ps)

头删就是前移

for (int i = 0; i < ps->size; i++)

{

ps->arr[i] = ps->arr[i + 1];

}

最后记得--ps->size

SLInsert(SL* ps, int pos, SLDataType x) && SLFind(SL* ps,SLDataType x)

先说一下SLFind(SL* ps,SLDataType x)(它是int类型的自定义函数)忘记写了(你可以完成的)

讲一下利用for循环遍历若是ps->a[i] = x就返回下标i(i最开始是0)没找到就返回-1

最后说一下 SLInsert(SL* ps, int pos, SLDataType x)后移到pos+1

然后ps->a[pos] = x;

记得给有效值加加++ps->size

文章到这就结束了,希望对你有所帮助!

相关推荐
汉字萌萌哒10 小时前
2025 CSP-S提高级(第一轮)C++真题以及答案
数据结构·算法
春栀怡铃声10 小时前
【C++修仙录02】筑基篇:list 使用
数据结构·list
夏日听雨眠11 小时前
数据结构(BF算法 )
数据结构·算法·排序算法
夏日听雨眠11 小时前
数据结构(KMP算法)
数据结构·算法
并不喜欢吃鱼11 小时前
从零开始 C++----十【C++ 数据结构】AVL 树详解:从原理到实现
开发语言·数据结构·c++
图码11 小时前
[特殊字符] 高效统计排序数组中目标元素的出现次数
数据结构·算法·排序算法·柔性数组·图搜索
炘爚11 小时前
数据结构:单链表
数据结构
代码中介商11 小时前
哈夫曼树:高效压缩数据的秘密武器
数据结构·算法
练习时长一年11 小时前
LeetCode热题100之缺失的第一个正数
数据结构·算法·leetcode