#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class List
{
public:
List(bool temp = false);
List(const List &rhs);
~List();
void append(int value); //append方法
int pop(int index = -1); //pop方法
int getLength(void) const; //获取列表长度
int & operator[](int index) const; //像数组一样访问,并能越界检查
List & reverse(void); //翻转列表
List operator+(const List &rhs); //连接两个列表
List operator*(unsigned int times); //重复一个列表
List & operator=(const List &rhs); //直接拷贝
List & sort(void); //排序,升序
void fillRandom(int start, int end); //填充随机数据
List & operator~(void);
List & operator=(List &&rhs);
private:
int *pointer;
int usableLength;
int maxLength;
bool temp;
friend ostream & operator<<(ostream &os, const List &rhs);
};
List::List(bool temp)
{
srand((unsigned)time(NULL));
pointer = NULL;
this -> temp = temp;
usableLength = maxLength = 0;
}
List::~List()
{
if (pointer != NULL && !temp)
{
free(pointer);
}
}
int List::getLength(void) const
{
return usableLength;
}
void quick_sort(int array[], int left, int right) {
int i = left, j = right;
int temp, pivot;
pivot = array[(left + right) / 2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
if (left < j) {
quick_sort(array, left, j);
}
if (i < right) {
quick_sort(array, i, right);
}
}
List & List::sort(void)
{
quick_sort(pointer, 0, usableLength - 1);
return *this;
}
void List::append(int value)
{
usableLength++;
if (usableLength > maxLength)
{
pointer = (int *)realloc(pointer, sizeof(int) * usableLength);
maxLength = usableLength;
}
pointer[usableLength - 1] = value;
}
void List::fillRandom(int start, int end)
{
for (int i = 0; i < usableLength; i++)
{
pointer[i] = rand() % (end - start + 1) + start;
}
}
int List::pop(int index)
{
int value;
if (index == -1)
{
usableLength--;
value = pointer[usableLength];
pointer = (int *)realloc(pointer, sizeof(int) * usableLength);
}
else
{
value = pointer[index];
for (int i = index; i < usableLength - 1; i++)
{
pointer[i] = pointer[i+1];
}
usableLength--;
}
return value;
}
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
List & List::reverse(void)
{
for (int i = 0; i <= (usableLength - 1) / 2; i++)
{
swap(pointer[i], pointer[usableLength - i - 1]);
}
return *this;
}
List & List::operator~(void)
{
return reverse();
}
int & List::operator[](int index) const
{
if (index == -1)
{
return pointer[usableLength-1];
}
else if (index >= 0 && index < usableLength)
{
return pointer[index];
}
else
{
cerr << "数组越界!" << endl;
throw(1);
}
}
List List::operator+(const List &rhs)
{
List tempList(true);
tempList = *this;
for (int i = 0; i < rhs.getLength(); i++)
{
tempList.append(rhs[i]);
}
return tempList;
}
ostream & operator<<(ostream &os, const List &rhs)
{
for (int i = 0; i < rhs.usableLength; i++)
{
os << "list[" << i << "]=" << rhs.pointer[i] << endl;
}
return os;
}
List List::operator*(unsigned int times)
{
List tempList(true);
for (int i = 0; i < times; i++)
{
for (int j = 0; j < usableLength; j++)
{
tempList.append(pointer[j]);
}
}
return tempList;
}
List & List::operator=(const List &rhs)
{
if (this != &rhs)
{
usableLength = maxLength = rhs.usableLength;
pointer = (int *)realloc(pointer, sizeof(int) * usableLength);
for (int i = 0; i < usableLength; i++)
{
pointer[i] = rhs[i];
}
}
return *this;
}
List & List::operator=(List &&rhs)
{
usableLength = maxLength = rhs.usableLength;
pointer = rhs.pointer;
return *this;
}
List::List(const List &rhs)
{
pointer = NULL;
*this = rhs;
}
int main()
{
List a;
for (int i = 0; i < 8; i++)
{
a.append(i);
}
/*a.pop();
cout << a << "---------------------" << endl;
cout << "a.pop(4)=" << a.pop(4) << endl;
cout << a << "---------------------" << endl;
cout << "a[3]=" << a[3] << endl;
a[3] = 32;
cout << "a[3]=" << a[3] << endl;
cout << "a.getLength()=" << a.getLength() << endl;
cout << "---------------------" << endl;
List b;
b = a;
cout << b;
cout << "---------------------" << endl;
a = a * 3;
cout << a;
cout << "---------------------" << endl;
cout << b;
cout << "---------------------" << endl;
List c = b;
cout << c;*/
/*List b;
for (int i = 11; i < 15; i++)
{
b.append(i);
}
List c = a + b;
cout << c;
cout << "---------------------" << endl;
c.reverse();
cout << c;
cout << "---------------------" << endl;
~c;
cout << c;*/
a.fillRandom(1, 100);
cout << a << "----------" << endl;
a.sort();
cout << a << endl;
return 0;
}