#include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -1 #define MAXSIZE 100 using namespace std; typedef int KeyType; typedef char InfoType; typedef int Status; typedef struct { KeyType key; }ElemType; typedef struct { ElemType* r; int length; }SqList; Status InitSqList(SqList &L) { L.r = new ElemType[MAXSIZE]; if (!L.r) exit(OVERFLOW); L.length = 0; return OK; } Status InsertElem(SqList& L,int i, ElemType e) { if ((i < 1) || (i > L.length+1)) return ERROR; if (L.length == MAXSIZE) return ERROR; for (int j = L.length - 1; j >= i - 1; --j) { L.r[j + 1] = L.r[j]; } L.r[i - 1] = e; ++L.length; return OK; } void PrintSqList(SqList L) { for (int i = 0; i < L.length; ++i) cout << L.r[i].key << " "; cout << endl; } int Partition(SqList& L, int low, int high) { int pivotkey; L.r[0] = L.r[low]; pivotkey = L.r[low].key; while (low < high) { while (low < high && L.r[high].key >= pivotkey) --high; L.r[low] = L.r[high]; while (low < high && L.r[low].key <= pivotkey) ++low; L.r[high] = L.r[low]; } L.r[low] = L.r[0]; return low; } void QSort(SqList& L, int low, int high) { int pivotloc; if (low < high) { pivotloc = Partition(L, low, high); QSort(L, low, pivotloc-1); QSort(L, pivotloc+1, high); } } void QuickSort(SqList& L) { QSort(L, 1, L.length); } int main() { ElemType e; int n; SqList L; InitSqList(L); cout << "输入顺序表元素个数:" << endl; cin >> n; cout << "依次输入元素的值:" << endl; for (int i = 0; i < n; ++i) { cin >> e.key; InsertElem(L, i,e); } cout << "排序前的顺序表" << endl; PrintSqList(L); QuickSort(L); cout << "排序后的顺序表" << endl; PrintSqList(L); return 0; }修改上述代码
时间: 2024-03-17 21:44:53 浏览: 149
#include <iostream>
以下是修改后的代码,主要是将函数的返回值类型由void改为Status,并添加了相关的错误码返回:
```
#include<iostream>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 100
using namespace std;
typedef int KeyType;
typedef char InfoType;
typedef int Status;
typedef struct {
KeyType key;
}ElemType;
typedef struct {
ElemType* r;
int length;
}SqList;
Status InitSqList(SqList& L) {
L.r = new ElemType[MAXSIZE];
if (!L.r) {
return OVERFLOW;
}
L.length = 0;
return OK;
}
Status InsertElem(SqList& L, int i, ElemType e) {
if ((i < 1) || (i > L.length + 1)) {
return ERROR;
}
if (L.length == MAXSIZE) {
return ERROR;
}
for (int j = L.length - 1; j >= i - 1; --j) {
L.r[j + 1] = L.r[j];
}
L.r[i - 1] = e;
++L.length;
return OK;
}
void PrintSqList(SqList L) {
for (int i = 0; i < L.length; ++i) {
cout << L.r[i].key << " ";
}
cout << endl;
}
int Partition(SqList& L, int low, int high) {
int pivotkey;
L.r[0] = L.r[low];
pivotkey = L.r[low].key;
while (low < high) {
while (low < high && L.r[high].key >= pivotkey) --high;
L.r[low] = L.r[high];
while (low < high && L.r[low].key <= pivotkey) ++low;
L.r[high] = L.r[low];
}
L.r[low] = L.r[0];
return low;
}
Status QSort(SqList& L, int low, int high) {
int pivotloc;
if (low < high) {
pivotloc = Partition(L, low, high);
QSort(L, low, pivotloc - 1);
QSort(L, pivotloc + 1, high);
}
return OK;
}
Status QuickSort(SqList& L) {
QSort(L, 1, L.length);
return OK;
}
int main() {
ElemType e;
int n;
SqList L;
if (InitSqList(L) == OVERFLOW) {
cout << "顺序表初始化失败!" << endl;
return 0;
}
cout << "输入顺序表元素个数:" << endl;
cin >> n;
cout << "依次输入元素的值:" << endl;
for (int i = 0; i < n; ++i) {
cin >> e.key;
if (InsertElem(L, i, e) == ERROR) {
cout << "插入元素失败!" << endl;
return 0;
}
}
cout << "排序前的顺序表" << endl;
PrintSqList(L);
if (QuickSort(L) == ERROR) {
cout << "排序失败!" << endl;
return 0;
}
cout << "排序后的顺序表" << endl;
PrintSqList(L);
return 0;
}
```
阅读全文