本文主要实现了线性表中顺序表的初始化、创建、增加、删除、修改、清空,判断表长、判断表长等基本操作。
主要依据严蔚敏版数据结构教材以及王道数据结构考研辅导书。
基本函数如下:
int InitList(SqList &L);//初始化
bool CreateList(SqList &L, int n);
int Length(SqList L);//返回长度信息
bool ListInsert(SqList &L, int i, ElemType e);//前插
bool ListDelete(SqList &L, int i, ElemType &e);//删除
int LocateElem(SqList L, ElemType e); //查找值为e的第一个元素的位置
ElemType GetElem(SqList L, int i);//获取第i个位置的值
void PrintList(SqList L);//输出操作
bool Empty(SqList L);//判空
void DestroyList(SqList &L);//销毁,表结构不存在
void Clear(SqList &L);//清空操作,表结构依然存在
功能菜单如下:
以下是详细代码(VS2015编译通过):
#include <iostream>
#include <cstring>//memset的头文件头文件
#include <stdlib.h>//exit的头文件
#define MaxSize 50
#define ElemType int
//注意:我们平时所说的第几个,如第i个元素,对应数组下标的i-1
using namespace std;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
/*-----------------------------基本函数-------------------*/
int InitList(SqList &L);//初始化
bool CreateList(SqList &L, int n);
int Length(SqList L);//返回长度信息
bool ListInsert(SqList &L, int i, ElemType e);//前插
bool ListDelete(SqList &L, int i, ElemType &e);//删除
int LocateElem(SqList L, ElemType e); //查找值为e的第一个元素的位置
ElemType GetElem(SqList L, int i);//获取第i个位置的值
void PrintList(SqList L);//输出操作
bool Empty(SqList L);//判空
void DestroyList(SqList &L);//销毁,表结构不存在
void Clear(SqList &L);//清空操作,表结构依然存在
/*-----------功能性函数--------------------*/
bool Create(SqList &L);//创建表
void Insert(SqList &L);
void Delete(SqList &L);
void Search(SqList L);
void showLength(SqList L);
void isEmpty(SqList L);
void menu();
void menu() {
cout << "********1.创建 2.插入*********" << endl;
cout << "********3.删除 4.查找*********" << endl;
cout << "********5.输出 6.求表长*********" << endl;
cout << "********7.判表空 8.清空***********" << endl;
cout << "********9.退出********************" << endl;
}
int main() {
SqList L;
int choice;
InitList(L);
while (1)
{
menu();
cout << "请输入菜单序号:" << endl;
cin >> choice;
if (choice == 9) break;
switch (choice)
{
case 1:Create(L);break;
case 2:Insert(L);break;
case 3:Delete(L);break;
case 4:Search(L);break;
case 5:PrintList(L);break;
case 6:showLength(L);break;
case 7:isEmpty(L);break;
case 8:Clear(L);break;
default:cout << "输入错误!!!" << endl;
}
}
return 0;
}
/*-----------------------------基本函数-------------------*/
int InitList(SqList &L) {//初始化
//L.data=(int *)malloc(sizeof(int) * MaxSize);
/*void *memset(void *s, int ch, size_t n);
将s中当前位置后面的n个字节用 ch 替换并返回 s 。*/
memset(L.data, 0, sizeof(L));
if (!L.data)
exit(0);
L.length = 0;
return 0;
}
bool CreateList(SqList &L, int n) {//创建表
for (int i = 0;i<n;i++) {
cin >> L.data[i];
L.length++;
}
return true;
}
int Length(SqList L) {//求表长
return L.length;
}
int LocateElem(SqList L, ElemType e) {
for (int i = 0;i<L.length;i++)
{
if (L.data[i] == e)
{
return i + 1;
}
}
return 0;
}
ElemType GetElem(SqList L, int i) {
return L.data[i - 1];
}
bool ListInsert(SqList &L, int i, ElemType e) {
if (i<1 || i>L.length + 1)//如果位置是第一个之前,或者表长长度+1之后(可在最后一位后插入),则位置不合法
return false;
if (L.length >= MaxSize)
return false;
for (int j = L.length;j >= i - 1;j--)//整体往后挪
{
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e;//目标插入点被赋值
L.length++;
return true;
}
bool ListDelete(SqList &L, int i, ElemType &e) {
if (i<1 || i>L.length + 1)
return false;
if (L.length >= MaxSize)
return false;
e = L.data[i - 1];
for (int j = i;i<L.length;j++) {//前移
L.data[j - 1] = L.data[j];
L.length--;
}
cout << "删除成功!!!" << endl;
return true;
}
void PrintList(SqList L) {
cout << "表中元素分别为";
for (int i = 0;i<L.length;i++)
{
cout << L.data[i] << " ";
}
cout << endl;
}
bool Empty(SqList L) {
if (L.length == 0)
return true;
return false;
}
void DestroyList(SqList &L) {
//考的实际上是数组的空间释放问题
}
void Clear(SqList &L) {
L.length = 0;
cout << "表已清空" << endl;
}
/****************功能性函数*****************/
bool Create(SqList &L) {
int n;
cout << "请输入要创建的表的长度:" << endl;
cin >> n;
if (n<0 && n>MaxSize)
return false;
cout << "请输入这" << n << "个数(用空格隔开)" << endl;
if (CreateList(L, n))//判断即调用过
cout << "创建成功!" << endl;
return true;
}
void showLength(SqList L) {
cout << "表长为" << Length(L) << endl;
}
void Insert(SqList &L) {
int n;
ElemType e;
cout << "请输入想要插入的位置:" << endl;
cin >> n;
cout << "请输入第" << n << "个位置上的元素的值:" << endl;
cin >> e;
ListInsert(L, n, e);
}
void Delete(SqList &L) {
int n;
ElemType e;
cout << "请输入想删除的元素的位置:" << endl;
cin >> n;
ListDelete(L, n, e);
}
void Search(SqList L) {
int choice, i;
ElemType e;
cout << "1.按值查找;2.按序号查找;3.返回上一步" << endl;
cout << "请输出你选择操作的序号:" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入要查找的元素的值:";
cin >> e;
cout << "值为" << e << "的元素在第" << LocateElem(L, e) << "个位置" << endl;
break;
case 2:
cout << "请输入要查找的元素的序号:";
cin >> i;
cout << "第" << i << "个位置上的元素是" << GetElem(L, i) << endl;
break;
case 3:
break;
default:
cout << "输入有误!" << endl;
break;
}
}
void isEmpty(SqList L) {
if (Empty(L))
cout << "表为空" << endl;
else
cout << "表非空" << endl;
}
有不足请批评指正