網站首頁 編程語言 正文
今天起開始編寫數據結構中的各種數據結構及算法的實現,說到順序表,我們首先得了解下線性表。
1.線性表
線性表(linear list)是n個具有相同特性的數據元素的有限序列。 線性表是一種在實際中廣泛使用的數據結構,常見的線性表:順序表、鏈表、棧、隊列、字符串…
線性表在邏輯上是線性結構,也就說是連續的一條直線。但是在物理結構上并不一定是連續的,線性表在物理上存儲時,通常以數組和鏈式結構的形式存儲。
2.順序表
2.1 概念及結構
順序表是用一段物理地址連續的存儲單元依次存儲數據元素的線性結構,一般情況下采用數組存儲。在數組上完成數據的增刪查改。順序表一般可分為:
1.靜態順序表:使用定長數組存儲。
2.動態順序表:使用動態開辟的數組存儲。
//順序表的靜態存儲
#define N 100
struct SeqList
{
int a[N];//定長存儲
int size;//有效數據的個數
};
//順序表的動態存儲
typedef struct SeqList
{
SeqDataType* a;//指向動態開辟的數組
int size; //有效數據個數
int capacity; //容量
}SeqList;
順序表本質上是數組,在數組上增加了兩個要求:
1.插入數據的過程中,可以動態增長
2.并且要求里面存儲的數據必須是從左往右,是連續的
順序表的缺陷
1.動態增容有性能消耗
2.頭部插入數據時,需要挪動數據
2.2 提供接口
靜態順序表只適用于確定知道需要存多少數據的場景。靜態順序表的定長數組導致N定大了,空間開多了浪費,開少了不夠用。所以現實中基本都是使用動態順序表,根據需要動態的分配空間大小,所以下面我們來實現動態順序表。
首先在頭文件<SeqList.h>中提供接口:
typedef int SeqDataType; //需要插入什么類型的數據,就改成對應類型
typedef struct SeqList
{
SeqDataType* a;//指向動態開辟的數組
int size; //有效數據個數
int capacity; //容量
}SeqList;
//內存中管理數據結構 提供增刪查改的接口
//順序表初始化
void SeqListInit(SeqList* pq);
//順序表銷毀
void SeqListDestory(SeqList* pq);
//順序表打印
void SeqListPrint(SeqList* pq);//打印數組
//檢查空間,如果滿了,進行增容
void SeqCheckCapacity(SeqList* pq)
//順序表尾插
void SeqListPushBack(SeqList* pq, SeqDataType x);
//順序表頭插
void SeqListPushFront(SeqList* pq, SeqDataType x);
//順序表尾刪
void SeqListPopBack(SeqList* pq);
//順序表頭刪
void SeqListPopFront(SeqList* pq);
//順序表查找x
int SeqListFind(SeqList* pq, SeqDataType x);//查找 查到返回下標,沒查到返回-1
//順序表在指定位置插入數據
void SeqListInsert(SeqList* pq, int pos, SeqDataType x);//在下標pos位置處插入數據
//順序表在指定位置刪除數據
void SeqListErase(SeqList* pq, int pos);//把下標為pos位置處的數據刪除
//順序表在指定位置替換數據
void SeqListModify(SeqList* pq, int pos, SeqDataType x);//把小標為pos位置的值改為x
2.3 接口實現
在源文件SeqList.c中實現接口功能
(1)順序表初始化
void SeqListInit(SeqList* pq)
{
assert(pq != NULL);//或者 assert(pq); 斷言 防止傳入空指針
pq->a = NULL;
pq->size = 0;
pq->capacity = 0;
}
(2)順序表銷毀
void SeqListDestory(SeqList* pq)
{
assert(pq);
free(pq->a);
pq->a = NULL;
pq->size = 0;
pq->capacity = 0;
}
(3)順序表打印
void SeqListPrint(SeqList* pq)
{
assert(pq);
for (int i = 0; i < pq->size; ++i)
{
printf("%d ", pq->a[i]);
}
printf("\n");
}
(4)檢查空間,如果滿了,進行增容
//檢查是否需要擴容
void SeqCheckCapacity(SeqList* pq)
{
//滿了,需要增容
if (pq->size == pq->capacity)
{
int newcapacity = (pq->capacity == 0 ? 4 : pq->capacity * 2);
//realloc接收的地址如果為空,將像malloc一樣,開辟一塊新空間
SeqDataType* newA = realloc(pq->a, sizeof(SeqDataType) * newcapacity);//realloc返回 開辟的新空間的地址
if (newA == NULL)
{
printf("realloc fail\n");
exit(-1);//失敗了就退出
}
pq->a = newA;
pq->capacity = newcapacity;
}
}
(5)順序表尾插
void SeqListPushBack(SeqList* pq, SeqDataType x)//尾插
{
assert(pq);
SeqCheckCapacity(pq);
pq->a[pq->size] = x;
pq->size++;
}
(6)順序表頭插
void SeqListPushFront(SeqList* pq, SeqDataType x)
{
assert(pq);
SeqCheckCapacity(pq);
int end = pq->size - 1;
while (end >= 0)
{
pq->a[end + 1] = pq->a[end];
end--;
}
pq->a[0] = x;
pq->size++;
}
(7)順序表尾刪
void SeqListPopBack(SeqList* pq)
{
assert(pq);
assert(pq->size > 0);
pq->size--;
}
(8)順序表頭刪
void SeqListPopFront(SeqList* pq)
{
assert(pq);
assert(pq->size > 0);
int begin = 0;
while (begin < pq->size - 1)
{
pq->a[begin] = pq->a[begin + 1];
begin++;
}
pq->size--;
}
(9)順序表查找x
int SeqListFind(SeqList* pq, SeqDataType x)
{
assert(pq);
for (int i = 0; i < pq->size; ++i)
{
if (pq->a[i] == x)
{
return x;
}
}
return -1;//沒找到
}
(10)順序表在指定位置插入數據
void SeqListInsert(SeqList* pq, int pos, SeqDataType x){<!--{C}%3C!%2D%2D%20%2D%2D%3E-->assert(pq);assert(pos >= 0 && pos < pq->size);SeqCheckCapacity(pq);//檢查是否需要擴容int end = pq->size - 1;while (end >= pos){<!--{C}%3C!%2D%2D%20%2D%2D%3E-->pq->a[end + 1] = pq->a[end];end--;}pq->a[pos] = x;pq->size++;}void SeqListInsert(SeqList* pq, int pos, SeqDataType x)
{
assert(pq);
assert(pos >= 0 && pos < pq->size);
SeqCheckCapacity(pq);//檢查是否需要擴容
int end = pq->size - 1;
while (end >= pos)
{
pq->a[end + 1] = pq->a[end];
end--;
}
pq->a[pos] = x;
pq->size++;
}
(11)順序表在指定位置刪除數據
void SeqListErase(SeqList* pq, int pos)
{
assert(pq);
assert(pos >= 0 && pos < pq->size);
int begin = pos;
while (begin <= pq->size - 1)
{
pq->a[begin] = pq->a[begin + 1];
begin++;
}
pq->size--;
}
(12)順序表在指定位置替換數據
void SeqListModify(SeqList* pq, int pos, SeqDataType x)
{
assert(pq);
assert(pos >= 0 && pos < pq->size);
pq->a[pos] = x;
}
主函數的設計大家可以自由發揮,做個簡單的測試功能調用函數或是創建菜單欄實現交互都可以。我水平有限,請朋友們諒解!寫的不好的地方還請大佬們指出。
下期預告——單鏈表
原文鏈接:https://blog.csdn.net/qq_43460230/article/details/124160112
相關推薦
- 2022-09-22 nodeSelector:Pod 定向調度
- 2022-01-28 mybatis事務DefaultSqlSession-策略模式
- 2022-06-20 Flutter?Navigator路由傳參的實現_Android
- 2022-11-21 C++獲取文件大小數值的三種方式介紹_C 語言
- 2021-11-16 使用Flutter定位包獲取地理位置_Android
- 2022-05-27 C++?超詳細快速掌握二叉搜索樹_C 語言
- 2022-08-30 如何解決React?useEffect鉤子帶來的無限循環問題_React
- 2022-03-18 .NET?6開發TodoList應用之實現PUT請求_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支