1. Sequence table
Sequence diagram:
1. Structure declaration and method (function) declaration
//结构的声明
typedef int DataType;
typedef struct sqlist
{
DataType* data;//指向存储空间的指针
int length; //已经存储的个数
int size; //顺序表的容量,单位个数
}sqlist;
//方法的声明
//判空
bool IsEmpty(sqlist* sq);
//判满
bool IsFull(sqlist* sq);
//初始化
bool InitSqlist(sqlist* sq, int size);
//销毁
bool DestroyList(sqlist* sq);
//求表长
int LengthOfSqlist(sqlist* sq);
//扩容
static bool ApplyNewSpace(sqlist* sq);
//插入
//按位置插入
bool InsertOfPos(sqlist* sq, DataType value, int pos);
//头插法
bool InsertOfFront(sqlist* sq, DataType value);
//尾插法
bool InsertOfRear(sqlist* sq, DataType value);
//删除
//按位置删除
bool DeleteOfPos(sqlist* sq, int pos);
//头删法
bool DeleteOfFront(sqlist* sq);
//尾删法
bool DeleteOfRear(sqlist* sq);
//根据值查找
bool SearchOfValue(sqlist* sq, DataType value);
//
void ShowSqlist(sqlist* sq);
2. Method (function) realization
#include<stdio.h>
#include "sqlist.h"
#include<malloc.h>
//初始化
bool InitSqlist(sqlist* sq, int size) //传递顺序表的地址,和初始容量
{
if (sq == NULL || size <= 0) return false;//如果传递的顺序表地址不存在或者容量不合理,则直接返回
//为顺序表开辟堆区空间,使用malloc函数
sq->data = (DataType*)malloc(size * sizeof(DataType));
//判断是否开辟成功
if (sq->data == NULL)
{
return false;
}
sq->length = 0;//顺序表的元素个数初始化为0
sq->size = size;//初始化容量为size个
return true;
}
//顺序表的销毁
bool DestroyList(sqlist* sq)
{
if (sq == NULL)
{
return false;
}
free(sq->data);
sq->data = NULL;//防止出现野指针
sq->length = 0;
sq->size = 0;
return true;
}
//求顺序表表长
int LengthOfSqlist(sqlist* sq)
{
if (sq == NULL) return false;
return sq->length;
}
//判空操作
bool IsEmpty(sqlist* sq)
{
if (sq == NULL|| sq->length == 0) return true;
return false;
}
//判满操作
bool IsFull(sqlist* sq)
{
if (sq == NULL) return false;
return sq->length == sq->size;
}
//顺序表扩容
static bool ApplyNewSpace(sqlist* sq)
{
if (sq == NULL) return false;
int new_size = sq->size == 0 ? 10 : 2 * sq->size;//如果容量为0,则new_size=10;否则new_size=2*sq-size
DataType* new_space = (DataType*)malloc(sizeof(DataType) * new_size);
if (new_space == NULL) return false;
//将旧值复制到新空间中
for (int i = 0; i < sq->length; i++)
{
new_space[i] = sq->data[i];
}
//复制完毕后,释放原来的空间
free(sq->data);
sq->data = new_space;//把新空间的地址赋值给sq-data
sq->size = new_size;//把扩容好的容量赋值给sq->size
return true;
}
//按位置插入
bool InsertOfPos(sqlist* sq, DataType value, int pos)
{
if (sq == NULL) return false;
if (pos > sq->length || pos < 0) return false;
//如果顺序表满了并且没有扩容成功,返回false
if (IsFull(sq) && !ApplyNewSpace(sq)) return false;
//在pos位置插入value值:1.将pos位置和以后位置依次向后挪一个单位。2.在pos位置插入value值 3.sq->length++
for (int i = sq->length; i > pos; i--)
{
sq->data[i] = sq->data[i - 1];
}
sq->data[pos] = value;
sq->length++;
return true;
}
//头插法
bool InsertOfFront(sqlist* sq, DataType value)
{
return InsertOfPos(sq, value, 0);
}
//尾插法
bool InsertOfRear(sqlist* sq, DataType value)
{
return InsertOfPos(sq, value, sq->length);
}
//按位置删除
bool DeleteOfPos(sqlist* sq, int pos)
{
//如果删除位置小于0或者大于等于sq->length
if (sq == NULL || pos >= sq->length || pos < 0) return false;
for (int i = pos; i < sq->length-1; i++)
{
sq->data[i] = sq->data[i + 1];
}
sq->length--;
return true;
}
//头删法
bool DeleteOfFront(sqlist* sq)
{
return DeleteOfPos(sq, 0);
}
//尾删法
bool DeleteOfRear(sqlist* sq)
{
return DeleteOfPos(sq, sq->length-1);
}
//根据值删除
bool DeleteOfValue(sqlist* sq, DataType value)
{
if (sq == NULL) return false;
for (int i = 0; i < sq->length; )
{
if (sq->data[i] == value)
{
DeleteOfPos(sq, i);
sq->length--;
}
else
{
i++;
}
}
return true;
}
//根据值查找
bool SearchOfValue(sqlist* sq, DataType value)
{
if (sq == NULL) return false;
for (int i = 0; i < sq->length; i++)
{
if (sq->data[i] == value)
{
return true;
}
}
return false;
}
//输出
void ShowSqlist(sqlist* sq)
{
for (int i = 0; i < sq->length; i++)
{
printf("%d ",sq->data[i]);
}
printf("表长%d\n", LengthOfSqlist(sq));
printf("\n");
}
3. Main function code (test)
#include <stdio.h>
#include<malloc.h>
#include "sqlist.h"
int main()
{
sqlist list1; //创建顺序表
InitSqlist(&list1, 10); //初始化顺序表,初始容量为10(个)
InsertOfPos(&list1,11,0); //按位置0位置插入
InsertOfPos(&list1, 22, 1);//按位置1位置插入
InsertOfPos(&list1, 14, 2);//按位置2位置插入
InsertOfRear(&list1, 55); //尾插
InsertOfFront(&list1, 66); //头插
ShowSqlist(&list1); //输出
DeleteOfFront(&list1); //头删
ShowSqlist(&list1); //输出
DeleteOfPos(&list1,2); //按位置删除
ShowSqlist(&list1); //输出
DeleteOfRear(&list1); //尾删
ShowSqlist(&list1); //输出
DestroyList(&list1); //销毁顺序表
return 0;
}
4. Output results