顺序表的建立及其基本操作的实现
一、顺序表的基本定义及特点
顺序表,即线性表的顺序存储。它是用一组地址连续的存储单元依次存储线性表中的数据元素,从而使得逻辑上相邻的两个元素在物理位置上也相邻。因此,顺序表的特点是元素的逻辑顺序与其存储的物理顺序相同。
线性表的顺序存储结构:
每个数据元素的存储位置都和顺序表的起始位置相差一个和该数据元素的位序成正比的常数,因此,顺序表中的任意一个数据元素都可以随机存取,所以顺序表是一种随机存取的存储结构。
二、代码展示
1.代码实现
代码如下:
#include"stdio.h" //顺序表
#define L_sequence_Maxsize 10 //顺序表最大存储值
typedef int type_l;
typedef struct L_sequence {
type_l data[L_sequence_Maxsize]; //顺序表数据域
int length; //表长
}L_s;
void Initial_L_s(L_s &table) { //初始化
table.length = 0;
printf("顺序表已初始化\n");
};
bool InPut(L_s &table,int i,int data) { //插入
if (i < 1 || i > table.length + 1) {
printf("Error:lllegal location\n");
return false;
}
if (table.length >= L_sequence_Maxsize) {
printf("Error:Table full\n");
return false;
}
for (int j = table.length; j >= i; j--)
table.data[j] = table.data[j - 1];
table.data[i - 1] = data;
table.length++;
return true;
}
bool OutPut(L_s& table) { //输出
int num = 1;
type_l* ptr = &table.data[0];
while (ptr != &table.data[table.length]) {
printf("[%d]%d\n",num++, *(ptr++));
}
return true;
}
bool Delete(L_s &table) { //删除
int i = 0;
printf("Please enter the number:\n");
scanf_s("%d",&i);
if (i<1 || i>table.length) {
printf("Error:lllegal location\n");
return false;
}
for (int j = i - 1;j < table.length - 1;j++) {
table.data[j] = table.data[j+1];
}
table.length--;
return true;
}
bool Modify(L_s& table) { //修改
int i = 0;
printf("Please enter the number:\n");
scanf_s("%d",&i);
if (i<1 || i>table.length) {
printf("Error:lllegal location\n");
return false;
}
printf("[%d]is:%d\n",i,table.data[i-1]);
printf("Please enter the value\n");
scanf_s("%d", &table.data[i - 1]);
return true;
}
bool Air(L_s& table) { //清空
int i = 0;
while (table.length) {
table.data[i++] = 0;
table.length--;
}
return true;
}
int main()
{
L_s table;
type_l data = 0;
int i = 1;
bool SWITCH = true;
printf("顺序表已建立\n");
Initial_L_s(table);
do {
int choose = 0;
bool (*f)(L_s & table) = NULL;
printf("Operate:\n1.插入\t2.单次插入\t3.删除\t4.输出\t5.修改\t6.清空\t7.退出\n");
scanf_s("%d", &choose);
switch (choose)
{
case 1:
if(table.length == L_sequence_Maxsize)
printf("Error:Table full\n");
i = table.length + 1;
while (i != L_sequence_Maxsize + 1)
{
printf("INPUT[%d]:", i);
scanf_s("%d", &data);
InPut(table, i++, data);
}
break;
case 2:
printf("Please enter the number:\n");
scanf_s("%d", &i);
printf("Please enter the data:\n");
printf("INPUT[%d]:", i);
scanf_s("%d", &data);
InPut(table, i, data);
break;
case 3:
f = Delete;
break;
case 4:
f = OutPut;
break;
case 5:
f = Modify;
break;
case 6:
f = Air;
break;
case 7:
SWITCH = false;
break;
default:
printf("Operate not found\n");
break;
}
if (f)
{
if (f(table))
printf("Successful execution!\n");
else
printf("Execution failed!\n");
}
} while (SWITCH);
return 0;
}
2.输出展示
顺序表已建立
顺序表已初始化
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
1
INPUT[1]:1
INPUT[2]:2
INPUT[3]:3
INPUT[4]:4
INPUT[5]:5
INPUT[6]:6
INPUT[7]:7
INPUT[8]:8
INPUT[9]:9
INPUT[10]:10
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
3
Please enter the number:
3
Successful execution!
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
4
[1]1
[2]2
[3]4
[4]5
[5]6
[6]7
[7]8
[8]9
[9]10
Successful execution!
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
2
Please enter the number:
8
Please enter the data:
INPUT[8]:-32
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
4
[1]1
[2]2
[3]4
[4]5
[5]6
[6]7
[7]8
[8]-32
[9]9
[10]10
Successful execution!
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
5
Please enter the number:
1
[1]is:1
Please enter the value
43
Successful execution!
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
4
[1]43
[2]2
[3]4
[4]5
[5]6
[6]7
[7]8
[8]-32
[9]9
[10]10
Successful execution!
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
6
Successful execution!
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
2
Please enter the number:
1
Please enter the data:
INPUT[1]:12
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
4
[1]12
Successful execution!
Operate:
1.插入 2.单次插入 3.删除 4.输出 5.修改 6.清空 7.退出
7
D:\vstudio_code\Date_Struct\x64\Debug\Date_Struct.exe (进程 12328)已退出,代码为 0。
按任意键关闭此窗口. . .
结尾
注:该文仅为记录个人学习,如有错误或与您预期不符,欢迎各路大佬指出,万分感谢。