顺序表的实现其实就是和通讯录差不多的,这里将会带领大家一起学习顺序表,总结四个字就是“增删查改”,接下来让我们开始学习吧!
(觉得还不错的铁铁可以点一个赞哦,谢谢!)
目录
一、Sqlist.h (对结构体定义和函数的声明)
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
int size;
int capacity;
}SeqList;
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);
二、Sqlist.c(函数的定义)
三、头插和尾插
尾插比较简单,只需要增加赋值且数组有效个数的个数即可,而头插,需要不断挪动数组
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
CheckSeqlist(ps);//进来先检查容量
ps->size = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
int end = 0;
if (end >= 0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
}
四、头删与尾删
同样的,尾删也比较简单,如果该数组有效数据个数为0,无需尾删,否则就通过将数组最后一个数组赋为0,减小size的大小
头删:将后面的数据赋值给前面,最后减少有效数据的个数
void SeqListPopFront(SeqList* ps)
{
assert(ps);
if (ps->size == 0)
return;
ps->a[ps->size - 1] = 0;
ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
int start = 0;
while (start < ps->size - 1)
{
ps->a[start] = ps->a[start + 1];
start++;
}
ps->size--;
}
接下来是删除和插入函数的定义,这两个函数可以指定任何位置进行插入和删除
插入:
删除
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
CheckSeqlist(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 start = ps->size - 1;
while (start >= pos)
{
ps->a[start - 1] = ps->a[start];
start--;
}
ps->size--;
}
五、Sqlist.c
#include"Sqlist.h"
#include<stdlib.h>
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
if (ps->a == NULL)
{
perror("malloc fail");
return;
}
ps->capacity = 4;
ps->size = 0;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);//开辟了空间一定要释放
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void CheckSeqlist(SeqList* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDateType* tmp = (SLDateType*)realloc(ps->a,sizeof(SLDateType) * ps->capacity * 2);
if (tmp == NULL)
{
perror("realloc error");
return;
}
ps->a = tmp;
ps->capacity = ps->capacity * 2;
}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
CheckSeqlist(ps);//进来先检查容量
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
CheckSeqlist(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);
if (ps->size == 0)
return;
ps->a[ps->size - 1] = 0;
ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
int start = 0;
while (start < ps->size - 1)
{
ps->a[start] = ps->a[start + 1];
start++;
}
ps->size--;
}
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
//assert(pos > 0 && pos < ps->size - 1);
int pos = 0;
for (int i = 0; i < ps->size; i++)
{
ps->a[pos] = x;
}
return pos;
}
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
CheckSeqlist(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 start = ps->size - 1;
while (start >= pos)
{
ps->a[start - 1] = ps->a[start];
start--;
}
ps->size--;
}
六、测试代码(test.c)以及运行结果
#include"Sqlist.h"
testsqilist1()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
SeqListPrint(&s);
SeqListPushFront(&s, 0);
SeqListPushFront(&s, 1);
SeqListPushFront(&s, 2);
SeqListPushFront(&s, 3);
SeqListPrint(&s);
SeqListPopFront(&s);
SeqListPopFront(&s);
SeqListPopFront(&s);
SeqListPrint(&s);
SeqListPopBack(&s);
SeqListPopBack(&s);
SeqListPrint(&s);
SeqListInsert(&s, 3, 30);
SeqListPrint(&s);
SeqListErase(&s, 1);
SeqListPrint(&s);
SeqListDestroy(&s);
}
int main()
{
testsqilist1();
return 0;
}
以上就是我的顺序表的实现啦!如果有问题,可以在评论区或者私信我说哦,谢谢!