目錄
零.必備知識
a.順序表的底層是數(shù)組.
b.數(shù)組在內(nèi)存中是連續(xù)存放的.
c.動態(tài)內(nèi)存空間的開辟(malloc,calloc,realloc).
一.順序表的定義與實現(xiàn)?
????????1.1 順序表的定義?
????????1.2 順序表的初始化?
????????1.3 順序表的銷毀?
????????1.4 順序表容量的檢查與調(diào)整(最關(guān)鍵的部分)
????????1.5 順序表的尾插
????????1.6 順序表的頭插?
????????1.7 順序表的尾刪
????????1.8 順序表的頭刪
?????????1.9 順序表中在指定位置之前插入數(shù)據(jù)
????????1.10 刪除指定位置的數(shù)據(jù)?
????????1.11 順序表中查找指定數(shù)據(jù)
二.順序表源碼?
????????SeqList.h
????????SeqList.c
?
零.必備知識
a.順序表的底層是數(shù)組.
b.數(shù)組在內(nèi)存中是連續(xù)存放的.
c.動態(tài)內(nèi)存空間的開辟(malloc,calloc,realloc).
一.順序表的定義與實現(xiàn)?
注:具體解釋都在注釋中(在代碼中具體分析!)
????????1.1 順序表的定義?
????????1.2 順序表的初始化?
????????1.3 順序表的銷毀?
????????1.4 順序表容量的檢查與調(diào)整(最關(guān)鍵的部分)
?
????????1.5 順序表的尾插
?
????????1.6 順序表的頭插?
?
????????1.7 順序表的尾刪
?
????????1.8 順序表的頭刪
?
?????????1.9 順序表中在指定位置之前插入數(shù)據(jù)
?
????????1.10 刪除指定位置的數(shù)據(jù)?
?文章來源:http://www.zghlxwxcb.cn/news/detail-844849.html
????????1.11 順序表中查找指定數(shù)據(jù)
?
文章來源地址http://www.zghlxwxcb.cn/news/detail-844849.html
二.順序表源碼?
????????SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
靜態(tài)順序表
//struct SeqList
//{
// int arr[100];
// int size;
//};
// 動態(tài)順序表的定義
typedef int DateType; //數(shù)據(jù)類型
typedef struct SeqList
{
DateType* arr;
int size; //數(shù)組中的有效元素個數(shù)
int capacity; //數(shù)組的總?cè)萘?}SL;
// 順序表的初始化
void SLInit(SL* psl);
// 順序表的銷毀
void SLDestroy(SL* psl);
// 打印檢查
void SLprint(SL sl);
// 容量檢查與調(diào)整
void CheckCapacity(SL* psl);
// 尾插-頭插
void SLPushBack(SL* psl, DateType x);
void SLPushFront(SL* psl, DateType x);
// 尾刪-頭刪
void SLPopBack(SL* psl);
void SLPopFront(SL* psl);
// 在指定位置之前插入數(shù)據(jù)
void SLInsert(SL* psl, int pos, DateType x); //pos:點
// 刪除指定位置的數(shù)據(jù)
void SLErase(SL* psl, int pos);
// 順序表的查找
void SLFind(SL psl, DateType x);
????????SeqList.c
?
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
// 順序表的初始化
void SLInit(SL* psl)
{
psl->arr = NULL;
psl->size = psl->capacity = 0;
}
// 順序表的銷毀
void SLDestroy(SL* psl)
{
if (psl->arr)
{
free(psl->arr); //釋放動態(tài)開辟的內(nèi)存空間
}
psl->arr = NULL;
psl->size = psl->capacity = 0;
}
// 容量檢查與調(diào)整
void CheckCapacity(SL* psl)
{
if (psl->size == psl->capacity) //空間不夠了,需要進行擴容
{
int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2; //避免 psl->capacity初始容量為空
DateType* temp = (DateType*)realloc(psl->arr, newCapacity * sizeof(DateType));
if (temp == NULL) //避免因為realloc調(diào)整空間失敗,而導致psl->arr中的數(shù)據(jù)被清除
{
perror("realloc fail!");
exit(1);
}
psl->capacity = newCapacity;
psl->arr = temp;
}
}
// 尾插
void SLPushBack(SL* psl, DateType x)
{
CheckCapacity(psl);
// 插入
psl->arr[psl->size] = x;
psl->size++;
}
// 頭插
void SLPushFront(SL* psl, DateType x)
{
CheckCapacity(psl);
for (int i = psl->size; i > 0; i--)
{
psl->arr[i] = psl->arr[i - 1]; //psl->arr[1] = psl->arr[0]
}
psl->arr[0] = x;
(psl->size)++;
}
// 尾刪
void SLPopBack(SL* psl)
{
assert(psl);
assert(psl->size); //判斷是否為空
(psl->size)--;
}
// 頭刪
void SLPopFront(SL* psl)
{
assert(psl);
assert(psl->size); //判斷是否為空
for (int i = 0; i < psl->size - 1; i++)
{
psl->arr[i] = psl->arr[i + 1]; //psl->arr[psl->size - 1] = psl->arr[psl->size - 2]
}
(psl->size)--;
}
// 在指定位置之前插入數(shù)據(jù)
void SLInsert(SL* psl, int pos, DateType x)
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
CheckCapacity(psl);
for (int i = psl->size; i > pos; i--)
{
psl->arr[i] = psl->arr[i - 1]; //arr[pos + 1] = arr[pos]
}
psl->arr[pos] = x;
psl->size++;
}
// 刪除指定位置的數(shù)據(jù)
void SLErase(SL* psl, int pos)
{
assert(psl);
assert(psl->size != 0);
assert(pos >= 0 && pos < psl->size);
for (int i = pos; i < psl->size - 1; i++)
{
psl->arr[i] = psl->arr[i + 1]; //arr[i - 2] = arr[i - 1]
}
psl->size--;
}
// 順序表的查找
void SLFind(SL* psl, DateType x)
{
assert(psl);
assert(psl->size != 0);
for (int i = 0; i < psl->size; i++)
{
if (psl->arr[i] == x)
{
printf("找到了! 下標是%d\n", i);
return;
}
}
printf("找不到!\n");
}
// 打印
void SLprint(SL sl)
{
for (int i = 0; i < sl.size; i++)
{
printf("%d ", sl.arr[i]);
}
printf("\n");
}
到了這里,關(guān)于[C語言][數(shù)據(jù)結(jié)構(gòu)][動態(tài)內(nèi)存空間的開辟]順序表的實現(xiàn)!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!