#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define Max_Length 100 // 顺序表最大长度
#define Increment_Length 10 // 顺序表存储空间分配增量
typedef int ElemType; // ElemType为顺序表的元素类型
#define OK 0 //成功执行
#define Err_Memory -1 //内存分配错误
#define Err_OverFlow -2 //溢出
#define Err_IllegalPos -3 //非法插入位置
typedef struct {
ElemType *data; // data指向存储数据元素的一维数组,初始大小为Max_Length
int Length; // 顺序表的实际长度,其值小于等于ListLength
int ListLength; // 当前顺序表分配的空间大小,初始值为Max_Length
}SeqList;
// 菜单显示界面封装函数
void show_Menu() {
cout << " ***** 欢迎使用顺序表实验演示系统 ***** " << endl;
cout << " ***** 1、 遍历顺序表 ***** " << endl;
cout << " ***** 2、 插入数据元素 ***** " << endl;
cout << " ***** 3、 删除数据元素 ***** " << endl;
cout << " ***** 4、 查看某数据元素的位置 ***** " << endl;
cout << " ***** 5、 查看某位置的数据元素 ***** " << endl;
cout << " ***** 6、 清空顺序表 ***** " << endl;
cout << " ***** 0、 退出 ***** " << endl;
cout << " ******************************************** " << endl;
}
//初始化顺序表
int InitList(SeqList *L) {
L->data = (ElemType*)malloc(Max_Length * sizeof(ElemType)); //分配内存空间
if (!L->data) {
//分配内存错误
printf("内存分配异常,不能初始化顺序表!\n");
return Err_Memory;
}
L->Length = 0; // 顺序表实际长度置为0
L->ListLength = Max_Length; // 当前顺序表存储大小置为Max_Length
return OK;
system("pause");
system("cls");
}
//清空顺序表
int ClearList(SeqList *L) {
L->Length = 0; //顺序表的实际长度置为0,逻辑清空
return OK;
system
C++实现顺序表
最新推荐文章于 2024-04-27 18:58:47 发布