在考研复习过程中,我发现严书上给出的代码非常不易于理解,并且搜索引擎搜到的一些博客所写的代码质量实在是一言难尽。所以在此更新一些根据我自己的理解写的代码,希望可以帮助到初学者以及正在复习数据结构的人。同时也希望大家可以重视代码的规范性,提高代码的可阅读性。一定要做到见名知意。不要写出类似int a,b,c,d,e,f,g,以及不缩进不使用驼峰命名法的垃圾代码。
#include <iostream>
using namespace std;
#define MAXSIZE 100
bool insertElem(int *SqList, int &length, int position, int elem);
bool deleteElem(int *SqList, int &length, int position, int &elem);
bool createList(int *SqList, int &length);
bool merge(const int *SqListA, int lengthA, const int *SqListB, int lengthB, int *SqListC);
bool reverseSimple(int *SqListA, int length);
bool reverseComplex(int *SqList, int left, int right, int lengthToReverse);
bool locateByElem(const int *SqList, int number, int &location);
bool locateByPosition(const int *SqList, int length, int location, int &number);
bool moveByNumberP(int *SqList, int p, int listLength);
void partitionA(int *SqList, int length);
void partitionB(int *SqList, int length, int compare);
void partitionC(int *SqList, int length, int randomNumber);
void showElem(int *SqList, int length);
int main() {
int SqListA[MAXSIZE];
int length;
createList(SqListA, length)
return 0;
}
bool createList(int *SqList, int &length) {
cout << "please input the SqList length: ";
cin >> length;
if (length > MAXSIZE)
return false;
cout << "please input the elem in line: " << endl;
for (int i = 0; i < length; ++i) {
cin >> SqList[i];
}
return true;
}
bool insertElem(int *SqList, int &length, int position, int elem) {
if (position < 0 || position > length || length == MAXSIZE)
return false;
for (int i = length - 1; i >= position; i--) {
SqList[i + 1] = SqList[i];
}
SqList[position] = elem;
length++;
return true;
}
bool deleteElem(int *SqList, int &length, int position, int &elem) {
if (position < 0 || position > length - 1)
return false;
elem = SqList[position];
for (int i = position - 1; i < length - 1; ++i) {
SqList[i] = SqList[i + 1];
}
length--;
return true;
}
void showElem(int *SqList, int length) {
for (int j = 0; j < length; ++j) {
cout << SqList[j] << "\t";
}
cout << endl << "Length:" << length;
}
bool merge(const int *SqListA, int lengthA, const int *SqListB, int lengthB, int *SqListC) {
if (lengthB + lengthA > 2 * MAXSIZE)
return false;
int pointerA = 0;
int pointerB = 0;
int pointerC = 0;
while (pointerA < lengthA && pointerB < lengthB) {
if (SqListA[pointerA] < SqListB[pointerB]) {
SqListC[pointerC++] = SqListA[pointerA++];
} else {
SqListC[pointerC++] = SqListB[pointerB++];
}
}
while (pointerA < lengthA) {
SqListC[pointerC++] = SqListA[pointerA++];
}
while (pointerB < lengthB) {
SqListC[pointerC++] = SqListB[pointerB++];
}
return true;
}
bool reverseSimple(int *SqListA, int length) {
if (length < 0)
return false;
int temp;
for (int head = 0, back = length - 1; head < back; ++head, --back) {
temp = SqListA[head];
SqListA[head] = SqListA[back];
SqListA[back] = temp;
}
return true;
}
bool reverseComplex(int *SqList, int left, int right, int lengthToReverse) {
if (left < 0 || right < 0 || lengthToReverse < 0 ||
lengthToReverse > MAXSIZE || right > MAXSIZE || left > MAXSIZE)
return false;
int temp;
for (int head = left, back = right;
head < left + lengthToReverse && left < right; ++head, --back) {
temp = SqList[head];
SqList[head] = SqList[back];
SqList[back] = temp;
}
return true;
}
bool moveByNumberP(int *SqList, int p, int listLength) {
if (SqList == nullptr || p < 0 || listLength < 0)
return false;
reverseComplex(SqList, 0, p - 1, p);
reverseComplex(SqList, p, listLength - 1, listLength - p);
reverseComplex(SqList, 0, listLength - 1, listLength);
return true;
}
bool locateByElem(const int *SqList, int number, int &location) {
if (number < 0 || SqList == nullptr)
return false;
for (int i = 0; i < number; ++i) {
if (SqList[i] == number) {
location = i;
return true;
}
}
return false;
}
bool locateByPosition(const int *SqList, int length, int location, int &number) {
if (location < 0 || location > length - 1 || SqList == nullptr)
return false;
for (int i = 0; i < location; ++i) {
if (i == location) {
number = SqList[i];
}
}
return true;
}
void partitionA(int *SqList, int length) {
int head = 0;
int back = length - 1;
int temp = SqList[0];
while (head < back) {
while (head < back && SqList[back] >= temp)
--back;
if (head < back) {
SqList[head] = SqList[back];
head++;
}
while (head < back && SqList[head] < temp)
++head;
if (head < back) {
SqList[back] = SqList[head];
--back;
}
}
SqList[head] = temp;
}
void partitionB(int *SqList, int length, int compare) {
int head = 0;
int back = length - 1;
int temp = SqList[0];
while (head < back) {
while (head < back && SqList[back] >= compare)
--back;
if (head < back) {
SqList[head] = SqList[back];
head++;
}
while (head < back && SqList[head] < compare)
++head;
if (head < back) {
SqList[back] = SqList[head];
--back;
}
}
SqList[head] = temp;
}
void partitionC(int *SqList, int length, int randomNumber) {
int head = 0;
int back = length - 1;
int temp = SqList[0];
SqList[0] = SqList[randomNumber];
SqList[randomNumber] = temp;
temp = SqList[head];
while (head < back) {
while (head < back && SqList[back] >= temp)
--back;
if (head < back) {
SqList[head] = SqList[back];
head++;
}
while (head < back && SqList[head] < temp)
++head;
if (head < back) {
SqList[back] = SqList[head];
--back;
}
}
SqList[head] = temp;
}