数据结构与算法 cpp代码实现(严蔚敏第二版)
二、线性表
2.线性表 代码实现
一、顺序表基本操作实现
定义顺续表
定义变量L,改变成员变量:L.length
L是指针是(*L)改变成员变量:L->length
1.1顺序表的查找算法分析
平均查找长度ASL(Average SearchLength)
查找次数为((n+1)*n/2)/n=(n+1)/2
1.2顺序表的插入算法分析
1.3顺序表的删除算法分析
二、cpp实现
1.头文件
数据结构.h:
#pragma once
#include<iostream>
using namespace std;
//函数结果状态代码
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100//顺序表可能达到的最大长度
typedef int Status;//status是函数类型,其值是函数结果状态代码
typedef int ElemType;// ElemType是元素的数据类型,想要整体改变元素的数据类型只需改变ElemType前的数据类型即可
typedef struct
{
ElemType *elem;//存储数组初始地址
int length;//顺序表元素个数
} SqList;//定义顺序表类型
## 2.
```cpp
#include"数据结构.h"
void ShowList(SqList &L);
//线性表L的初始化
Status InitList_Sq(SqList& L)//构造一个顺序表
{
L.elem = new ElemType[MAXSIZE];//为顺序表静态分配空间,new一块空间,elem指向他
//或L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem)
exit(OVERFLOW);//存储分匹配失败
L.length = 0;//空表长度为0
cout << "选择输入的元素个数" << endl;
cin >> L.length;
cout << "输入数据元素" << endl;
for (int i = 0; i < L.length; i++)
{
cin >> L.elem[i];
}
return OK;
system("pause");
system("cls");
}
//求线性表L的长度
Status GetLength(SqList& L)
{
return(L.length);
system("pause");
system("cls");
}
//判断线性表是否为空
void isEmpty(SqList L)
{
if (L.length == 0)
{
cout << "当前线性表为空" << endl;
}
else
{
cout << "当前线性表不为空" << endl;
}
system("pause");
system("cls");
}
//顺序表的取值(根据位置i获取相应位置数据元素的内容)
Status GetElem(SqList L)//算法复杂度 O(1)
{
int i; ElemType e;
ShowList(L);
cout << "请输入您要查找的序号: " << endl;
cin >> i;
if (i<1 || i>L.length)
return ERROR;//判断i是否合理
e = L.elem[i - 1];//第i-1的单元存储着第i个数据
cout << "所取元素为: " << e << endl;
system("pause");
system("cls");
return OK;
}
//顺序表的查找
void LocateElem(SqList L)//在线性表从查找与指定值e相同的数据元素的位置
{
ElemType e;
cout << "请输入您要查找的数据元素: ";
cin >> e;
for (int i = 0; i < L.length; i++)
if (L.elem[i] == e)
cout << "所查找元素位置为: " << i + 1 << endl;//返回其序号
/*else
cout << "未查到指定元素" << endl;*/
system("pause");
system("cls");
}
//顺序表的插入
void Listinsert(SqList& L)
{
ShowList(L);
int i = 0;
ElemType e = 0;
cout << "输入插入的位置" << endl;
cin >> i;
cout << "输入插入的元素" << endl;
cin >> e;
if (i<1 || i>L.length+1)
{
cout << "位置选择有误" << endl;
}
if (L.length == MAXSIZE)
{
cout << "位置选择有误" << endl;
}
for (int j = L.length - 1; j >= i - 1; j--)//执行
{
L.elem[j + 1] = L.elem[j];//插入位置及之后的元素后移
}
L.elem[i] = e;//将新元素e放入i位置
++L.length;//表长增加1
cout << "插入后的顺序表长为:" << L.length << endl;
cout << "插入后的顺序表为:";
ShowList(L);
system("pause");
system("cls");
}
//顺序表的删除
void Listdelet(SqList& L)
{
ShowList(L);
int i;
cout << "输入删除元素的位置" << endl;
cin >> i;
if (i<1 || i>L.length )
{
cout << "位置选择有误" << endl;
}
for (int j = i; j <= L.length - 1; j++)//执行
L.elem[j - 1] = L.elem[j];//被删除元素之后的元素后移
L.length--;//表长减1
ShowList(L);
system("pause");
system("cls");
}
//清空线性表L
void ClearList(SqList& L)
{
L.length = 0;//将线性表的长度置位0
cout << "顺序表的长度为: " << L.length << endl;
system("pause");
system("cls");
}
//销毁线性表L
void DestoryListy(SqList& L)
{
if (L.elem)
delete L.elem;//释放存储空间
cout << "顺序表的长度为: " << L.length << endl;
system("pause");
system("cls");
}
//显示线性表所有数据
void ShowList(SqList& L)
{
for (int i = 0; i < L.length; i++)
{
cout << L.elem[i] << " ";
}
cout << endl;
}
void showMenu()
{
cout << "***********************************" << endl;
cout << "***** [1]----初始化顺序表----*****" << endl;
cout << "***** [2]----求顺序表长度----*****" << endl;
cout << "***** [3]----顺序表的判空----*****" << endl;
cout << "***** [4]----对顺序表取值----*****" << endl;
cout << "***** [5]----顺序表的查找----*****" << endl;
cout << "***** [6]----顺序表的插入----*****" << endl;
cout << "***** [7]----顺序表的删除----*****" << endl;
cout << "***** [8]----对顺序表清空----*****" << endl;
cout << "***** [9]----对顺序表销毁----*****" << endl;
cout << "***** [0]-----退出顺序表-----*****" << endl;
cout << "***********************************" << endl;
}
void Listexit()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
int main()
{
SqList L;
ElemType e;
while (true)
{
showMenu();
cout << "输入您的选择" << endl;
int i;
cin >> i;
switch (i)
{
case 0:
Listexit(); break;
case 1:
InitList_Sq(L);
cout << "表L的起始位置为: " << L.elem[0] << endl; break;
case 2:
GetLength(L);
cout << "表L的长度为: " << L.length << endl; break;
case 3:
isEmpty(L);
break;
case 4:
GetElem(L);
break;
case 5:
LocateElem(L);
cout << LocateElem << endl;
break;
case 6:
Listinsert(L);
break;
case 7:
Listdelet(L);
break;
case 8:
ClearList(L);
break;
case 9:
DestoryListy(L);
break;
default:
break;
}
}
system("pause");
return 0;
}