C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)順序表的示例代碼
順序表概念及結(jié)構(gòu)
順序表是用一段物理地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲(chǔ)。在數(shù)組上完成數(shù)據(jù)的增刪查改。
分類:
一般分為靜態(tài)順序表和動(dòng)態(tài)順序表;
靜態(tài)順序表:數(shù)組大小是固定的用完了無(wú)法增容;同時(shí)我們無(wú)法控制給數(shù)組開多少空間合適,開少了,空間不夠;開多了,有回會(huì)存在空間浪費(fèi);
動(dòng)態(tài)順序表:空間是可以變動(dòng)的,空間滿了我們就增容;解決了靜態(tài)順序表的空間不足問(wèn)題,同時(shí)也在一定程度上減少了空間浪費(fèi);
因此本篇博客主要實(shí)現(xiàn)動(dòng)態(tài)順序表;(靜態(tài)順序表實(shí)現(xiàn)思路差不多,讀者可以自行寫一下)
動(dòng)態(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;//用于維護(hù)動(dòng)態(tài)開辟的數(shù)組
size_t size;//用于維護(hù)動(dòng)態(tài)數(shù)組有多少個(gè)有效值
size_t capacity; // size_t==unsigned int//用于維護(hù)當(dāng)前動(dòng)態(tài)數(shù)組有多少空間
}SeqList;
// 對(duì)數(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);
功能實(shí)現(xiàn)
#include"SeqList.h"
static void Check_Capacity(SeqList* ps)
{
if (ps->capacity == ps->size)//容量滿了或者還沒(méi)開辟空間
{
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);//都沒(méi)元素了就不刪了
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--)//這里會(huì)發(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--;
}
程序運(yùn)行

到此這篇關(guān)于C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)順序表的示例代碼的文章就介紹到這了,更多相關(guān)C語(yǔ)言動(dòng)態(tài)順序表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
探討register關(guān)鍵字在c語(yǔ)言和c++中的差異
建議不要用register關(guān)鍵字定義全局變量,因?yàn)槿肿兞康纳芷谑菑膱?zhí)行程序開始,一直到程序結(jié)束才會(huì)終止,而register變量可能會(huì)存放在cpu的寄存器中,如果在程序的整個(gè)生命周期內(nèi)都占用著寄存器的話,這是個(gè)相當(dāng)不好的舉措2013-10-10
C語(yǔ)言實(shí)現(xiàn)自定義掃雷游戲(遞歸版)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)自定義掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
如何調(diào)用C標(biāo)準(zhǔn)庫(kù)的exit函數(shù)詳解
這篇文章主要給大家介紹了關(guān)于如何調(diào)用C標(biāo)準(zhǔn)庫(kù)的exit函數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
關(guān)于Qt添加opencv和libtorch庫(kù)的問(wèn)題
這篇文章主要介紹了Qt添加opencv和libtorch庫(kù)的相關(guān)知識(shí),兩種方法一種是通過(guò)手動(dòng)添加,一種是通過(guò)qt creator添加,需要的朋友可以參考下2022-01-01
關(guān)于C++中sort()函數(shù)的用法,你搞明白了沒(méi)
這篇文章主要介紹了關(guān)于C++中sort()函數(shù)的用法,并通過(guò)三種方法介紹了按降序排列的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
C語(yǔ)言調(diào)試手段:鎖定錯(cuò)誤的實(shí)現(xiàn)方法
本篇文章是對(duì)在C語(yǔ)言調(diào)試中,鎖定錯(cuò)誤的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

