顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表。
#pragma once
#include
#include
#include
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
size_t size;
size_t capacity; // unsigned int
}SeqList;
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestory(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType data);
void SeqListPushFront(SeqList* ps, SLDateType data);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType data);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType data);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);
int SeqListSize(SeqList* ps)
{
assert(ps);
return ps->size;
}
int SeqListCapacity(SeqList* ps)
{
assert(ps);
return ps->capacity;
}
void SeqListInit(SeqList* ps, int initCapacity)
{
assert(ps);
ps->a = (int*)malloc(initCapacity * sizeof(int));
if (NULL == ps->a)
{
assert(0);
return;
}
ps->capacity = initCapacity;
ps->size = 0;
}
void SeqListDestory(SeqList* ps)
{
assert(ps);
if (ps->a)
{
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListPushBack(SeqList* ps, SLDateType data)
{
assert(ps);
ps->a[ps->size] = data;
ps->size++;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
if (SeqListEmpty(ps))
{
return;
}
ps->size--;
}
void SeqListPushFront(SeqList* ps, SLDateType data)
{
assert(ps);
for (int i = ps->size-1; i >=0; i--)
{
ps->a[i+1] = ps->a[i];
}
ps->size++;
ps->a[0] = data;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
if (SeqListEmpty(ps))
{
return;
}
for (int i = 0; i < ps->size; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
int SeqListEmpty(SeqList* ps)
{
assert(ps);
return 0 == ps->size;
}
int SeqListFind(SeqList* ps, SLDateType data)
{
assert(ps);
if (SeqListEmpty(ps))
{
printf("顺序表为空\n");
}
for (int i = 0; i < ps->size; i++)
{
if (data == ps->a[i]);
return i;
}
printf("没有这个元素。\n");
return -1;
}
void SeqListInsert(SeqList* ps, size_t pos, SLDateType data)
{
assert(ps);
ps->size++;
if (SeqListEmpty(ps))
{
SeqListPushBack(ps, data);
}
for (int i = ps->size - 1; i > pos; i--)
{
ps->a[i] = ps->a[i - 1];
}
ps->a[pos] = data;
}
void SeqListErase(SeqList* ps, size_t pos)
{
assert(ps);
if (SeqListEmpty(ps))
{
printf("顺序表为空。\n");
return;
}
for (int i = pos; i < ps->size; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
void TestSeqList()
{
SeqList s;
SeqListInit(&s,10);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
SeqListPushBack(&s, 6);
SeqListPushBack(&s, 7);
SeqListPrint(&s);
SeqListPushFront(&s, 0);
SeqListPrint(&s);
SeqListInsert(&s, 4, 10);
SeqListPrint(&s);
SeqListErase(&s, 4);
SeqListPrint(&s);
printf("%d\n", SeqListSize(&s));
printf("%d\n", SeqListCapacity(&s));
SeqListDestory(&s);
}
#include"SeqList.h"
int main()
{
TestSeqList();
system("pause");
return 0;
}
顺序表的问题及思考:
SeqList.h
#pragma once
#include
#include
#include
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
size_t size;
size_t capacity; // unsigned int
}SeqList;
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestory(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType data);
void SeqListPushFront(SeqList* ps, SLDateType data);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType data);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType data);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);
SeqList.c
#include"SeqList.h"
void SeqListInit(SeqList* ps, int initCapacity)
{
assert(ps);
ps->a = (int*)malloc(initCapacity * sizeof(int));
if (NULL == ps->a)
{
assert(0);
return;
}
ps->capacity = initCapacity;
ps->size = 0;
}
void SeqListDestory(SeqList* ps)
{
assert(ps);
if (ps->a)
{
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListPushBack(SeqList* ps, SLDateType data)
{
assert(ps);
ps->a[ps->size] = data;
ps->size++;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
if (SeqListEmpty(ps))
{
return;
}
ps->size--;
}
void SeqListPushFront(SeqList* ps, SLDateType data)
{
assert(ps);
for (int i = ps->size-1; i >=0; i--)
{
ps->a[i+1] = ps->a[i];
}
ps->size++;
ps->a[0] = data;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
if (SeqListEmpty(ps))
{
return;
}
for (int i = 0; i < ps->size; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
int SeqListEmpty(SeqList* ps)
{
assert(ps);
return 0 == ps->size;
}
int SeqListFind(SeqList* ps, SLDateType data)
{
assert(ps);
if (SeqListEmpty(ps))
{
printf("顺序表为空\n");
}
for (int i = 0; i < ps->size; i++)
{
if (data == ps->a[i]);
return i;
}
printf("没有这个元素。\n");
return -1;
}
void SeqListInsert(SeqList* ps, size_t pos, SLDateType data)
{
assert(ps);
ps->size++;
if (SeqListEmpty(ps))
{
SeqListPushBack(ps, data);
return;
}
for (int i = ps->size - 1; i > pos; i--)
{
ps->a[i] = ps->a[i - 1];
}
ps->a[pos] = data;
}
void SeqListErase(SeqList* ps, size_t pos)
{
assert(ps);
if (SeqListEmpty(ps))
{
printf("顺序表为空。\n");
return;
}
for (int i = pos; i < ps->size; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
int SeqListSize(SeqList* ps)
{
assert(ps);
return ps->size;
}
int SeqListCapacity(SeqList* ps)
{
assert(ps);
return ps->capacity;
}
void TestSeqList()
{
SeqList s;
SeqListInit(&s,10);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
SeqListPushBack(&s, 6);
SeqListPushBack(&s, 7);
SeqListPrint(&s);
SeqListPushFront(&s, 0);
SeqListPrint(&s);
SeqListInsert(&s, 4, 10);
SeqListPrint(&s);
SeqListErase(&s, 4);
SeqListPrint(&s);
printf("%d\n", SeqListSize(&s));
printf("%d\n", SeqListCapacity(&s));
SeqListDestory(&s);
}
main.c
#include"SeqList.h"
int main()
{
TestSeqList();
system("pause");
return 0;
}