C++ 实现顺序表的基本操作:创建、插入、删除、查找、排序等
C++ 实现顺序表的基本操作
以下是用 C++ 实现顺序表的各种基本操作的代码:
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
struct SeqList {
int data[MAX_SIZE];
int length;
};
// 创建顺序表
void createSeqList(SeqList &L) {
cout << '请输入顺序表的长度:';
cin >> L.length;
cout << '请输入顺序表的元素:';
for (int i = 0; i < L.length; i++) {
cin >> L.data[i];
}
}
// 插入元素
bool insertElement(SeqList &L, int index, int value) {
if (index < 0 || index > L.length || L.length == MAX_SIZE) {
return false;
}
for (int i = L.length; i > index; i--) {
L.data[i] = L.data[i-1];
}
L.data[index] = value;
L.length++;
return true;
}
// 删除元素
bool deleteElement(SeqList &L, int index) {
if (index < 0 || index >= L.length) {
return false;
}
for (int i = index; i < L.length - 1; i++) {
L.data[i] = L.data[i+1];
}
L.length--;
return true;
}
// 读取表元
bool getElement(SeqList L, int index, int &value) {
if (index < 0 || index >= L.length) {
return false;
}
value = L.data[index];
return true;
}
// 获取最大值
bool getMaxElement(SeqList L, int &maxValue) {
if (L.length == 0) {
return false;
}
maxValue = L.data[0];
for (int i = 1; i < L.length; i++) {
if (L.data[i] > maxValue) {
maxValue = L.data[i];
}
}
return true;
}
// 获取最小值
bool getMinElement(SeqList L, int &minValue) {
if (L.length == 0) {
return false;
}
minValue = L.data[0];
for (int i = 1; i < L.length; i++) {
if (L.data[i] < minValue) {
minValue = L.data[i];
}
}
return true;
}
// 查找元素
int findElement(SeqList L, int value) {
for (int i = 0; i < L.length; i++) {
if (L.data[i] == value) {
return i;
}
}
return -1; // 未找到
}
// 表元素排序(升序)
void sortSeqList(SeqList &L) {
for (int i = 0; i < L.length-1; i++) {
for (int j = 0; j < L.length-i-1; j++) {
if (L.data[j] > L.data[j+1]) {
int temp = L.data[j];
L.data[j] = L.data[j+1];
L.data[j+1] = temp;
}
}
}
}
// 表元素逆置
void reverseSeqList(SeqList &L) {
for (int i = 0; i < L.length/2; i++) {
int temp = L.data[i];
L.data[i] = L.data[L.length-i-1];
L.data[L.length-i-1] = temp;
}
}
// 输出顺序表
void printSeqList(SeqList L) {
for (int i = 0; i < L.length; i++) {
cout << L.data[i] << ' ';
}
cout << endl;
}
int main() {
SeqList L;
createSeqList(L);
cout << '顺序表为:';
printSeqList(L);
int index, value;
cout << '请输入要插入的位置和元素:';
cin >> index >> value;
if (insertElement(L, index, value)) {
cout << '插入成功!插入后的顺序表为:';
printSeqList(L);
} else {
cout << '插入失败!' << endl;
}
cout << '请输入要删除的位置:';
cin >> index;
if (deleteElement(L, index)) {
cout << '删除成功!删除后的顺序表为:';
printSeqList(L);
} else {
cout << '删除失败!' << endl;
}
int element;
cout << '请输入要读取的位置:';
cin >> index;
if (getElement(L, index, element)) {
cout << '读取的元素为:' << element << endl;
} else {
cout << '读取失败!' << endl;
}
int maxValue;
if (getMaxElement(L, maxValue)) {
cout << '最大值为:' << maxValue << endl;
} else {
cout << '顺序表为空!' << endl;
}
int minValue;
if (getMinElement(L, minValue)) {
cout << '最小值为:' << minValue << endl;
} else {
cout << '顺序表为空!' << endl;
}
int searchValue;
cout << '请输入要查找的元素:';
cin >> searchValue;
int searchIndex = findElement(L, searchValue);
if (searchIndex != -1) {
cout << '元素' << searchValue << '在顺序表中的位置为:' << searchIndex << endl;
} else {
cout << '元素未找到!' << endl;
}
sortSeqList(L);
cout << '排序后的顺序表为:';
printSeqList(L);
reverseSeqList(L);
cout << '逆置后的顺序表为:';
printSeqList(L);
return 0;
}
这个代码实现了以下操作:
- 创建顺序表
- 插入元素
- 删除元素
- 读取表元
- 获取最大值元素
- 获取最小值元素
- 查找元素
- 表元素排序
- 表元素逆置
- 顺序表的输入和输出
你可以根据需要调用这些函数来操作顺序表。
原文地址: https://www.cveoy.top/t/topic/phgz 著作权归作者所有。请勿转载和采集!