实现了几个常见的算法,作了几道例题,同时还把快速排序的代码实现了
LineList.h文件
#pragma once
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
class LineList
{
private:
ElemType* data;
int len;
int size;
public:
LineList();
~LineList();
bool getBiggerSpace();
int getLength();
bool getAllData();
bool ListIsEmpty();
bool DestroyList();
ElemType getData(int palce);
int searchData(int num);
bool addData(int palce, ElemType value);
ElemType deleteData(int palce);
int addData(ElemType, bool _switch = false);
bool deleteSomeoneData(int value, bool _switch = false);
bool partitionl(bool _switch = false);
int quick_sort_1(int left = 0, int right = -1);
bool quick_sort(int left = 0, int right = -1);
bool fun_1(int left, int right);
int fun_2(int value);
};
LineList.cpp文件
#include "LineList.h"
LineList::LineList() :len(0),size(MAXSIZE)
{
this->data = new ElemType[MAXSIZE];
}
LineList::~LineList()
{
delete[] this->data;
this->data = NULL;
}
bool LineList::getBiggerSpace()
{
ElemType* _data = new ElemType[size + MAXSIZE];
for (int i = 0; i < this->len; i++)
{
_data[i] = this->data[i];
}
delete[] this->data;
this->data = _data;
return true;
}
int LineList::getLength()
{
return this->len;
}
bool LineList::getAllData()
{
if (this->ListIsEmpty())
{
cout << "该线性表为空" << endl;
return false;
}
for (int i = 0; i < len; i++)
{
cout << this->data[i] << " " << endl;
}
return true;
}
bool LineList::ListIsEmpty()
{
if (this->len == 0)
return true;
return false;
}
bool LineList::DestroyList()
{
delete[] this->data;
this->data = NULL;
this->len = 0;
return true;
}
ElemType LineList::getData(int palce)
{
if (palce > len + 1)
{
return (ElemType)-1;
}
return this->data[palce - 1];
}
int LineList::searchData(int num)
{
for (int i = 0; i < this->len; i++)
{
if (num == this->data[i])
return i;
}
return -1;
}
bool LineList::addData(int palce, ElemType value)
{
if (this->len >= this->size)
this->getBiggerSpace();
if (palce < 1 || palce > this->size)
return false;
for (int i = len-1; i >= palce - 1; i--)
{
data[i + 1] = data[i];
}
data[palce - 1] = value;
this->len++;
return true;
}
ElemType LineList::deleteData(int palce)
{
if (this->ListIsEmpty())
return (ElemType)-1;
if (palce < 1 || palce > this->size)
return (ElemType)-1;
ElemType value = this->data[palce - 1];
for (int i = palce - 1; i < this->len - 1; i++)
{
this->data[i] = this->data[i + 1];
}
this->len--;
return value;
}
int LineList::addData(ElemType value, bool _switch)
{
if (this->len >= this->size)
this->getBiggerSpace();
if (_switch == false)
{
this->data[len] = value;
len++;
return len - 1;
}
else
{
this->addData(1,value);
}
}
bool LineList::deleteSomeoneData(int value, bool _switch)
{
ElemType* _data = this->data;
int num = 0;
if (_switch == false)
{
for (int i = 0; i < this->len; i++)
{
if (this->data[i] != value)
{
_data[num] = this->data[i];
num++;
}
}
this->len = num;
}
else
{
for (int i = 0; i < this->len; i++)
{
if (this->data[i] == value)
num++;
this->data[i] = this->data[i + num];
}
this->len -= num;
}
return true;
}
bool LineList::partitionl(bool _switch)
{
if (this->ListIsEmpty())
return false;
int left = 0;
int right = this->len - 1;
if (_switch == false)
{
ElemType _data = this->data[0];
while (left < right)
{
while (left < right && this->data[right] > _data)
right--;
while (left < right && this->data[left] <= _data)
left++;
if (left < right)
swap(this->data[left], this->data[right]);
}
swap(this->data[left], this->data[0]);
return true;
}
else
{
ElemType _data = this->data[left];
while (left < right)
{
while (left < right && this->data[right] > this->data[0])
right--;
this->data[left] = this->data[right];
while (left < right && this->data[left] <= this->data[0])
left++;
this->data[right] = this->data[left];
}
this->data[left] = _data;
return true;
}
}
bool LineList::fun_1(int left, int right)
{
if (this->ListIsEmpty() || left <= 0 || right > this->len)
return false;
int num = 0;
ElemType* _data = this->data;
for (int i = 0; i < this->len; i++)
{
if (this->data[i] <left || this->data[i] > right)
{
_data[num] = this->data[i];
num++;
}
}
this->len = num;
return true;
}
int LineList::quick_sort_1(int left, int right)
{
if (this->ListIsEmpty())
return false;
ElemType s = this->data[left];
while (left < right)
{
while (left < right && this->data[right] > s)
right--;
this->data[left] = this->data[right];
while (left < right && this->data[left] <= s)
left++;
this->data[right] = this->data[left];
}
this->data[left] = s;
return left;
}
bool LineList::quick_sort(int left, int right)
{
int i;
if (right == -1)
right = this->len - 1;
if (left < right)
{
i = this->quick_sort_1(left,right);
this->quick_sort(left,i-1);
this->quick_sort(i+1,right);
}
return true;
}
int LineList::fun_2(int value)
{
if (this->ListIsEmpty())
return -1;
int left = 0, right = this->len - 1;
int num = 1;
while (left < right)
{
num = (left + right) / 2;
if (this->data[num] == value)
{
this->addData(num + 1, value);
return num + 1;
}
else if (this->data[num] > value) right = num - 1;
else if (this->data[num] < value) left = num + 1;
}
if (num == 0)
{
this->addData(0, value);
return 0;
}
else if (this->data[num] > value)
{
this->addData(num+1, value);
return num;
}
else if (this->data[num] < value)
{
this->addData(num + 3, value);
return num + 2;
}
}
text.h文件
#pragma once
bool partitonl_text();
bool list_text();
bool quickSort_test();
bool fun_1_test();
bool fun_2_test();
text.cpp文件
#include<iostream>
using namespace std;
#include"LineList.h"
#include"test.h"
int main()
{
return 0;
}
bool list_text()
{
LineList list;
for (int i = 0; i < 10; i++)
{
list.addData(i);
}
list.deleteData(2);
list.getAllData();
list.addData(3);
cout << "线性表长度" << list.getLength() << endl;
list.deleteSomeoneData(3, true);
cout << "线性表长度" << list.getLength() << endl;
list.getAllData();
return true;
}
bool partitonl_text()
{
LineList list;
for (int i = 0; i < 10; i++)
{
list.addData(i);
}
list.addData(12);
list.addData(1, 11);
list.addData(14);
list.addData(13);
list.addData(11);
list.getAllData();
list.partitionl(true);
list.getAllData();
cout << endl;
list.partitionl(false);
list.getAllData();
return true;
}
bool quickSort_test()
{
LineList list;
for (int i = 0; i < 3; i++)
{
list.addData(i);
}
list.addData(12);
list.addData(1, 11);
list.addData(14);
list.addData(13);
list.getAllData();
cout << endl;
list.quick_sort();
list.getAllData();
return true;
}
bool fun_1_test()
{
LineList list;
for (int i = 0; i < 10; i++)
{
list.addData(i);
}
list.addData(12);
list.addData(1, 11);
list.addData(14);
list.addData(13);
list.addData(11);
list.fun_1(2,8);
list.getAllData();
return true;
}
bool fun_2_test()
{
LineList list;
for (int i = 0; i < 10; i++)
{
list.addData(i);
}
list.addData(12);
list.addData(1, 11);
list.addData(14);
list.addData(18);
list.addData(11);
list.quick_sort();
list.fun_2(19);
cout << endl;
list.getAllData();
return true;
}