代码模块分为三部分:
1.test.c代码测试模块
2.SeqList.c函数实现模块
3.SeqList.h函数声明模块
代码测试环境:VS2022
test.c部分代码:
#include "SeqList.h"
int main()
{
SL s ;
s.a = NULL;
SLInit(&s);
int input = 0;
int elem = 0;
int pos = 0;
do
{
menu();
printf("请输入你的选项:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入你要插入的数据:>");
scanf("%d", &elem);
SLPushFront(&s, elem);
SLPrint(&s);
break;
case 2:
printf("请输入你要插入的数据:>");
scanf("%d", &elem);
SLPushBack(&s, elem);
SLPrint(&s);
break;
case 3:
SLPopBack(&s);
SLPrint(&s);
break;
case 4:
SLPopFront(&s);
SLPrint(&s);
break;
case 5:
printf("请输入你要插入的数据:>");
scanf("%d", &elem);
printf("请输入你要插入的位置:>");
scanf("%d", &pos);
SLInsert(&s, pos, elem);
SLPrint(&s);
break;
case 6:
printf("请输入你要删除的元素的位置:>");
scanf("%d", &pos);
SLErase(&s, pos);
SLPrint(&s);
break;
case 7:
printf("请输入你要修改的元素的位置:>");
scanf("%d", &pos);
printf("请输入修改后元素的值:>");
scanf("%d", &elem);
SLModify(&s, pos, elem);
SLPrint(&s);
break;
case 8:
printf("请输入你要查找的元素值:>");
scanf("%d", &elem);
SLFind(&s, elem);
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
SLDestroy(&s);
return 0;
}
SeqList.h部分代码:
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SQLDataType;
typedef struct SeqList
{
SQLDataType* a;
int size;
int capacity;
}SL;
void menu();
void SLInit(SL* psl);//初始化顺序表
void SLPrint(SL* psl);//打印顺序表
void SLDestroy(SL* psl);//销毁顺序表
void SLPushBack(SL* psl, SQLDataType x);//尾插元素
void SLPushFront(SL* psl, SQLDataType x);//前插
void SLPopBack(SL* psl);//尾删
void SLPopFront(SL* psl);//前删
void SLInsert(SL* psl, int pos, SQLDataType x);//随机位置插入元素
void SLErase(SL* psl, int pos);//随机删除
int SLFind(SL* psl, SQLDataType x);//查找
void SLModify(SL* psl, int pos, SQLDataType x);//修改
SeqList.c部分代码:
#include "SeqList.h"
void menu()
{
printf("**************************\n");
printf("*** 1.头插 2.尾插 ****\n");
printf("*** 3.尾删 4.前删 ****\n");
printf("*** 5.随机插入6.随机删除***\n");
printf("*** 7.修改 8.查找 ****\n");
printf("*** 0.退出 ****\n");
printf("**************************\n");
}
void SLInit(SL* psl)
{
psl->a = (SQLDataType*)malloc(sizeof(SQLDataType) * 4);
if (psl->a == NULL)
{
perror("malloc fail");
return;
}
psl->size = 0;
psl->capacity =4;
}
void SLCheckCapacity(SL* psl)
{
if (psl->size == psl->capacity)
{
SQLDataType* tmp = realloc(psl->a, sizeof(SQLDataType) * psl->capacity * 2);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
psl->a = tmp;//空间变为新开辟的空间
psl->capacity *= 2;//容量变为二倍
}
}
void SLPrint(SL* psl)
{
int i = 0;
for (i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
void SLDestroy(SL* psl)
{
free(psl->a);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
void SLPushBack(SL* psl, SQLDataType x)
{
SLCheckCapacity(psl);
psl->a[psl->size] = x;
psl->size++;
}
void SLPushFront(SL* psl, SQLDataType x)
{
SLCheckCapacity(psl);
int end = psl->size - 1;
while (end >= 0)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[0] = x;
psl->size++;
}
void SLPopBack(SL* psl)
{
assert(psl->size>0);//保证删除的元素个数不小于0
psl->size--;
}
void SLPopFront(SL* psl)
{
assert(psl->size > 0);
//所有元素向前覆盖
int start = 0;
while (start < psl->size - 1)
{
psl->a[start] = psl->a[start + 1];
start++;
}
psl->size--;
}
void SLInsert(SL* psl, int pos, SQLDataType x)
{
assert(0 <= pos && pos <= psl->size);
SLCheckCapacity(psl);
int end = psl->size - 1;
while (end >= pos)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[pos] = x;
psl->size++;
}
void SLErase(SL* psl, int pos)
{
assert(0 <= pos && pos < psl->size);
int start = pos + 1;
while (start < psl->size)
{
psl->a[start - 1] = psl->a[start];
start++;
}
psl->size--;
}
int SLFind(SL* psl, SQLDataType x)
{
int i = 0;
for (i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
{
return i;
}
}
return -1;
}
void SLModify(SL* psl, int pos, SQLDataType x)
{
assert(0 <= pos && pos < psl->size);
psl->a[pos] = x;
}