顺序表一般可以分为:
1. 静态顺序表:使用定长数组存储。
2. 动态顺序表:使用动态开辟的数组存储。
//静态顺序表
#define N 10
typedef struct SeqList
{
int a[N];
int size;
int capacity; //容量
}SeqList;
//动态顺序表
typedef int SeqDataType;
typedef struct SeqList
{
SeqDataType* a;
size_t size;
size_t capacity;
}SeqList;
顺序表的结构本质上是数组
顺序表需要实现的功能:
1.初始化和销毁
2.头删尾删,头插尾插
3.打印查找
4.删除,插入指定位置
SeqList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//动态顺序表
typedef int SeqDataType;
typedef struct SeqList
{
SeqDataType* a;
size_t size;
size_t capacity;
}SeqList;
//初始化和销毁
void SeqListInit(SeqList *ps);
void SeqListDestroy(SeqList *ps);
//头插尾插 头删尾删
void SeqListPushBack(SeqList *ps, SeqDataType x);
void SeqListPushFront(SeqList *ps, SeqDataType x);
void SeqListPopBack(SeqList *ps);
void SeqListPopFront(SeqList *ps);
//打印 查找
void SeqListPrint(SeqList* ps);
int SeqListFind(SeqList* ps, SeqDataType x);
//插入指定位置 删除指定位置
void SeqListInsert(SeqList* ps, int pos, SeqDataType x);
void SeqListErase(SeqList* ps, int pos);
SeqList.c
#include"SeqList.h"
//顺序表的初始化
void SeqListInit(SeqList *ps)
{
assert(ps);
ps->a=NULL;
ps->size = ps->capacity = 0;
}
//顺序表的销毁
void SeqListDestroy(SeqList *ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
//判断容量是否已满
void SeqCheckCapacity(SeqList* ps)
{
if (ps->size == ps->capacity)
{
int newcapacity;
if (ps->capacity == 0) //判断原来的容量是否为空
{
newcapacity = 4;
}
else
{
newcapacity = ps->capacity * 2;
}
SeqDataType* newA = realloc(ps->a, sizeof(SeqDataType) * newcapacity);
if (newA == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->a = newA;
ps->capacity = newcapacity;
}
}
//顺序表的头插
void SeqListPushBack(SeqList *ps, SeqDataType x)
{
assert(ps);
SeqCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
//顺序表的尾插
void SeqListPushFront(SeqList *ps, SeqDataType x)
{
assert(ps);
SeqCheckCapacity(ps);
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
}
//顺序表的头删
void SeqListPopFront(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
for (int i = 0; i < ps->size - 1; ++i)
{
ps->a[i] = ps->a[i + 1];
}
--ps->size;
}
//顺序表的尾删
void SeqListPopBack(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
--ps->size;
}
//顺序表的打印
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; ++i)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
//顺序表的查找
int SeqListFind(SeqList* ps, SeqDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; ++i)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
//顺序表的插入
void SeqListInsert(SeqList* ps, int pos, SeqDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
SeqCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
++ps->size;
}
//顺序表的删除
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
int i = pos;
while (i < ps->size - 1)
{
ps->a[i] = ps->a[i + 1];
++i;
}
--ps->size;
}
Test.c
测试用的主函数
#include"SeqList.h"
void TestSeqList1()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
SeqListPrint(&s);
SeqListPushFront(&s, 0);
SeqListPrint(&s);
SeqListPopBack(&s);
SeqListPrint(&s);
SeqListPopFront(&s);
SeqListPrint(&s);
int i=SeqListFind(&s, 4);
printf("要找的数字在第%d位\n", i);
SeqListInsert(&s, 3, 99);
SeqListPrint(&s);
SeqListErase(&s, 3);
SeqListPrint(&s);
SeqListDestroy(&s);
}
void main()
{
TestSeqList1();
}