一.过程解析
首先我们需要三个文件,分别是头文件——SeqList.h,具体操作文件——SeqList.c,以及测试文件——test.c
首先在头文件中我们写出需要用到的内容
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//定义动态顺序表结构
typedef struct SeqList {
int* arr;
int capacity;//空间大小
int size;//有效数据个数
}SL;
typedef int SLDatatype;
//typedef struct SeqList SL;
//初始化
void SLTnit(SL* ps);//函数声明
//销毁
void SLDestroy(SL* ps);
//插入数据
void SLPushBack(SL* ps, SLDatatype x);
void SLPushFront(SL* ps, SLDatatype x);
//打印
void SLPrint(SL* ps);
//判断空间是否可以使用
void SLCheckCapacity(SL* ps);
//删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
//在指定位置之前插入数据
void SLInsert(SL* ps, SLDatatype x, int pos);
//删除指定位置的数据
void SLErase(SL* ps, int pos);
//查找指定位置的数据
int SLFind(SL* ps, SLDatatype x);
根据头文件的内容,我们一步步进行——代码的实现在SeqList.c中进行,测试在test.c中进行
1.初始化的实现
在SeqList.c中可以写出以下代码:
//初始化
void SLTnit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
在这个环节中有一个易错点,如果我们最开始在头文件中写的是
void SLTnit(SL s);
则在SeqList.c中应该写
void SLTnit(SL s)
{
s.arr = NULL;
s.size = s.capacity = 0;
}
这样看似也正确,但是在测试时却会出现
那么是为什么呢?原因就在于我们忽略了传值调用和传址调用的区别,在这里应该用传址调用!
2.销毁的实现
当我们实现初始化时,同理就可以实现销毁
//销毁
void SLDestroy(SL* ps)
{
if (ps->arr)//相当于ps->arr!=NULL
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
则在test.c中有以下框架:
#include"SeqList.h"
void SLtest01()
{
SL s;
SLTnit(&s);
SLDestroy(&s);
}
int main()
{
SLtest01();
return 0;
}
有基本框架,我们可以进行其他的代码实现
3.插入数据的代码实现
插入有两种,一种是在数据的最前面插入——头插,一种是在数据的最后面插入——尾插;
3.1尾插
因为我们要在数据中插入一个数,我们首先应该想到的是原有的空间是否充足,因此我们需要写一个函数来进行判断——这时我们需要运用到一个函数realloc
在MSDN中有
void *realloc( void *memblock, size_t size );
又因为我们不能确定空间是否可以申请到,我们需要引入一个中间变量,可以定义为SLDatatype* tmp,那么问题又来了,当空间不够使,我们应该怎样扩容呢?
通常,增容我们是成倍数的增加,比如2,3......(最常用的是2),那么我们为什么不可以一次增加一个呢,这样没有了空间的浪费了,这时因为——增容的操作本身就有一定的程序性能的消耗,如果频繁地增容很可能导致程序效率低下!
3.1.1 判断空间是否充足的代码实现
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;//考虑等于0的情况
SLDatatype* tmp = (SLDatatype*)realloc(ps->arr, newCapacity * sizeof(SLDatatype));//应为字节数
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);
}
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
在这个代码中我们需要注意的是:
1.不能忽略ps->capacity==0的情况;
2.必须要引入中间变量;
3.注意运用realloc我么申请的是字节数。
3.1.2 尾插的代码实现
首先我们应该先判断传入的数据的地址是否为空,有两种方式:
第一种:
//第一种
if (ps == NULL)
{
return;
}
第二种:
//第二种
assert(ps);//等价于assert(ps!=NULL)
3.2头插
在头插中我们需要进行数据整体后移一位的操作,然后让第一个位置为指定数据
代码实现:
void SLPushFront(SL* ps, SLDatatype x)
{
assert(ps);
SLCheckCapacity(ps);
//数据整体后移一位
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
//下标为0的位置
ps->arr[0] = x;
ps->size++;
}
需要注意的是,在末尾我们不要忘了ps->size的自增。
4.删除数据的代码实现
删除数据也有两种类型:尾删和头删。
4.1 尾删
代码实现如下:
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);//因为要删除数据,所以ps->size不能为0
ps->arr[ps->size - 1] = -1;//语句1
ps->size--;
}
需要注意的是 :语句1删去也会成立。
4.2 头删
首先从下标为1的数据从后向前挪动一位
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
//数据整体向前挪动一位
for (int i = 0; i < ps->size-1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
5.在指定位置之前插入数据
//在指定位置之前插入数据
void SLInsert(SL* ps, SLDatatype x, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
//pos及之后的数据向后移动
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = x;
ps->size++;
}
需要注意的是:
1.在插入之前空间是否充足;
2.注意i的取值范围;
3.pos等于0时为在第一个数据前插入,pos等于ps->size时为在最后一个数的后面插入。
6.删除指定位置的数据
先删除指定位置的数据,然后位于其后的数据整体向前挪动一位。
//删除指定位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
//pos之后的数据整体向后挪动一位
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
7.查找数据
查找数据较简单,具体如下:
//查找指定位置的数据
int SLFind(SL* ps, SLDatatype x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
return 1;
}
}
//如果没有找到
return -1;
}
顺序表在这里就大概结束了!
需要注意的是:在写的过程中,要边写边测试(本篇文章中省略了检验的过程)!!!
二.总的代码(SeqList.c中的)
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
//初始化
void SLTnit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
//销毁
void SLDestroy(SL* ps)
{
if (ps->arr)//相当于ps->arr!=NULL
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
//判断空间是否可以使用
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;//考虑等于0的情况
SLDatatype* tmp = (SLDatatype*)realloc(ps->arr, newCapacity * sizeof(SLDatatype));//应为字节数
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);
}
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
//插入数据
//尾插
void SLPushBack(SL* ps, SLDatatype x)
{
//第二种
assert(ps);//等价于assert(ps!=NULL)
第一种
//if (ps == NULL)
//{
// return;
//}
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, SLDatatype x)
{
assert(ps);
SLCheckCapacity(ps);
//数据整体后移一位
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
//下标为0的位置
ps->arr[0] = x;
ps->size++;
}
//删除
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);//因为要删除数据,所以ps->size不能为0
ps->arr[ps->size - 1] = -1;
ps->size--;
}
//头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
//数据整体向前挪动一位
for (int i = 0; i < ps->size-1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//在指定位置之前插入数据
void SLInsert(SL* ps, SLDatatype x, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
//pos及之后的数据向后移动
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = x;
ps->size++;
}
//删除指定位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
//pos之后的数据整体向后挪动一位
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//查找指定位置的数据
int SLFind(SL* ps, SLDatatype x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
return 1;
}
}
//如果没有找到
return -1;
}
好了,就到这里,我们下一个知识点见(* ̄︶ ̄)!