顺序表的底层实现
1、顺序表的概念
顺序表是用一段物理地址连续的存储单元存储数据元素的线性结构,一般情况下采用数组存储。可以完成对数据的增删改查。
2、顺序表的结构
顺序表的结构有两种:
1、静态顺序表:使用定长数组存储
2、动态顺序表:使用动态开辟的数组存储
//顺序表的静态存储
#define N 100
typedef int SLDataType;
typedef struct SeqList
{
SLDataType array[N];//定长数组
size_t size;//有效数组个数
}SeqList;
//顺序表的动态存储
typedef int SLDataType;
typedef struct SeqList
{
SLDataType *arrary;//指向动态开辟的数组
size_t size;//有效数组个数
size_t capacity;//容量空间大小
}SeqList;
3、顺序表的实现
接口SeqList.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define CAPACITY 2
typedef int SLDataType;
//顺序表的动态存储
typedef struct SeqList
{
SLDataType *array;//指向动态开辟的数组
size_t size;//有效数据长度
size_t capacity;//当前容量大小
}SeqList;
//初始化顺序表
void SeqListInit(SeqList *psl, size_t capacity);
//销毁顺序表
void SeqListDestory(SeqList *psl);
//顺序表扩容
void CheckCapacity(SeqList *psl);
//尾插
void SeqListPushBack(SeqList *psl, SLDataType x);
//尾删
void SeqListPopBack(SeqList *psl);
//头插
void SeqListPushFront(SeqList *psl, SLDataType x);
//头删
void SeqListPopFront(SeqList *psl);
//查找元素
int SeqListFind(SeqList *psl, SLDataType x);
//pos位置插入x
void SeqListInsert(SeqList *psl, size_t pos, SLDataType x);
//删除pos位置
void SeqListErase(SeqList *psl, size_t pos);
//删除x
void SeqListRemove(SeqList *psl, SLDataType x);
//修改pos位置为x
void SeqListModify(SeqList *psl, size_t pos, SLDataType x);
//打印顺序表
void SeqListPrint(SeqList *psl);
//顺序表的冒泡排序
void SeqListBubbleSort(SeqList *psl);
//顺序表的二分查找
int SeqListBinarySearch(SeqList *psl, SLDataType x);
//删除顺序表的所有x元素
void SeqListRemoveAll(SeqList *psl, SLDataType x);
#endif//_SEQLIST_H_
接口功能SeqList.c
#include "SeqList.h"
//初始化顺序表
void SeqListInit(SeqList *psl, size_t capacity)
{
assert(psl);
psl->capacity = capacity;
psl->array = (SLDataType *)malloc(capacity*sizeof(SLDataType));
assert(psl->array);
psl->size = 0;
}
//销毁顺序表
void SeqListDestory(SeqList *psl)
{
assert(psl);
if (psl->array)
{
free(psl->array);
psl->array = NULL;
psl->capacity = 0;
psl->size = 0;
}
}
//顺序表扩容
void CheckCapacity(SeqList *psl)
{
assert(psl);
if (psl->size == psl->capacity)
{
psl->capacity *= CAPACITY;
psl->array = (SLDataType *)realloc(psl->size, psl->capacity*sizeof(SLDataType));
}
}
//尾插
void SeqListPushBack(SeqList *psl, SLDataType x)
{
assert(psl);
CheckCapacity(psl);
psl->array[psl->size] = x;
psl->size++;
}
//尾删
void SeqListPopBack(SeqList *psl)
{
assert(psl || psl->size);
psl->size--;
}
//头插
void SeqListPushFront(SeqList *psl, SLDataType x)
{
assert(psl);
CheckCapacity(psl);
for (int i = psl->size - 1; i >= 0; i--)
{
psl->array[i + 1] = psl->array[i];
}
psl->array[0] = x;
psl->size++;
}
//头删
void SeqListPopFront(SeqList *psl)
{
assert(psl);
psl->size--;
for (size_t i = 0; i < psl->size; i++)
{
psl->array[i] = psl->array[i + 1];
}
}
//查找元素
int SeqListFind(SeqList *psl, SLDataType x)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
if (psl->array[i] == x)
{
return i;
}
}
return -1;
}
//pos位置插入x
void SeqListInsert(SeqList *psl, size_t pos, SLDataType x)
{
assert(psl || pos <= psl->size);
CheckCapacity(psl);
for (int i = psl->size - 1; i >= pos; i--)
{
psl->array[i + 1] = psl->array[i];
}
psl->array[pos] = x;
psl->size++;
}
//删除pos位置
void SeqListErase(SeqList *psl, size_t pos)
{
assert(psl || pos <= psl->size);
psl->size--;
for (int i = pos; i < psl->size; i++)
{
psl->array[i] = psl->array[i + 1];
}
}
//删除x
void SeqListRemove(SeqList *psl, SLDataType x)
{
assert(psl);
size_t pos = SeqListFind(psl, x);
if (pos)
{
SeqListErase(psl, pos);
}
}
//修改pos位置为x
void SeqListModify(SeqList *psl, size_t pos, SLDataType x)
{
assert(psl || pos <= psl->size);
psl->array[pos] = x;
}
//打印顺序表
void SeqListPrint(SeqList *psl)
{
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->array[i]);
}
putchar('\n');
}
//顺序表的冒泡排序
void SeqListBubbleSort(SeqList *psl)
{
assert(psl);
int flag = 0;
for (int i = 0; i < psl->size - 1; i++)
{
for (int j = 0; j < psl->size - 1; j++)
{
if (psl->array[j] > psl->array[j + 1])
{
SLDataType tmp = psl->array[j];
psl->array[j] = psl->array[j + 1];
psl->array[j + 1] = tmp;
flag = 1;
}
if (flag == 0)
{
return;
}
}
}
}
//顺序表的二分查找
int SeqListBinarySearch(SeqList *psl, SLDataType x)
{
assert(psl);
int left = 0;
int right = psl->size - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (psl->array[mid] < x)
{
left = mid + 1;
}
else if (psl->array[mid] > x)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
//删除顺序表的所有x元素
void SeqListRemoveAll(SeqList *psl, SLDataType x)
{
assert(psl);
size_t pos = SeqListFind(psl, x);
if (pos == 0 || pos)
{
SeqListErase(psl, pos);
}
}
j接口测试函数main.c
#include "SeqList.h"
int main() {
SeqList test;
SeqListInit(&test, 10);
SeqListPushBack(&test, 1);
SeqListPushBack(&test, 2);
SeqListPushBack(&test, 3);
SeqListPushBack(&test, 4);
SeqListPushBack(&test, 5);
SeqListPushBack(&test, 6);
SeqListPushBack(&test, 7);
SeqListPushBack(&test, 8);
SeqListPushBack(&test, 9);
SeqListPushBack(&test, 10);
SeqListPrint(&test);
SeqListPopBack(&test);
SeqListPrint(&test);
SeqListPushFront(&test, 11);
SeqListPrint(&test);
SeqListPopFront(&test);
SeqListPrint(&test);
printf("SeqListFind = %d\n", SeqListFind(&test, 6));
SeqListRemove(&test, 6);
SeqListPrint(&test);
SeqListModify(&test, 0, 12);
SeqListPrint(&test);
SeqListBubbleSort(&test);
SeqListPrint(&test);
printf("SeqListBinarySort = %d\n", SeqListBinarySearch(&test, 12));
SeqListRemoveAll(&test, 2);
SeqListPrint(&test);
SeqListDestory(&test);
system("pause");
return 0;
}
来源:CSDN
作者:三吋时光
链接:https://blog.csdn.net/gp1330782530/article/details/104617649