很早以前学习数据结构时编写的(不过当年好像忘记写修改数据了)
基于数组实现数据的存储。
主要实现数组的添加数字,插入数据,删除数据,展示数据,数据排序,查找数据。
插入数据:
Status GetElem(SqList &L, ElemType e) {//往顺序表中增加数据
L.elem[L.length] = e;//elem[L.length]单元储存e
cout << "数据添加成功" << endl;
L.length++;//增加表长
return OK;
}
删除数据:
Status ListDelete(SqList &L, ElemType e) {//从顺序表中删除数据
for (int i = 0; i <= L.length - 1; i++) {
if (L.elem[i] == e) {
for (int j = i; j < L.length - 1; j++) {
L.elem[j] = L.elem[j + 1];
}
cout << "删除成功" << endl;
L.length--;
return OK;
}
}
cout << "没有找到您要删除的数" << endl;
return ERROR;
}
展示数据:
void ListShow(SqList &L) {//展示数据
for (int i = 0; i <= L.length - 1; i++) {
cout << L.elem[i] << " ";
}
cout << endl;
}
排序算法主要实现了冒泡排序,堆排序,直接插入排序,直接选择排序,快速排序:
void BubbleSort(SqList &L) {//对数据进行排序
cout << "您要排序的数为:" << endl;
ListShow(L);
int flag = 1;
for (int i = 0; (i < L.length - 1) && (flag == 1); i++) {
flag = 0;
for (int j = 0; j < L.length - 1 - i; j++) {
if (L.elem[j] > L.elem[j + 1]) {
int temp = L.elem[j + 1];
L.elem[j + 1] = L.elem[j];
L.elem[j] = temp;
flag = 1;
}
}
cout << "第" << i + 1 << "次排序结果为: ";
ListShow(L);
}
cout << "冒泡排序的结果为:" << endl;
ListShow(L);
}
void HeadAdjust(SqList &L, int len) {
for (int i = len / 2 - 1; i >= 0; i--) {
if ((2 * i + 1) < len && (L.elem[i] < L.elem[2 * i + 1])) {
int temp = L.elem[2 * i + 1];
L.elem[2 * i + 1] = L.elem[i];
L.elem[i] = temp;
}
if ((2 * i + 2) < len && (L.elem[i] < L.elem[2 * i + 2])) {
int temp = L.elem[2 * i + 2];
L.elem[2 * i + 2] = L.elem[i];
L.elem[i] = temp;
}
}
}
void Exchange(SqList &L, int len) {
int temp = L.elem[len - 1];
L.elem[len - 1] = L.elem[0];
L.elem[0] = temp;
}
void InsertSort(SqList &L) {
for (int i = 1; i < L.length; i++) {
int temp = L.elem[i];
int j;
for (j = i - 1; j >= 0 && temp < L.elem[j]; j--) {
L.elem[j + 1] = L.elem[j]; //将较大元素后移
}
L.elem[j + 1] = temp;
cout << "第" << i << "次插入的结果为:";
ListShow(L);
}
}
void SelectSort(SqList &L) {//进行直接选择排序
int k = 0;
for (int i = 0; i < L.length - 1; i++) {
int temp = L.elem[i];
for (int j = i; j < L.length - 1; j++) {
if (L.elem[j + 1] < temp) {
temp = L.elem[j + 1];
k = j + 1;
if (j == L.length - 2) {
L.elem[k] = L.elem[i];
L.elem[i] = temp;
}
}
}
cout << "第" << i + 1 << "次直接选择排序的结果是:";
ListShow(L);
}
}
int partition(SqList &L, int low, int high) {
int temp = L.elem[low];
int pivotkey = L.elem[low];
while (low < high) {
while ((low < high) && (L.elem[high] >= pivotkey)) {
high--;
}
L.elem[low] = L.elem[high];
while ((low < high) && (L.elem[low] <= pivotkey)) {
low++;
}
L.elem[high] = L.elem[low];
}
L.elem[low] = temp;
cout << "排序结果是:";
ListShow(L);
return low;
}
void QuickSort(SqList &L, int low, int high) {
if (low < high) {
int pivotloc = partition(L, low, high);
QuickSort(L, low, pivotloc - 1);//对左子表递归排序
QuickSort(L, pivotloc + 1, high);//对右子表s递归排序
}
}
查找数据:
Status LocateElem(SqList L, ElemType e) {//查找数据的位置
for (int i = 0; i < L.length; i++) {
if (L.elem[i] == e)//将顺序表中数据与e进行比较
{
cout << "查找成功" << endl;
cout << "数据位置为第" << i + 1 << "位" << endl;//找到后输出数据的位置
return OK;
}
}
cout << "没有找到" << endl;
return ERROR;
}
以下是完整代码:
#include <iostream>
#include<string>
using namespace std;
#define MAXSIZE 100//顺序表的最大长度
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct SqList {//顺序表的存储结构
ElemType *elem;//储存空间的基地址
int length;//数组的长度
} SqList;
void menu() {//构建用户菜单
cout << "请选择您的操作:" << endl;
cout << "1.添加数据" << endl;
cout << "2.插入数据" << endl;
cout << "3.删除数据" << endl;
cout << "4.展示数据" << endl;
cout << "5.数据排序" << endl;
cout << "6.查找数据" << endl;
cout << "7.退出" << endl;
}
//初始化一个新的顺序表
Status InitLIst(SqList &L) {
L.elem = new ElemType[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
if (!L.elem) {//存储分配失败退出
cout << "初始化失败" << endl;
exit(0);
}
L.length = 0;//将空表长度初始化为0
cout << "顺序表构建成功" << endl;
return OK;
}
Status GetElem(SqList &L, ElemType e) {
L.elem[L.length] = e;//elem[L.length]单元储存e
cout << "数据添加成功" << endl;
L.length++;//增加表长
return OK;
}
Status LocateElem(SqList L, ElemType e) {//查找数据的位置
for (int i = 0; i < L.length; i++) {
if (L.elem[i] == e)//将顺序表中数据与e进行比较
{
cout << "查找成功" << endl;
cout << "数据位置为第" << i + 1 << "位" << endl;//找到后输出数据的位置
return OK;
}
}
cout << "没有找到" << endl;
return ERROR;
}
Status ListInsert(SqList &L, int i, ElemType e) {//往顺序表中插入数据
if ((i < 1) || (i > L.length + 1)) {//判断插入的位置i是否合法
cout << "插入失败" << endl;
return ERROR;
}
if (L.length == MAXSIZE) {//判断当前空间是否可以插入
cout << "当前储存空间已满" << endl;
return ERROR;
}
for (int j = L.length - 1; j >= i - 1; j--) {
L.elem[j + 1] = L.elem[j];//从最后一位开始,插入位置结束,将数据依次往后移
}
L.elem[i - 1] = e;//将要加入的数据插入顺序表
cout << "插入成功" << endl;
L.length++;//增加表长
return OK;
}
Status ListDelete(SqList &L, ElemType e) {//从顺序表中删除数据
for (int i = 0; i <= L.length - 1; i++) {
if (L.elem[i] == e) {
for (int j = i; j < L.length - 1; j++) {
L.elem[j] = L.elem[j + 1];
}
cout << "删除成功" << endl;
L.length--;
return OK;
}
}
cout << "没有找到您要删除的数" << endl;
return ERROR;
}
void ListShow(SqList &L) {
for (int i = 0; i <= L.length - 1; i++) {
cout << L.elem[i] << " ";
}
cout << endl;
}
void BubbleSort(SqList &L) {//对数据进行排序
cout << "您要排序的数为:" << endl;
ListShow(L);
int flag = 1;
for (int i = 0; (i < L.length - 1) && (flag == 1); i++) {
flag = 0;
for (int j = 0; j < L.length - 1 - i; j++) {
if (L.elem[j] > L.elem[j + 1]) {
int temp = L.elem[j + 1];
L.elem[j + 1] = L.elem[j];
L.elem[j] = temp;
flag = 1;
}
}
cout << "第" << i + 1 << "次排序结果为: ";
ListShow(L);
}
cout << "冒泡排序的结果为:" << endl;
ListShow(L);
}
void HeadAdjust(SqList &L, int len) {
for (int i = len / 2 - 1; i >= 0; i--) {
if ((2 * i + 1) < len && (L.elem[i] < L.elem[2 * i + 1])) {
int temp = L.elem[2 * i + 1];
L.elem[2 * i + 1] = L.elem[i];
L.elem[i] = temp;
}
if ((2 * i + 2) < len && (L.elem[i] < L.elem[2 * i + 2])) {
int temp = L.elem[2 * i + 2];
L.elem[2 * i + 2] = L.elem[i];
L.elem[i] = temp;
}
}
}
void Exchange(SqList &L, int len) {
int temp = L.elem[len - 1];
L.elem[len - 1] = L.elem[0];
L.elem[0] = temp;
}
void InsertSort(SqList &L) {
for (int i = 1; i < L.length; i++) {
int temp = L.elem[i];
int j;
for (j = i - 1; j >= 0 && temp < L.elem[j]; j--) {
L.elem[j + 1] = L.elem[j]; //将较大元素后移
}
L.elem[j + 1] = temp;
cout << "第" << i << "次插入的结果为:";
ListShow(L);
}
}
void SelectSort(SqList &L) {//进行直接选择排序
int k = 0;
for (int i = 0; i < L.length - 1; i++) {
int temp = L.elem[i];
for (int j = i; j < L.length - 1; j++) {
if (L.elem[j + 1] < temp) {
temp = L.elem[j + 1];
k = j + 1;
if (j == L.length - 2) {
L.elem[k] = L.elem[i];
L.elem[i] = temp;
}
}
}
cout << "第" << i + 1 << "次直接选择排序的结果是:";
ListShow(L);
}
}
int partition(SqList &L, int low, int high) {
int temp = L.elem[low];
int pivotkey = L.elem[low];
while (low < high) {
while ((low < high) && (L.elem[high] >= pivotkey)) {
high--;
}
L.elem[low] = L.elem[high];
while ((low < high) && (L.elem[low] <= pivotkey)) {
low++;
}
L.elem[high] = L.elem[low];
}
L.elem[low] = temp;
cout << "排序结果是:";
ListShow(L);
return low;
}
void QuickSort(SqList &L, int low, int high) {
if (low < high) {
int pivotloc = partition(L, low, high);
QuickSort(L, low, pivotloc - 1);//对左子表递归排序
QuickSort(L, pivotloc + 1, high);//对右子表s递归排序
}
}
int main() {
cout << "欢迎使用顺序表" << endl;
menu();
SqList l;
InitLIst(l);
int c;
int choose = 0;
int e = 0;
int d = 0;
do {
cin >> choose;
switch (choose) {
case 1:
cout << "请输入您要添加的数:" << endl;
cin >> e;
GetElem(l, e);
cout << "请继续选择" << endl;
break;
case 2:
ListShow(l);
cout << "请输入您要插入的数的位置:" << endl;
cin >> d;
cout << "请输入您要插入的数:" << endl;
cin >> e;
ListInsert(l, d, e);
cout << "请继续选择" << endl;
break;
case 3:
cout << "请输入您要删除的数" << endl;
cin >> e;
ListDelete(l, e);
cout << "请继续选择" << endl;
break;
case 4:
cout << "当前顺序为:" << endl;
ListShow(l);
cout << "请继续选择" << endl;
break;
case 5:
cout << "请选择排序方式:" << endl;
cout << "1.冒泡排序" << endl;
cout << "2.堆排序" << endl;
cout << "3.直接插入排序" << endl;
cout << "4.直接选择排序" << endl;
cout << "5.快速排序" << endl;
cout << "6.返回主菜单" << endl;
cout << "7.退出" << endl;
int b;
cin >> b;
switch (b) {
case 1:
BubbleSort(l);
cout << "请继续选择" << endl;
break;
case 2:
c = 0;
cout << "您想要进行堆排序的数为:";
ListShow(l);
for (int i = l.length; i > 1; i--) {
HeadAdjust(l, i);
Exchange(l, i);
c++;
cout << "第" << c << "次结果为:";
ListShow(l);
}
cout << "堆排序的结果为:" << " ";
cout << endl;
ListShow(l);
cout << "请继续选择" << endl;
break;
case 3:
cout << "您要进行直接插入排序的数为" << endl;
ListShow(l);
InsertSort(l);
cout << "直接插入排序的结果为:" << " ";
cout << endl;
ListShow(l);
cout << "请继续选择" << endl;
break;
case 4:
cout << "您要进行直接选择排序的数为" << endl;
ListShow(l);
SelectSort(l);
cout << "直接选择排序的结果为:" << " ";
cout << endl;
ListShow(l);
cout << "请继续选择" << endl;
break;
case 5:
cout << "您要进行快速排序的数为" << endl;
ListShow(l);
QuickSort(l, 0, l.length - 1);
cout << "快速排序的结果为:" << " ";
cout << endl;
ListShow(l);
cout << "请继续选择" << endl;
break;
case 6:
menu();
break;
case 7:
return 0;
}
break;
case 6:
cout << "请输入您要查找的数:" << endl;
cin >> e;
LocateElem(l, e);
cout << "请继续选择" << endl;
break;
case 7:
cout << "bye~" << endl;
return 0;
break;
default:
cout << "您的选择有误" << endl;
cout << "请重新选择" << endl;
break;
}
} while (choose);
return 0;
}