SeqList.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* array; //指向动态开辟的数组
size_t size; //顺序表的有效数据个数
size_t capacity; //顺序表的容量空间大小
}SL;
void SLInit(SL* ps); //顺序表初始化
void SLPrint(SL* ps); //打印顺序表
void SLPushBcak(SL* ps, SLDateType x); //顺序表尾插
void SLPopBack(SL* ps); //顺序表尾删
void SLPushFront(SL* ps, SLDateType x); //顺序表头插
void SLPopFront(SL* ps); //顺序表头删
int SLFind(SL* ps, SLDateType x); //顺序表查找
void SLInsert(SL* ps, size_t pos, SLDateType x); //顺序表在pos位置插入x
void SLErase(SL* ps, size_t pos); //顺序表删除pos位置的值
void SLDestory(SL* ps); //顺序表销毁
void SLModify(SL* ps, size_t pos, SLDateType y); //顺序表修改
text.c
#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void testSeqList1()
//testSeqList模块是为了完成某个模块就调试一下,比全部写好再调试更容易找出问题所在
{
SLDateType x;
SL sl;
SLInit(&sl);
printf("请输入你想要尾插的数");
scanf("%d", &x);
SLPushBcak(&sl, x);
SLPrint(&sl);
printf("请输入你想要尾插的数");
scanf("%d", &x);
SLPushBcak(&sl, x);
SLPrint(&sl);
}
void testSeqList2()
{
SL sl;
SLInit(&sl);
SLDateType x;
printf("请输入你想要尾插的数\n");
scanf("%d", &x);
SLPushBcak(&sl, x);
printf("请输入你想要尾插的数\n");
scanf("%d", &x);
SLPushBcak(&sl, x);
SLPrint(&sl);
SLPopBack(&sl);
SLPrint(&sl);
SLPopBack(&sl);
SLPrint(&sl);
SLPopBack(&sl);
SLPrint(&sl);
}
void testSeqList3()
{
SL sl;
SLInit(&sl);
SLPushBcak(&sl, 1);
SLPushBcak(&sl, 2);
SLPushBcak(&sl, 3);
SLPrint(&sl);
SLPushFront(&sl,4);
SLPrint(&sl);
}
void testSeqList4()
{
SL sl;
SLInit(&sl);
SLPushBcak(&sl, 3);
SLPushBcak(&sl, 4);
SLPushBcak(&sl, 5);
SLPrint(&sl);
SLPopFront(&sl);
SLPrint(&sl);
}
void testSeqList5()
{
SL sl;
SLInit(&sl);
SLPushBcak(&sl, 4);
SLPushBcak(&sl, 5);
SLPushBcak(&sl, 6);
SLPrint(&sl);
SLDateType x;
printf("请输入你想要删除的数\n");
scanf("%d", &x);
int pos = SLFind(&sl,x);
while (pos != -1) //可以删除重复的数字
{
SLErase(&sl, pos);
pos = SLFind(&sl, x);
}
SLPrint(&sl);
SLDateType y;
printf("请输入你想要修改的值和修改后的值\n");
scanf("%d%d", &x,&y);
pos = SLFind(&sl, x);
if (pos != -1)
{
SLModify(&sl, pos, y);
SLPrint(&sl);
}
else
{
printf("没找到\n");
}
}
void testSeqList6()
{
SL sl;
SLInit(&sl);
SLPushBcak(&sl, 4);
SLPushBcak(&sl, 5);
SLPushBcak(&sl, 6);
SLPrint(&sl);
SLInsert(&sl, 4, 8);
SLPrint(&sl);
}
void testSeqList7()
{
SL sl;
SLInit(&sl);
SLPushBcak(&sl, 4);
SLPushBcak(&sl, 5);
SLPushBcak(&sl, 6);
SLPrint(&sl);
SLErase(&sl, 1);
SLPrint(&sl);
}
void testSeqList8()
{
SL sl;
SLInit(&sl);
SLPushBcak(&sl, 4);
SLPushBcak(&sl, 5);
SLPushBcak(&sl, 6);
SLDestory(&sl);
SLPrint(&sl);
SLPopBack(&sl);
SLPrint(&sl);
SLPushFront(&sl, 9);
SLPrint(&sl);
SLPopFront(&sl);
SLPrint(&sl);
}
void menu()
{
printf("******************************\n");
printf("1.尾插 2.头插\n");
printf("3.尾删 4.头删\n");
printf("5.查找 6.删除指定位置的值\n");
printf("7.修改 8.打印\n");
printf("9.插入 10.删除指定的值\n");
printf("0.退出\n");
printf("******************************\n");
}
int main()
{
SL sl;
SLInit(&sl);
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
int num, x, y;
size_t pos;
switch(input)
{
case 1:
printf("请输入想要尾插的数,以-2结束:>\n");
scanf("%d", &num);
while (num != -2)
{
SLPushBcak(&sl, num);
scanf("%d", &num);
}
break;
case 2:
printf("请输入想要头插的数,以-2结束:>\n");
scanf("%d", &num);
while (num != -2)
{
SLPushFront(&sl, num);
scanf("%d", &num);
}
break;
case 3:
SLPopBack(&sl);
break;
case 4:
SLPopFront(&sl);
break;
case 5:
printf("请输入想要查找的数:>");
scanf("%d", &num);
pos = SLFind(&sl, num);
if (pos != -1)
{
printf("找到了,下标为:%d\n", pos);
}
else
{
printf("没找到:<\n");
}
break;
case 6:
printf("请输入要删除的值的位置\n");
scanf("%d", &pos);
SLErase(&sl, pos);
break;
case 7:
printf("请输入你想要修改的值和修改后的值\n");
scanf("%d%d", &x, &y);
pos = SLFind(&sl, x);
if (pos != -1)
{
SLModify(&sl, pos, y);
printf("已修改:>\n");
}
break;
case 8:
SLPrint(&sl);
break;
case 9:
printf("请输入在哪个位置插入什么值\n");
scanf("%d%d", &pos, &num);
SLInsert(&sl, pos, num);
break;
case 10:
printf("请输入你想要删除的值\n");
scanf("%d", &num);
int pos = SLFind(&sl, num);
while (pos != -1) //可以删除重复的数字
{
SLErase(&sl, pos);
pos = SLFind(&sl, num);
}
if (pos == -1)
{
printf("你想要删除的值已不存在了\n");
}
break;
case 0:
printf("退出\n");
break;
default:
printf("输出错误,请重新输入");
break;
}
} while (input);
return 0;
}
SeqList.c
#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SLInit(SL* ps) //顺序表初始化
{
assert(ps);
ps->array = NULL;
ps->size = ps->capacity = 0;
}
void checkCapacity(SL* ps) //顺序表扩容
{
assert(ps);
if (ps->size == ps->capacity)
{
int Newcapacity = ps->size == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = (SLDateType*)realloc(ps->array,Newcapacity * sizeof(SLDateType));
if (tmp == NULL)
{
perror("checkCapacity::realloc");
return;
}
ps->array = tmp;
ps->capacity = Newcapacity;
}
}
void SLPushBcak(SL* ps, SLDateType x) //顺序表尾插
{
//assert(ps);
//checkCapacity(ps);
//ps->array[ps->size] = x;
//ps->size++;
SLInsert(ps, ps->size, x); //使用SLInsert更简洁方便
}
void SLPrint(SL* ps) //顺序表打印
{
assert(ps);
size_t i = 0;
for (i = 0; i < ps->size; i++)
{
printf("%d ", ps->array[i]);
}
printf("\n");
}
void SLPopBack(SL* ps) //顺序表尾删
{
//assert(ps);
//assert(ps->size > 0);
//ps->size--;
SLErase(ps, ps->size - 1);
}
void SLPushFront(SL* ps, SLDateType x) //顺序表头插
{
//assert(ps);
//checkCapacity(ps);
//int end = ps->size-1;
//while(end >= 0)
//{
// ps->array[end + 1] = ps->array[end];
// end--;
//}
//ps->array[0] = x;
//ps->size++;
SLInsert(ps, 0, x);
}
void SLPopFront(SL* ps) //顺序表头删
{
//assert(ps);
//assert(ps->size > 0);
//size_t begin = 0;
//while (begin<ps->size-1)
//{
// ps->array[begin] = ps->array[begin+1];
// begin++;
//}
//ps->size--;
SLErase(ps, 0);
}
int SLFind(SL* ps, SLDateType x)
{
assert(ps);
size_t i = 0;
while (i < ps->size)
{
if (ps->array[i] == x)
{
return i; //返回下标,方便后面我们删除和修改顺序表
}
i++;
}
return -1;
}
void SLInsert(SL* ps, size_t pos, SLDateType x) //顺序表在pos位置插入x
{
assert(ps);
checkCapacity(ps);
size_t end = ps->size;
assert(pos >= 0 && pos <= ps->size);
while (end > pos)
{
ps->array[end] = ps->array[end - 1];
end--;
}
ps->array[pos] = x;
ps->size++;
}
void SLErase(SL* ps, size_t pos) //顺序表删除pos位置的值
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
while (pos < ps->size-1)
{
ps->array[pos] = ps->array[pos + 1];
pos++;
}
ps->size--;
}
void SLModify(SL* ps, size_t pos, SLDateType y)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->array[pos] = y;
}
void SLDestory(SL* ps) //销毁顺序表
{
assert(ps);
if (ps->array)
{
free(ps->array);
ps->array = NULL;
ps->size = ps->capacity = 0;
}
}
部分演示:
🙇感谢大家的阅读,如有错误请指出,我们下次再见。