代码实现顺序表的操作函数
静态顺序表
头文件实现:
//值类型
typedef int DataType;
typedef int DataType;
typedef struct SeqList {
DataType data; // 值
int size; // 数量
} SeqList;
源文件实现:
// 1、初始化
void SeqListInit(SeqList *pSL);
// 2、尾部插入
void SeqListPushBack(SeqList *pSL, DataType data);
// 3、头部插入
void SeqListPushFront(SeqList *pSL, DataType data);
// 4、按下标插入,pos 的范围是 [0, size]
void SeqListInsert(SeqList *pSL, int pos, DataType data);
// 5、尾部删除
void SeqListPopBack(SeqList *pSL);
// 6、头部删除
void SeqListPopFront(SeqList *pSL);
// 7、按下标删除,pos 的范围是 [0, size)
void SeqListErase(SeqList *pSL, int pos);
// 8、按值删除,只删遇到的第一个
void SeqListRemove(SeqList *pSL, DataType data);
// 9、按值删除,删除所有的
void SeqListRemoveAll(SeqList *pSL, DataType data);
//10、 清空
void SeqListClear(SeqList *pSL);
// 11、按值查找,返回第一个找到的下标,如果没找到,返回 -1
int SeqListFind(SeqList *pSL, DataType data);
// 12、判断是否为空,1 表示空, 0 表示不空
int SeqListEmpty(SeqList *pSL);
// 13、返回数量
int SeqListSize(SeqList *pSL);
// 14、销毁
void SeqListDestroy(SeqList *pSL);
void SeqListInsert(SeqList *pSL, int pos, DataType data);
// 5、尾部删除
void SeqListPopBack(SeqList *pSL);
// 6、头部删除
void SeqListPopFront(SeqList *pSL);
// 7、按下标删除,pos 的范围是 [0, size)
void SeqListErase(SeqList *pSL, int pos);
// 8、按值删除,只删遇到的第一个
void SeqListRemove(SeqList *pSL, DataType data);
// 9、按值删除,删除所有的
void SeqListRemoveAll(SeqList *pSL, DataType data);
//10、 清空
void SeqListClear(SeqList *pSL);
// 11、按值查找,返回第一个找到的下标,如果没找到,返回 -1
int SeqListFind(SeqList *pSL, DataType data);
// 12、判断是否为空,1 表示空, 0 表示不空
int SeqListEmpty(SeqList *pSL);
// 13、返回数量
int SeqListSize(SeqList *pSL);
// 14、销毁
void SeqListDestroy(SeqList *pSL);
.h文件
#include<stdio.h>
#include<assert.h>
#include<Windows.h>
typedef int DataType;
#define MAX_SIZE 100
typedef struct seqList{
DataType arr[MAX_SIZE];//顺序表
int size;//代表当前顺序表的有效数字
}SeqList;
.c文件
#include"seqList.h"
//初始化
void SeqListInit(SeqList *pSL)
{
assert(pSL != NULL);
pSL->size = 0;
}
//销毁
void SeqListDestroy(SeqList *pSL)
{
assert(pSL != NULL);
pSL->size = 0;
}
// 清空
void SeqListClear(SeqList *pSL)
{
assert(pSL != NULL);
int i = 0;
while (i < pSL->size){
pSL->arr[i] = 0;
}
pSL->size = 0;
}
// 判断是否为空,1 表示空, 0 表示不空
int SeqListEmpty(SeqList *pSL)
{
if (pSL == NULL){
return 1;
}
return 0;
}
// 返回数量
int SeqListSize(SeqList *pSL)
{
return pSL->size;
}
//增删改查
//增
//尾插
void SeqListPushBack(SeqList *pSL, DataType date)
{
assert(pSL != NULL);
assert(pSL->size < MAX_SIZE);
pSL->arr[pSL->size] = date;
pSL->size++;
}
//头插
void SeqListPushHead(SeqList *pSL, DataType date)
{
assert(pSL != NULL);
assert(pSL->size < MAX_SIZE);
//根据循环条件
//1.1
#if 0
int space;
for (space = pSL->size; space > 0; space--){
pSL->arr[space] = pSL->arr[space - 1];
}
#endif
//1.2
#if 0
int space;
for (space = pSL->size - 1; space >= 0; space--){
pSL->arr[space + 1] = pSL->arr[space];
}
#endif
//2
int i;
for (i = 0; i < pSL->size; i++){
pSL->arr[pSL->size - i] = pSL->arr[pSL->size - 1 - i];
}
pSL->arr[0] = date;
pSL->size++;
}
//根据指定位置插入
void SeqListInsert(SeqList *pSL, int pos, DataType date)
{
assert(pSL != NULL);
assert(pSL->size < MAX_SIZE);
assert(pos >= 0 && pos <= pSL->size);
int space;
for (space = pSL->size; space > pos; space--){
pSL->arr[space] = pSL->arr[space - 1];
}
pSL->arr[pos] = date;
pSL->size++;
}
//查询
int SeqListFind(SeqList *pSL, DataType date)
{
assert(pSL != NULL);
int i = 0;
for (i = 0; i < pSL->size; i++){
if (pSL->arr[i] == date){
return i;
}
}
return -1;
}
//删
//尾删
void SeqListDeleteBack(SeqList *pSL)
{
assert(pSL != NULL);
assert(pSL->size > 0);
pSL->size--;
}
//头删
void SeqListDeleteHead(SeqList *pSL)
{
assert(pSL != NULL);
assert(pSL->size > 0);
int pos;
for (pos = 1; pos < pSL->size; pos++){
pSL->arr[pos - 1] = pSL->arr[pos];
}
pSL->size--;
}
//根据指定位置删除
void SeqListDelete(SeqList *pSL, int pos)
{
assert(pSL != NULL);
assert(pSL->size > 0);
int space;
for (space = pos; space < pSL->size - 1; space++){
pSL->arr[space] = pSL->arr[space + 1];
}
pSL->size--;
}
//根据数据删除,只删除第一个
void SeqListDeleteOne(SeqList *pSL, DataType date)
{
assert(pSL != NULL);
assert(pSL->size > 0);
int pos = SeqListFind(pSL, date);
if (pos != -1){
SeqListDelete(pSL, pos);
}
}
//根据数据删除,删除所有
void SeqListDeleteAll(SeqList *pSL, DataType date)
{
assert(pSL != NULL);
assert(pSL->size > 0);
int i, j;
for (i = 0, j = 0; i < pSL->size; i++){
if (pSL->arr[i] != date){
pSL->arr[j] = pSL->arr[i];
j++;
}
}
pSL->size = j;
}
//修改
void SeqListUpdate(SeqList *pSL, int pos, DataType date)
{
assert(pSL != NULL);
pSL->arr[pos] = date;
}
void SeqListPrint(SeqList *pSL)
{
int i = 0;
for (i = 0; i < pSL->size; i++){
printf("%d ", pSL->arr[i]);
}
printf("\n");
}
void mySwap(int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
//选择排序
void SeqListSort(SeqList *pSL)
{
int minSize = 0;
int maxSize = pSL->size - 1;
int minIndex;
int maxIndex;
int i;
while (minSize < maxSize){
minIndex = minSize;
maxIndex = minSize;
for (i = minSize + 1; i <= maxSize; i++){
if (pSL->arr[i] < pSL->arr[minIndex]){
minIndex = i;
}
if (pSL->arr[i] > pSL->arr[maxIndex]){
maxIndex = i;
}
}
if (minIndex != minSize){
mySwap(&(pSL->arr[minIndex]), &(pSL->arr[minSize]));
}
if (minSize == maxIndex){
maxIndex = minIndex;
}
if (maxIndex != maxSize){
mySwap(&(pSL->arr[maxIndex]), &(pSL->arr[maxSize]));
}
minSize++;
maxSize--;
}
}
int main()
{
SeqList seqlist;
SeqListInit(&seqlist);
/*SeqListPushBack(&seqlist, 1);
SeqListPushBack(&seqlist, 2);
SeqListPrint(&seqlist);
SeqListPushHead(&seqlist, 7);
SeqListPushHead(&seqlist, 7);
SeqListPrint(&seqlist);
SeqListInsert(&seqlist, 0, 7);
SeqListInsert(&seqlist, 0, 12);
SeqListInsert(&seqlist, 0, 13);
SeqListInsert(&seqlist, 0, 7);
SeqListPrint(&seqlist);*/
/*SeqListDeleteHead(&seqlist);
SeqListDeleteHead(&seqlist);
SeqListPrint(&seqlist);
SeqListDeleteBack(&seqlist);
SeqListDeleteBack(&seqlist);
SeqListPrint(&seqlist);
SeqListDelete(&seqlist, 1);
SeqListDelete(&seqlist, 2);
SeqListPrint(&seqlist);*/
/*SeqListDeleteOne(&seqlist, 7);
SeqListDeleteAll(&seqlist, 7);
SeqListPrint(&seqlist);*/
/*SeqListUpdate(&seqlist, 1, 10);
SeqListPrint(&seqlist);*/
//printf("%d\n", SeqListFind(&seqlist, 13));
/*int a = 5;
int b = 5;
a ^= b;
b ^= a;
a ^= b;
printf("%d %d", a, b);*/
SeqListPushBack(&seqlist, 3);
SeqListPushBack(&seqlist, 8);
SeqListPushBack(&seqlist, 9);
SeqListPushBack(&seqlist, 0);
SeqListPushBack(&seqlist, 7);
SeqListPushBack(&seqlist, 1);
SeqListPrint(&seqlist);
SeqListSort(&seqlist);
SeqListPrint(&seqlist);
SeqListDestroy(&seqlist);
SeqListPrint(&seqlist);
system("pause");
return 0;
}
运行结果:
![](https://i-blog.csdnimg.cn/blog_migrate/69d944ca1c1fa09ea8aefe53b65e0373.png)
动态顺序表
与静态顺序表不同的是,需要考虑
扩容的问题
.h文件
#pragma once//防止头文件重定义
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
typedef int DataType;
typedef struct SeqListDynamic{
DataType *array;
int capacity;
int size;//当前有效数据个数
}SeqListD;
#define CAPACITY 2
.c文件
#include"SeqListD.h"
//初始化
void SeqListDInit(SeqListD *pSLD)
{
assert(pSLD);
pSLD->capacity = CAPACITY;
pSLD->size = 0;
pSLD->array = (DataType *)malloc(pSLD->capacity * sizeof(DataType));
assert(pSLD->array != NULL);
}
//销毁
void SeqListDDestroy(SeqListD *pSLD)
{
assert(pSLD);
pSLD->size = 0;
pSLD->capacity = 0;
}
//扩容
void ExpandIfRequired(SeqListD *pSLD)
{
if (pSLD->size < pSLD->capacity){
return;
}
//扩容
/*:realloc函数的功能
1、容量增大
2、开辟新容量的空间
3、将原数据一致新空间
4、释放原空间
5、将新空间挂给pSLD
*/
//1、
pSLD->capacity *= 2;
//2、伙伴算法:扩充二倍
DataType * newarray = (DataType*)malloc(pSLD->capacity * sizeof(DataType));
assert(pSLD->array != NULL);
//3、
int i;
for (i = 0; i < pSLD->size; i++){
newarray[i] = pSLD->array[i];
}
//4、
free(pSLD->array);
//5、
pSLD->array = newarray;
}
//尾插
void SeqListDPushBack(SeqListD *pSLD, DataType date)
{
assert(pSLD);
ExpandIfRequired(pSLD);
pSLD->array[pSLD->size] = date;
pSLD->size++;
}
void SeqListDPrint(SeqListD *pSLD)
{
int i = 0;
for (i = 0; i < pSLD->size; i++){
printf("%d ", pSLD->array[i]);
}
printf("\n");
}
int main()
{
SeqListD seqlistd;//结构体
SeqListDInit(&seqlistd);//结构体指针传参
SeqListDPushBack(&seqlistd, 2);
SeqListDPushBack(&seqlistd, 3);
SeqListDPushBack(&seqlistd, 4);
SeqListDPushBack(&seqlistd, 5);
SeqListDPrint(&seqlistd);
SeqListDDestroy(&seqlistd);
SeqListDPrint(&seqlistd);
system("pause");
return 0;
}
实现扩容问题:
![](https://i-blog.csdnimg.cn/blog_migrate/6cffe0ba102cb73b4cbd97ddff86b9b8.png)