0
点赞
收藏
分享

微信扫一扫

【线性表】

Gascognya 2022-02-17 阅读 72

有关线性表的代码`
#include<stdio.h>
#include
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef struct {
int* elem;
int length;
int listsize;
} SqList;

Status InitList_Sq(SqList& L) {
int number;
L.elem = (int*)malloc(LIST_INIT_SIZE * sizeof(int));
if (!L.elem)
{
cout << “OVERFLOW” << endl;
return 0;
InitList_Sq(L);
}
else
{
L.length = 0;
L.listsize = LIST_INIT_SIZE;
cout << “请输入线性表表长:”;
scanf_s("%d", &number);
for (int i = 0; i < number; i++) {
cout << “请输入第” << i + 1 << “个元素的值:”;
scanf_s("%d", L.elem + i);
L.length++;
}
return 1;
}
}
void PRINT(SqList L) {
//cout << “线性表当前最大存储范围为” << LIST_INIT_SIZE << endl;
//cout << “线性表当前长度为” << L.length << endl;
cout << “线性表为:”;
for (int i = 0; i < L.length; i++) {
cout << L.elem[i] << " ";
}
cout << endl;
}
int Partition(SqList& L, int low, int high) {
int k = 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] = k;
PRINT(L);
return low;

}
void Qsort(SqList& L, int low, int high) {
if (low < high) {
int pivotloc = Partition(L, low, high);
Qsort(L, low, pivotloc - 1);
Qsort(L, pivotloc + 1, high);
}
}
void QuickSort(SqList& L) {
Qsort(L, 0, L.length-1);
}
int main() {
SqList LA;
InitList_Sq(LA);
QuickSort(LA);
}`

举报

相关推荐

0 条评论