順序表的基本概念
順序表是在計算機記憶體中以陣列的形式保存的線性表,線性表的順序存盤是指用一組地址連續的存盤單元依次存盤線性表中的各個元素、使得線性表中在邏輯結構上相鄰的資料元素存盤在相鄰的物理存盤單元中,即通過資料元素物理存盤的相鄰關系來反映資料元素之間邏輯上的相鄰關系,采用順序存盤結構的線性表通常稱為順序表,順序表是將表中的結點依次存放在計算機記憶體中一組地址連續的存盤單元中
順序表的實作方式
順序表一般利用陣列+動態記憶體管理實作,一般設定特定的介面方便后續呼叫;
我們要利用順序表實作簡單資料的增刪查改,即頭插頭刪,尾插尾刪,中間插入中間洗掉;
下面是常用的介面函式
//尾插
void SeqListPushBack(SeqList* pq, SeqDateType);
//頭插
void SeqListPushFront(SeqList* pq, SeqDateType);
//尾刪
void SeqListPopBack(SeqList* pq);
//頭刪
void SeqListPopFront(SeqList* pq);
//列印
void SeqListPrint(SeqList* pq);
//檢查容量是否滿
void SeqListCheckCapacity(SeqList* pq);
//進行初始化
void SeqListInit(SeqList* pq);
//銷毀線性表
void SeqListDestory(SeqList* pq);
// 順序表查找
void SeqListFind(SeqList* ps, SeqDateType x);
// 順序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SeqDateType x);
// 順序表洗掉pos位置的值
//void SeqListErase(SeqList* ps, size_t pos);
順序表介面的實作
下面我們通過具體的函式來實作順序表:
首先搭建基本環境,創建一個結構體,并將其命名為SeqLIst,結構體內創建3個資料,一個陣列,一個是當前陣列容量大小,最后一個是陣列最大容量
typedef int SeqDateType;
typedef struct SeqList
{
SeqDateType* a;
int size;
int capacity;
}SeqList;
接下來初始化順序表,讓其剛開始大小為0
//初始化順序表
void SeqListInit(SeqList* pq)
{
assert(pq);
pq->a = NULL;
pq->size = 0;
pq->capacity = 0;
}
按照正常思路接下來就是插入資料了,但是在每次插入資料前我們都要判斷以下當前順序表所存盤的資料和容量進行比較,如果空間不夠進行擴容:
void SeqListCheckCapacity(SeqList* pq);檢查陣列容量,判斷陣列容量是否滿了
//檢查容量
void SeqListCheckCapacity(SeqList* pq)
{
if (pq->size == pq->capacity)
{
int newcapacity = (pq->capacity == 0 ? 4 : pq->capacity * 2);
SeqDateType* p = realloc(pq->a,newcapacity * sizeof(SeqDateType));
if (p != NULL)
{
pq->a = p;
pq->capacity = newcapacity;
}
else
{
printf("realloc fail\n");
exit(-1);
}
}
}
接下來就該實作我們的插入洗掉資料了:
void SeqListPushBack(SeqList* pq, SeqDateType);在陣列尾部進行插入**
//尾部插入資料
void SeqListPushBack(SeqList* pq,SeqDateType x)
{
assert(pq);
//每次插入資料都要檢查容量
SeqListCheckCapacity(pq);
pq->a[pq->size] = x;
pq->size++;
}
頭部插入資料:void SeqListPushFront(SeqList* pq, SeqDateType x)
//頭部插入資料
void SeqListPushFront(SeqList* pq, SeqDateType x)
{
assert(pq);
SeqListCheckCapacity(pq);
int end = pq->size - 1;
for (;end>=0;end--)
{
pq->a[end+1] = pq->a[end];
}
pq->a[0] = x;
pq->size++;
}
尾部洗掉資料:void SeqListPopBack(SeqList* pq)
void SeqListPopBack(SeqList* pq)
{
pq->size--;
}
頭部洗掉資料:void SeqListPopFront(SeqList* pq)
//頭刪
void SeqListPopFront(SeqList* pq)
{
assert(pq);
assert(pq->size > 0);
for (int i = 0; i < pq->size-1; i++)
{
pq->a[i] = pq->a[i + 1];
}
pq->size--;
}
列印資料:void SeqListPrint(SeqList* pq)
//列印資料
void SeqListPrint(SeqList* pq)
{
assert(pq);
for (int i = 0; i < pq->size; i++)
{
printf("%d\n", pq->a[i]);
}
}
銷毀資料:void SeqListDestory(SeqList* pq)
```c
```bash
//銷毀資料
void SeqListDestory(SeqList* pq)
{
assert(pq);
pq->a = NULL;
pq->capacity = 0;
pq->size = 0;
}
查找指定內容:void SeqListFind(SeqList* ps, SeqDateType x)
//查找指定內容
void SeqListFind(SeqList* ps, SeqDateType x)
{
assert(ps);
int i = 0;
for (; i < ps->size; i++)
{
if (x == ps->a[i])
{
break;
}
}
if (i == ps->size)
{
printf("未找到該成員\n");
return;
}
else
{
printf("%d的下標為%d\n", ps->a[i], i);
}
}
指定位置插入資料:void SeqListInsert(SeqList* ps, size_t pos, SeqDateType x)
//指定位置插入資料
void SeqListInsert(SeqList* ps, size_t pos, SeqDateType x)
{
assert(ps);
//assert(pos<=ps->size);
SeqListCheckCapacity(ps);
if (pos == 0)
{
}
//int end = ps->size - 1;
//while (end >= (int)pos)
//{
// ps->a[end + 1] = ps->a[end];
// --end;
//}
size_t end = ps->size;
while (end >= pos)
{
ps->a[end] = ps->a[end - 1];
--end;
}
ps->a[pos] = x;
ps->size++;
}
指定位置洗掉資料:void SeqListErase(SeqList* ps, size_t pos)
// 順序表洗掉pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
//assert(ps && (pos <ps->size));
//size_t start = pos;
//while (start < ps->size-1)
//{
// ps->a[start] = ps->a[start + 1];
// ++start;
//}
//asserr(ps);
//assert(pos >=0 && pos<=ps.size);
size_t start = pos + 1;
while (start < ps->size)
{
ps->a[start - 1] = ps->a[start];
++start;
}
ps->size--;
}
如果搞懂順序表的話不妨自己動手利用順序表寫一個動態通訊錄檢驗一下自己,如果你喜歡小編的文章的話請多多一鍵三連吧,謝謝大家的支持
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/272031.html
標籤:其他
