網(wǎng)站首頁 編程語言 正文
順序表概念及結(jié)構(gòu)
順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲。在數(shù)組上完成數(shù)據(jù)的增刪查改。
分類:
一般分為靜態(tài)順序表和動態(tài)順序表;
靜態(tài)順序表:數(shù)組大小是固定的用完了無法增容;同時我們無法控制給數(shù)組開多少空間合適,開少了,空間不夠;開多了,有回會存在空間浪費;
動態(tài)順序表:空間是可以變動的,空間滿了我們就增容;解決了靜態(tài)順序表的空間不足問題,同時也在一定程度上減少了空間浪費;
因此本篇博客主要實現(xiàn)動態(tài)順序表;(靜態(tài)順序表實現(xiàn)思路差不多,讀者可以自行寫一下)
動態(tài)順序表數(shù)據(jù)結(jié)構(gòu):
基本操作
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include<assert.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;//用于維護動態(tài)開辟的數(shù)組
size_t size;//用于維護動態(tài)數(shù)組有多少個有效值
size_t capacity; // size_t==unsigned int//用于維護當前動態(tài)數(shù)組有多少空間
}SeqList;
// 對數(shù)據(jù)的管理:增刪查改
//初始化順序表
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, size_t pos, SLDateType x);
// 順序表刪除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);
功能實現(xiàn)
#include"SeqList.h"
static void Check_Capacity(SeqList* ps)
{
if (ps->capacity == ps->size)//容量滿了或者還沒開辟空間
{
size_t NewCapacity = (ps->capacity == 0) ? 4 : ps->capacity * 2;
int* tmp = (int*)realloc(ps->a, NewCapacity * sizeof(int));
if (tmp == NULL)
exit(-1);
ps->a = tmp;
ps->capacity = NewCapacity;
}
}
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->capacity = 0;
ps->size = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
if (ps->size)
{
int len = ps->size;
for (int i = 0; i <len; i++)
printf("%d ", ps->a[i]);
}
else
printf("NULL\n");
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
Check_Capacity(ps);
//在插入數(shù)據(jù)之前要先檢查一下是否還有剩余容量
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
Check_Capacity(ps);
int end = ps->size - 1;
for (end; end >= 0; end--)
ps->a[end + 1] = ps->a[end];
ps->a[end + 1] = x;
ps->size++;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
assert(ps->size>0);//都沒元素了就不刪了
int begin = 1;
int len = ps->size;
for (begin; begin < len; begin++)
ps->a[begin - 1] = ps->a[begin];
ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
assert(ps->size>0);
ps->size--;
}
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
assert(ps->size>0);
int len = ps->size;
for (int i = 0; i < len; i++)
if (ps->a[i] == x)
return i;
return -1;
}
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
assert(ps);
//如果pos給我亂傳,超出合法范圍?
assert(pos<=ps->size);
Check_Capacity(ps);
int end = ps->size - 1;
int target = pos;
for (; end >= target; end--)//這里會發(fā)生向上轉(zhuǎn)換,最好把pos類型轉(zhuǎn)換一下
ps->a[end + 1] = ps->a[end];
ps->a[end+1] = x;
ps->size++;
}
void SeqListErase(SeqList* ps, size_t pos)
{
assert(ps);
assert(ps->size>0);
//pos亂傳?
assert(pos<=ps->size);
int begin = pos + 1;
int len = ps->size;
for (; begin < len; begin++)
{
ps->a[begin - 1] = ps->a[begin];
}
ps->size--;
}
程序運行
原文鏈接:https://blog.csdn.net/qq_62106937/article/details/127540973
相關(guān)推薦
- 2021-12-04 解決ASP.NET?Core中使用漏桶算法限流的問題_實用技巧
- 2022-02-01 微信小程序批量獲取input的輸入值,監(jiān)聽輸入框,數(shù)據(jù)同步
- 2023-06-19 Python中str.format()和f-string的使用_python
- 2024-03-07 MyBatis多表映射
- 2022-03-28 Python中selenium_webdriver下拉框操作指南_python
- 2022-02-20 Spring Boot 打包報錯Failed to execute goal org.apache.
- 2023-04-03 React中使用Mobx的方法_React
- 2022-11-12 C++中的數(shù)組、鏈表與哈希表_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支