一 顺序表的结构体定义和函数声明
#include<iostream>
using namespace std;
#define ElemType int // 自定义
#define Ext 30
// 动态初始化
typedef struct seqList
{
ElemType* data; // 指示动态分配数组的指针
ElemType Maxsize,length; // 最大容量 和 当前长度
};
#define Maxsize 10// 函数声明
void Init_List(seqList &L); // 初始化函数
void Rev_List(seqList L); // 遍历函数
void List_Insert(seqList &L, ElemType f, ElemType a); // 在指定位置插入 a 操作
ElemType List_delete(seqList &L, ElemType f); // 在指定位置删除操作
ElemType List_find(seqList L, ElemType f); // 按序号查找
ElemType List_find2(seqList L, ElemType a); // 按值查找
二 顺序表的操作
2.1 顺序表的插入操作
void List_Insert(seqList &L, ElemType f, ElemType a); // 在指定位置插入 a 操作
void List_Insert(seqList &L, ElemType f,ElemType a)
{
if(f < 1 || f > L.length+1)
{
printf("插入位置有误,请重新输入\n");
exit(-1);
}
if(f >= Maxsize)
{
printf("顺序表空间已满\n");
exit(-1);
}
for(int i = L.length; i >= f; i--)
{
L.data[i] = L.data[i-1];
}
L.data[f-1] = a;
L.length++;
}int main()
{
ElemType a = 0; // 插入的位置
ElemType b = 0; // 插入的元素
seqList L; // 定义了一个顺序表
Init_List(L);
Rev_List(L);
printf("\n");
printf("请输入插入的位置 插入的元素\n");
scanf("%d,%d",&a,&b);
List_Insert(L, a, b);
Rev_List(L);}
2.2 插入操作的演示图片
2.3 顺序表的删除操作
ElemType List_delete(seqList &L, ElemType f); // 在指定位置删除操作
ElemType List_delete(seqList &L, ElemType f)
{
int a = 0;
a = L.data[f-1]; // 查看被删除的元素
if(f < 1 || f >= L.length)
{
printf("删除位置有误,请重新输入\n");
exit(-1);
}
for(int i = f; i < L.length; i++)
{
L.data[i-1] = L.data[i];
}
L.length--;
return a;
}int main(void)
{
ElemType c = 0; // 删除的位置
seqList L; // 定义了一个顺序表
Init_List(L);
Rev_List(L);
printf("\n");
printf("请输入删除的位置\n");
scanf("%d",&c);
int b = List_delete(L,c);
printf("删除的元素为 %d \n", b);
Rev_List(L);}
2.4 删除操作的演示
2.5 顺序表的按序号查找
ElemType List_find(seqList L, ElemType f)
{
ElemType a = 0;
if(f < 1 || f > L.length)
{
printf("你输入的查找位置有误,请重新输入\n");
exit(-1);
}
a = L.data[f-1]; // 所查找位置上的值赋给a
return a;
}int main(void)
{
ElemType e = 0; // 查询的序号
seqList L; // 定义了一个顺序表
Init_List(L);
Rev_List(L);
printf("请输入查询的序号\n");
scanf("%d",&e);
ElemType f = List_find(L,e);
printf("所查找的元素为 %d \n", f);}
2.6 按序号查找演示结果
2.7 顺序表按值查找
ElemType List_find2(seqList L, ElemType a)
{
for(int i = 0; i < L.length; i++)
{
if(L.data[i] == a)
{
return i;
}
}
return -1;
}int main()
{
seqList L; // 定义了一个顺序表
Init_List(L);
Rev_List(L);
printf("\n");printf("请输入查找的元素\n");
ElemType a = 0;
scanf("%d",&a);
ElemType d = List_find2(L,a);
if(d == -1)
{
printf("查找失败,顺序表中无当前查找数据\n");
}
else
{
printf("查找成功 查找的数据下标为 %d \n", d);
}}
2.8 按值查找的结果演示
三 总代码如下
#include<iostream>
using namespace std;
#define ElemType int
#define Ext 30
// 动态初始化
typedef struct seqList
{
ElemType* data; // 指示动态分配数组的指针
ElemType Maxsize,length; // 最大容量 和 当前长度
};
#define Maxsize 10
// 函数声明
void Init_List(seqList &L); // 初始化函数
void Rev_List(seqList L); // 遍历函数
void List_Insert(seqList &L, ElemType f, ElemType a); // 在指定位置插入 a 操作
ElemType List_delete(seqList &L, ElemType f); // 在指定位置删除操作
ElemType List_find(seqList L, ElemType f); // 按序号查找
ElemType List_find2(seqList L, ElemType a); // 按值查找int main(void)
{
ElemType a = 0; // 插入的位置
ElemType b = 0; // 插入的元素
ElemType c = 0; // 删除的位置
ElemType e = 0; // 查询的序号
seqList L; // 定义了一个顺序表
Init_List(L);
Rev_List(L);
printf("\n");
printf("请输入插入的位置 插入的元素\n");
scanf("%d,%d",&a,&b);
List_Insert(L, a, b);
Rev_List(L);
printf("请输入删除的位置\n");
scanf("%d",&c);
int h = List_delete(L,c);
printf("删除的元素为 %d \n", h);
Rev_List(L);
printf("请输入查询的序号\n");
scanf("%d",&e);
ElemType f = List_find(L,e);
printf("所查找的元素为 %d \n", f);
printf("请输入查找的元素\n");
ElemType g = 0;
scanf("%d",&g);
ElemType d = List_find2(L,g);
if(d == -1)
{
printf("查找失败,顺序表中无当前查找数据\n");
}
else
{
printf("查找成功 查找的数据下标为 %d \n", d);
}
}void Init_List(seqList &L)
{
int len = 0; // 长度
L.data = (ElemType*)malloc(Maxsize * sizeof(ElemType)); // 动态开辟内存
if(L.data == NULL)
{
printf("开辟空间失败\n");
exit(0); // 退出程序
}
printf("请输入顺序表的长度\n");
scanf("%d",&(len));if(len > Maxsize ) // len > Maxsize 内存会不够 程序会崩
{
int * Newp = (int *)malloc(Ext * sizeof(int)); // 重新开辟空间
L.data = Newp;
}L.length = len;
for(int i = 0; i < len; i++)
{
printf("第 %d 个元素内容为\n", i+1);
scanf("%d",&(L.data[i]));
}
}void Rev_List(seqList L)
{
for(int i = 0; i < L.length; i++)
{
printf("第 %d 个元素内容为 %d \n", i+1,L.data[i]);
}
}void List_Insert(seqList &L, ElemType f,ElemType a)
{
if(f < 1 || f > L.length+1)
{
printf("插入位置有误,请重新输入\n");
exit(-1);
}
if(f >= Maxsize)
{
printf("顺序表空间已满\n");
exit(-1);
}
for(int i = L.length; i >= f; i--)
{
L.data[i] = L.data[i-1];
}
L.data[f-1] = a;
L.length++;
}ElemType List_delete(seqList &L, ElemType f)
{
int a = 0;
a = L.data[f-1]; // 查看被删除的元素
if(f < 1 || f >= L.length)
{
printf("删除位置有误,请重新输入\n");
exit(-1);
}
for(int i = f; i < L.length; i++)
{
L.data[i-1] = L.data[i];
}
L.length--;
return a;
}ElemType List_find(seqList L, ElemType f)
{
ElemType a = 0;
if(f < 1 || f > L.length)
{
printf("你输入的查找位置有误,请重新输入\n");
exit(-1);
}
a = L.data[f-1]; // 所查找位置上的值赋给a
return a;
}ElemType List_find2(seqList L, ElemType a)
{
for(int i = 0; i < L.length; i++)
{
if(L.data[i] == a)
{
return i;
}
}
return -1;
}
四 总结
1 顺序表的插入和删除操作是类似的,关于插入和删除需要判断插入位置的合法性,其次就是把数组里面的元素移动即可
2 由于顺序表是用数组存起来的,所以查找操作很容易想到