当前位置:网站首页>顺 序 表
顺 序 表
2022-08-09 05:13:00 【潜水少年请求出战】
#顺序表
## 1概念及结构
1.顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组
上完成数据的增删查改。
### 顺序表一般可以分为:
1. 静态顺序表:使用定长数组存储元素。
2. 接口实现
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪
费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实
现动态顺序表。
typedef int SLDataType;
// 顺序表的动态存储
typedef struct SeqList
{
SLDataType* array; // 指向动态开辟的数组
size_t size ; // 有效数据个数
size_t capicity ; // 容量空间的大小
}SeqList;
// 基本增删查改接口
// 顺序表初始化
void SeqListInit(SeqList* psl, size_t capacity);
// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl);
// 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x);
// 顺序表尾删
void SeqListPopBack(SeqList* psl);
// 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x);
// 顺序表头删
void SeqListPopFront(SeqList* psl);
// 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos);
// 顺序表销毁
void SeqListDestory(SeqList* psl);
// 顺序表打印
void SeqListPrint(SeqList* psl);
看到这里小伙伴就懵了 ,我们先作道题
链接: oj题:移除元素.
//思路:将数组中要删除的元素用后面的元素覆盖掉
int removeElement(int* nums, int numsSize, int val)
{
int i=0, j=0;
for(i=0; i<numsSize; i++)
{
// 将数组中不等于val的值依次挪动到数组的开始位置
if(nums[i]!=val)
{
nums[j]=nums[i];
++j;
}
}
return j;
}
看到这里大家就发现头删就是下一个数往前覆盖没错;而头删恰好相反,它是往后移动。
3.代码操作
1.(头文件里放)
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
int size; //size_t size;
int capacity;
}SL;
//初始化
void SLInit(SL* ps);
void SLDestory(SL* ps);
//增容
void SLCheckCapacity(SL* ps);
//头插
void SLPushFront(SL* ps, SLDateType x);
//尾插
void SLPushBack(SL* ps, SLDateType x);
//在任意位置插入数据
void SLInsert(SL* ps, int pos, SLDateType x);
//打印
void SLPrint(SL* ps);
//头删
void SLPopFront(SL* ps, SLDateType x);
//尾删
void SLPopBack(SL* ps, SLDateType x);
//任意删除
void SLErase(SL* ps, int pos, SLDateType x);
//修改
SLModify(SL* ps, int pos, SLDateType x);
//查找
int SLFind(SL* ps, SLDateType x);
2.(.c里面放)
#include"SeqList.h"
void SLInit(SL* ps)
{
assert(ps != NULL);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
assert(ps != NULL);
// 检查容量空间,满了扩容
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = (SLDateType*)realloc(ps->a, newCapacity * sizeof(SLDateType));
if (tmp == NULL)
{
printf("realloc fail\n");
//exit(-1);
return;
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
int SLFind(SL* ps, SLDateType x)
{
assert(ps != NULL);
if (ps->size == ps->capacity)
{
SLCheckCapacity(ps);
}
//查找
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SLInsert(SL* ps, int pos, SLDateType x)
{
assert(ps != NULL);
assert((pos >= 0) && (ps->size >= pos));
//检查是否增容。
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
void SLErase(SL* ps, int pos, SLDateType x)
{
assert(ps != NULL);
assert((pos >= 0) && (ps->size >= pos));
int start = pos;
while (start < ps->size - 1)
{
ps->a[start] = ps->a[start + 1];
++start;
}
ps->size--;
}
void SLPushFront(SL* ps, SLDateType x)
{
assert(ps != NULL);
SLInsert(ps, 0, x);
}
void SLPushBack(SL* ps, SLDateType x)
{
assert(ps != NULL);
SLInsert(ps, ps->size - 1, x);
}
void SLPopFront(SL* ps, SLDateType x)
{
assert(ps != NULL);
SLErase(ps, 0, x);
}
void SLPopBack(SL* ps, SLDateType x)
{
assert(ps != NULL);
SLErase(ps, ps->size - 1, x);
}
SLModify(SL* ps, int pos, SLDateType x)
{
assert(ps != NULL);
//查找
int c = SLFind(ps, x);
ps->a[c] = x;
}
void SLPrint(SL* ps)
{
assert(ps != NULL);
int i = 0;
for (i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
}
void SLDestory(SL* ps)
{
assert(ps != NULL);
if (ps->a)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
}
当然还在需要一个.c文件测试,里面有main函数。
边栏推荐
- 【零基础玩转BLDC系列】无刷直流电机闭环控制与软件架构
- mysql content does not exist error
- 剑指Offer-二叉树路径问题总结
- 后台登录模块以及验证码登录
- 【Harmony OS】【ARK UI】Date 基本操作
- 面向6G的欠采样相移键控可见光调制方案
- Parameters in dynamic libraries cannot be modified through macro definitions or global variables in header files
- The development trend of software testing
- 9.jenkins安装
- 剑指Offer-双指针类型题目总结
猜你喜欢

Zuul---路由功能

保存Simulink仿真模型为图片或者PDF的方法

Nacos源码安装

详谈归并排序时间复杂度过程推导----软考

【计算机网络-哈工大】---学习笔记(下)---(二)Web安全威胁、SSL\IPsec、虚拟专用网、防火墙

Address book (dynamic version) (C language) (VS)

中断系统结构及中断控制详解

硅光电池采集用于植物叶片农残检测

C进阶 - 程序的编译(预处理操作) + 链接

Quantitative Genetics Heritability Calculation 2: Half Siblings and Full Siblings
随机推荐
TP6的安装与测试
软件测试的发展趋势
22-08-08 西安 尚医通(04)MongoDB命令、MongoTemplate、MongoRepository
明明加了唯一索引,为什么还是产生重复数据?
如何一键进行Win11系统的重装?
降压模块的使用
神经网络预测应力应变-单轴实验
力扣202-快乐数——哈希集合
Nacos源码安装
Parameters in dynamic libraries cannot be modified through macro definitions or global variables in header files
equals和==
数据库设计---三范式和反范式设计
说明高级语言、汇编语言、机器语言三者的区别,谈谈你对汇编语言的认识。
【HMS core】【ML kit】机器学习服务常见问题FAQ
数据库事务&锁机制
2022-08-08 第四小组 修身课 学习笔记(every day)
数字化时代,企业为什么需要商业智能BI
【LeetCode】136. 只出现一次的数字
匿名共享内存 ashmem
Why do enterprises need business intelligence BI in the digital age