顺序表的基本操作:增删改查(含全部代码,C语言实现)——数据结构线性表
数据结构学习基础---基本操作,C语言。1顺序表定义;2顺序表初始化;3检查增容;4销毁顺序表;5尾插;6尾删;7头插;8头删;9打印元素;10查找元素返回下标;11指定元素插入;12删除指定元素;完整代码
·
部分同学用的是Visual Studio。为了解决scanf函数在vs中报错的问题,需要在你的源文件第一行放这行代码(文章最后有完整代码)
#define _CRT_SECURE_NO_WARNINGS 1
顺序表的代码实现
1.顺序表的定义
定义结构体类型;结构体外面的typedef int SLDataType:用typedef声名int类型,是为了方便修改结构体中的存放的数据类型。
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size;//数组中存储的数据个数
int capacity;//表示最大容量
}SL;
注:定义结构体变量,后面全部代码的传值调用和传址调用一定要注意要区分开来:例如:
SL sl;//定义结构体变量
SeqListPopFront(&sl);
2.顺序表的初始化
void SeqListInit(SL* ps)
{
ps->a = NULL;
ps->size = ps->capacity = 0;
}
3.检查增容
注意,这里传址调用,所以这里的传入的参数为SL*ps
ps->size == ps->capacity:如果最大容量和存储数据相同(表示整个顺序表没有空间或者初始化都为0,则需要扩容;其他情况就不用扩容)
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;三目运算符用法(如果最大容量为0那么新的容量就为4否则新容量就为老容量的两倍)
realloc动态内存申请函数
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//检查增容
void SeqListCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
}
4.销毁顺序表
void SeqListDestory(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
5尾插入元素
//尾插
void SeqListPushBack(SL* ps, SLDataType x)
{
SeqListCheckCapacity(ps);//检查容量,自动扩容
//若空间足够可直接插入数据即可
ps->a[ps->size] = x;
ps->size++;
}
6.尾删除元素
void SeqListPopBack(SL* ps)
{
assert(ps->size > 0);
ps->size--;
}
7.头插入元素
void SeqListPushFront(SL* ps, SLDataType x)
{
//检查增容
SeqListCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
}
8.头删除元素
void SeqListPopFront(SL* ps)
{
assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}
9.打印元素
void SeqListPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
10.查找元素返回下标
//找到了返回x位置下标,没找到返回-1
int SeqListFind(SL* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
11.指定pos下标进行位置插入
//指定pos下标位置插入
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
assert(pos <= ps->size && pos >= 0);
SeqListCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
12.删除pos位置的数据
void SeqListErase(SL* ps, int pos)
{
assert(pos >= 0 && pos < ps->size);
int begin = pos;
while (begin < ps->size - 1)
{
ps->a[begin] = ps->a[begin + 1];
begin++;
}
ps->size--;
}
全部代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size;//数组中存储的数据个数
int capacity;//表示最大容量
}SL;
void SeqListPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListInit(SL* ps)
{
ps->a = NULL;
ps->size = ps->capacity = 0;
}
//检查增容
void SeqListCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
}
void SeqListDestory(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
//尾插
void SeqListPushBack(SL* ps, SLDataType x)
{
SeqListCheckCapacity(ps);
//3.空间足够,直接插入数据即可
ps->a[ps->size] = x;
ps->size++;
//SeqListInsert(ps, ps->size, x);
}
//尾删
void SeqListPopBack(SL* ps)
{
assert(ps->size > 0);
ps->size--;
//SeqListErase(ps, ps->size - 1);
}
//头插
void SeqListPushFront(SL* ps, SLDataType x)
{
//检查增容
SeqListCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
//SeqListInsert(ps, 0, x);
}
//头删
void SeqListPopFront(SL* ps)
{
assert(ps->size > 0);
//挪动数据
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
//SeqListErase(ps, 0);
}
//找到了返回x位置下标,没找到返回-1
int SeqListFind(SL* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
//指定pos下标位置插入
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
assert(pos <= ps->size && pos >= 0);
SeqListCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
//删除pos位置的数据
void SeqListErase(SL* ps, int pos)
{
assert(pos >= 0 && pos < ps->size);
int begin = pos;
while (begin < ps->size - 1)
{
ps->a[begin] = ps->a[begin + 1];
begin++;
}
ps->size--;
}
void TestSeqList1()
{
SL sl;
//初始化
SeqListInit(&sl);
printf("这是第1组数据 ");
SeqListPushBack(&sl, 1);
SeqListPushBack(&sl, 2);
SeqListPushBack(&sl, 3);
SeqListPushBack(&sl, 4);
SeqListPushBack(&sl, 5);
SeqListPrint(&sl);
printf("这是第2组数据 ");
SeqListPopBack(&sl);
SeqListPopBack(&sl);
SeqListPopBack(&sl);
SeqListPushBack(&sl, 4);
SeqListPushBack(&sl, 4);
SeqListPrint(&sl);
printf("这是第3组数据 ");
//元素查找函数,返回下标
int pos = SeqListFind(&sl, 4);
if (pos != -1)
{
//指定pos下标进行位置插入
SeqListInsert(&sl, pos, 40);
}
SeqListPrint(&sl);
printf("这是第4组数据 ");
SeqListInsert(&sl, 0, -1);
SeqListPrint(&sl);
printf("这是第5组数据 ");
//元素查找,返回下标
int find = SeqListFind(&sl, 99);
printf("find=%d", find);
SeqListDestory(&sl);
}
void Menu()
{
printf("**********************************\n");
printf(" 请选择你的操作:>\n");
printf("1,头插 2,头删\n");
printf("3,尾插 4,尾删\n");
printf("5,打印 -1,退出\n");
printf("**********************************\n");
}
void MenuTest()
{
SL sl;
SeqListInit(&sl);
int input = 0;
int x;
while (input != -1)
{
Menu();
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入你要头插的数据,以-1结束\n");
scanf("%d", &x);
while (x != -1)
{
SeqListPushFront(&sl, x);
scanf("%d", &x);
}
break;
case 2:
printf("头删\n");
SeqListPopFront(&sl);
break;
case 3:
printf("请输入你要尾插的数据,以-1结束\n");
scanf("%d", &x);
while (x != -1)
{
SeqListPushBack(&sl, x);
scanf("%d", &x);
}
break;
case 4:
printf("尾删\n");
SeqListPopBack(&sl);
break;
case 5:
SeqListPrint(&sl);
break;
case 6:
break;
default:
printf("无此选项,请重新输入\n");
break;
}
}
SeqListDestory(&sl);
}
int main()
{
//(删除pos位置的数据; 指定pos下标位置插入; 找到了返回x位置下标,没找到返回-1)这三个函数可以自己测试添加
//TestSeqList1();//这里大家自行尝试调试,
MenuTest();
return 0;
}

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)