一、概述
1、主题:顺序表的初始化及增删改查实战练习(完整代码在末尾)
2、编程环境:CLion
3、使用语言:C、C++。本次实战中,为了降低编写难度,会使用C++的引用和bool类型。(C++对C是完全兼容的,所以代码格式可以和C一致,无需修改)
4、预备知识:C语言的基础语法以及C语言的数组、函数、指针、结构体和typedef关键字。以及C++的引用和布尔类型。
二、实战步骤代码及解析
(一)、 创建项目
1、在CLion中新建一个C++项目:
2、将C++中的头文件改为C的头文件
#include <stdio.h>
int main()
{
return 0;
}
(二)、顺序表的定义
1、宏定义一个全局变量用于存储线性表的长度。
#define MaxSize 50
2、将int类型自定义为ElemType,方便修改顺序表的类型。
typedef int ElemType;
3、定义结构体变量
typedef struct
{
ElemType data[MaxSize];
int length;//length存储数组的元素个数。
}SqList;
自定义结构体变量类型为SqList,结构体的成员为:1、ElemType类型数组data,大小为MaxSize。2、lengrth存储数组的元素个数,这里要区分数组长度(length)和线性表(MaxSize)的大小。
(三)、初始化顺表
1、主函数中相关变声明
SqList L;//声明结构体变量L
bool ret;//声明布尔类型的变量ret用于保存自定义函数的返回值。
2、初始化顺序表函数
/**
* 函数名:initList
* 函数功能:初始化顺序表
* 返回值类型:bool
* 参数列表:1、顺序表l。2、需要数组的长度i。*/
bool initList(SqList &l,int i)//这里要改变L,所以加引用。
{
//1、判断数组长度是否合法
if(i<1||i>MaxSize)
{
return false;
}
//2、for循环向数组中添加元素
for (int j = 0; j < i; j++) {
l.data[j]=j+1;
}
//3、保存数组的长度
l.length=i;
}
3、调用初始化函数对数组进行初始化
ret= initList(L,10);
if(ret)
{
printf("init success!\n");
printList(L);//调用打印数组函数
}else
{
printf("init false!\n");
}
4、初始化结果:
(四)、打印数组函数
1、定义打印数组函数
/**
* 函数名:printList
* 函数功能:打印数组元素
* 返回值类型:bool
* 参数列表:1、顺序表l。*/
bool printList(SqList l)
{
for (int i = 0; i < l.length; i++) {
printf("%d",l.data[i]);
}
printf("%d");
}
(五)、添加操作函数
1、定义插入操作函数
/**
* 函数名:listInsert
* 函数功能:完成线性表的插入操作
* 返回值类型:bool
* 参数列表:1、顺序表l。2、元素插入的位置loc。3、需要插入的元素值element*/
bool listInsert(SqList l,int loc,ElemType element)
{
//1、判断插入位置是否合法
if(loc<1||loc>l.length)
{
return false;
}
//2、判断存储空间是否满了
if(l.length==MaxSize)
{
return false;
}
//3、把插入位置后面的元素依次往后移
for (int j = l.length; j >=loc; j--) {
l.data[loc-1]=l.data[loc];
}
//4、空出的位置放入要插入的元素
l.data[loc-1]=element;
//5、线性表的长度加一
l.length+1;
return true;
}
2、调用插入函数进行插入操作
int insLoc;//需要插入元素的位置
ElemType insEle;//插入元素的值
printf("enter the insertLocal and element\n");
scanf("%d %d", &insLoc, &insEle);
ret = listInsert(L, insLoc, insEle);
if (ret) {
printf("insert success!\n");
printList(L);
} else {
printf("insert false!\n");
}
3、操作结果
(六)、删除操作函数
1、定义删除函数
/**
* 函数名:deleteList
* 函数功能:完成线性表的删除操作
* 返回值类型:bool
* 参数列表:1、顺序表l。2、元素插入的位置loc。*/
bool deleteList(SqList &l, int loc) {
if (loc < 1 || loc > l.length) {
return false;
}
for (int j = loc; j < l.length; j++) {
l.data[j - 1] = l.data[j];
}
l.length--;
return true;
}
2、调用删除函数
int delete_loc;
printf("Enter the location you want to delete\n");
scanf("%d", &delete_loc);//要删除元素的位置
ret = deleteList(L, delete_loc);
if (ret) {
printf("delete %d success!\n", delete_loc);
printList(L);
} else {
printf("false\n");
}
3、操作结果
三、整体代码
#include <stdio.h>
#define MaxSize 50
typedef int ElemType;//将int类型自定义为ElemType,方便修改顺序表的类型
typedef struct {
ElemType data[MaxSize];
int length;//存储数组的元素个数
} SqList;
bool initList(SqList &l, int i);
bool printList(SqList l);
bool listInsert(SqList &l, int loc, ElemType element);
bool deleteList(SqList &l, int loc);
int main() {
SqList L;//声明结构体变量L
bool ret;//声明布尔类型的变量ret用于保存自定义函数的返回值。
ret = initList(L, 10);
if (ret) {
printf("init success!\n");
printList(L);
} else {
printf("init false!\n");
}
int insLoc;//需要插入元素的位置
ElemType insEle;//插入元素的值
printf("enter the insertLocal and element\n");
scanf("%d %d", &insLoc, &insEle);
ret = listInsert(L, insLoc, insEle);
if (ret) {
printf("insert success!\n");
printList(L);
} else {
printf("insert false!\n");
}
int delete_loc;
printf("Enter the location you want to delete\n");
scanf("%d", &delete_loc);//要删除元素的位置
ret = deleteList(L, delete_loc);
if (ret) {
printf("delete %d success!\n", delete_loc);
printList(L);
} else {
printf("false\n");
}
return 0;
}
/**
* 函数名:initList
* 函数功能:初始化顺序表
* 返回值类型:bool
* 参数列表:1、顺序表l。2、需要顺序表的大小i。*/
bool initList(SqList &l, int i) {
if (i < 1 || i > MaxSize) {
return false;
}
for (int j = 0; j < i; j++) {
l.data[j] = j + 1;
}
l.length = i;
return true;
}
/**
* 函数名:printList
* 函数功能:打印数组元素
* 返回值类型:bool
* 参数列表:1、顺序表l。*/
bool printList(SqList l) {
for (int i = 0; i < l.length; i++) {
printf("%3d", l.data[i]);
}
printf("\n");
}
/**
* 函数名:listInsert
* 函数功能:完成线性表的插入操作
* 返回值类型:bool
* 参数列表:1、顺序表l。2、元素插入的位置loc。3、需要插入的元素值element*/
bool listInsert(SqList &l, int loc, ElemType element) {
//1、判断插入位置是否合法
if (loc < 1 || loc > l.length) {
return false;
}
//2、判断存储空间是否满了
if (l.length == MaxSize) {
return false;
}
//3、把插入位置后面的元素依次往后移
for (int j = l.length; j >= loc; j--) {
l.data[j] = l.data[j - 1];
}
//4、空出的位置放入要插入的元素
l.data[loc - 1] = element;
//5、线性表的长度加一
l.length + 1;
return true;
}
/**
* 函数名:deleteList
* 函数功能:完成线性表的删除操作
* 返回值类型:bool
* 参数列表:1、顺序表l。2、元素插入的位置loc。*/
bool deleteList(SqList &l, int loc) {
if (loc < 1 || loc > l.length) {
return false;
}
for (int j = loc; j < l.length; j++) {
l.data[j - 1] = l.data[j];
}
l.length--;
return true;
}
如有错误,敬请指正!