Python List列表

本文详细介绍了一种使用C++实现的动态数组列表类,包括基本操作如append、pop、获取列表长度,以及高级功能如排序、翻转、连接列表和填充随机数。通过具体的代码示例展示了如何创建和操作这种自定义的列表对象。
#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;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值