数据结构学习记录之列表篇(使用静态数组和动态数组模拟实现列表)

c++中模拟python列表(只存储单一数据类型)

一、使用静态数组实现 :

SList.h

/* 静态数组实现列表 */

#include <iostream>

#ifndef __SList__H
#define __SList__H

const int CAPACITY = 1024; //列表容量
typedef int ElementType;

class SList
{
private:
    int size; //列表大小
    ElementType myArray[CAPACITY]; //使用静态数组实现列表
    
public:
    SList();
    ~SList();

    bool empty() const; //判断是否为空列表
    int length() const; //获得列表大小
    void insert(int index, ElementType value); //在index处插入一个值
    void display(std::ostream& out) const; //输出列表
    void append(ElementType value); //尾部添加
    void remove(ElementType value); //移除值为value的第一个元素
    void pop(int index = -1); //移除指定位置元素,默认尾部移除
    void clear(); //清空列表
    void sort(bool reverse = false); //列表排序
    void reverse(); //列表翻转
    ElementType& operator[](int index);
    
};

std::ostream& operator<<(std::ostream& out, SList& s);


#endif

SList.cpp

#include "SList.h"

using namespace std;

ostream& operator<<(ostream& out, SList& s)
{
    s.display(out);
    return out;
}

SList::SList(): size(0)
{

}

SList::~SList()
{

}

bool SList::empty() const
{
    return this->size == 0;
}

int SList::length() const
{
    return this->size;
}

void SList::insert(int index, ElementType value)
{
    if (this->size == CAPACITY)
    {
        cerr << "The list is full" << endl;
        return;
    }
    //判断索引是否有效
    if (index < 0)
    {
        cerr << "Invalid index" << endl;
        return;
    } else if (index < this->size) //中间插入
    {
        for (int i = this->size - 1; i >= index; i--)
        {
            this->myArray[i+1] = this->myArray[i];
        }
        this->myArray[index] = value;
        ++this->size;
    } else {
        this->myArray[this->size] = value;
        ++this->size;
    }
}

void SList::display(ostream& out) const
{
    out << "[";
    for (int i = 0; i < this->size; i++)
    {
        out << this->myArray[i] << ",";
    }
    out << "]" << endl;
}

void SList::append(ElementType value)
{
    if (this->size == CAPACITY)
    {
        cerr << "The list is full" << endl;
        return;
    }
    this->myArray[this->size] = value;
    ++this->size;
    return;
}

void SList::remove(ElementType value)
{
    bool in = false;
    for (int i = 0; i < this->size; i++)
    {
        if (this->myArray[i] == value)
        {
            for (int j = i; j < this->size; j++)
            {
                this->myArray[j] = this->myArray[j+1];
            }
            --this->size;
            in = true;
            break;
        }
    }
    if (in == false)
    {
        cerr << value << " not in list" << endl;
    }
    return;
}

void SList::pop(int index)
{
    if (index < -1)
    {
        cerr << "Invalid index" << endl;
        return;
    }
    if (index >= this->size)
    {
        cerr << "pop index out of range" << endl;
        return;
    }
    if (index == -1)
    {
        --this->size;
    } else {
        for (int i = index; i < this->size; i++)
        {
            this->myArray[i] = this->myArray[i+1];
        }
        --this->size;
    }
    return;
}

void SList::clear()
{
    this->size = 0;
}

void SList::sort(bool reverse)
{
    if (reverse == false)
    {
        //选择排序 从小到大
        for (int i = 0; i < this->size; i++)
        {
            ElementType min = this->myArray[i];
            int index = i;
            for (int j = i + 1; j < this->size; j++)
            {
                if (this->myArray[j] < min)
                {
                    min = this->myArray[j];
                    index = j;
                }
            }
            this->myArray[index] = this->myArray[i];
            this->myArray[i] = min;
        }
        
    } else {
        //冒泡排序 从大到小
        for (int i = 0; i < this->size; i++)
        {
            for (int j = 0; j < this->size - i - 1; j++)
            {
                ElementType tmp = this->myArray[j];
                if (this->myArray[j] < this->myArray[j + 1])
                {
                    this->myArray[j] = this->myArray[j + 1];
                    this->myArray[j + 1] = tmp;
                }
            }
        }
    }
    return;
}

void SList::reverse()
{
    int font = 0;
    int back = this->size - 1;

    for (; font < back; font++, back--)
    {
        ElementType tmp = this->myArray[font];
        this->myArray[font] = this->myArray[back];
        this->myArray[back] = tmp;
    }
}

ElementType& SList::operator[](int index)
{
    if (index < 0 || index > this->size)
    {
        cerr << "bad index" << endl;
        ElementType tmp;
        exit(1);
    }
    return this->myArray[index];
}

二、动态数组实现:

DList.h

/* 使用动态数组实现列表 */

#include <iostream>

#ifndef __DLIST__H
#define __DLIST__H

typedef int ElementType;

class DList
{
private:
    int size;
    int capacity;
    ElementType* myArray;

public:
    DList(int capacity = 1024);
    DList(const DList& orignal);
    DList& operator=(const DList& rightHandSide);
    ~DList();

    bool empty() const; //判断是否为空列表
    int length() const; //获得列表大小
    void insert(int index, ElementType value); //在index处插入一个值
    void display(std::ostream& out) const; //输出列表
    void append(ElementType value); //尾部添加
    void remove(ElementType value); //移除值为value的第一个元素
    void pop(int index = -1); //移除指定位置元素,默认尾部移除
    void clear(); //清空列表
    void sort(bool reverse = false); //列表排序
    void reverse(); //列表翻转
    ElementType& operator[](int index);
};

std::ostream& operator<<(std::ostream& out, DList& s);

#endif

DList.cpp

#include "DList.h"

using namespace std;

DList::DList(int capacity): size(0), capacity(capacity)
{
    myArray = new ElementType[capacity];
}

DList::DList(const DList& orignal): size(orignal.size), capacity(orignal.capacity)
{
    delete [] myArray;
    myArray = new ElementType[capacity];
    for (int i = 0; i < size; i++)
    {
        myArray[i] = orignal.myArray[i];
    }
}

DList& DList::operator=(const DList& rightHandSide)
{
    delete [] myArray;
    size = rightHandSide.size;
    capacity = rightHandSide.capacity;
    myArray = new ElementType[capacity];
    for (int i = 0; i < size; i++)
    {
        myArray[i] = rightHandSide.myArray[i];
    }
}

DList::~DList()
{
    delete [] myArray;
}

bool DList::empty() const
{
    return size == 0;
}

int DList::length() const
{
    return this->size;
}

void DList::insert(int index, ElementType value)
{
    if (this->size == capacity)
    {
        cerr << "The list is full" << endl;
        return;
    }
    //判断索引是否有效
    if (index < 0)
    {
        cerr << "Invalid index" << endl;
        return;
    } else if (index < this->size) //中间插入
    {
        for (int i = this->size - 1; i >= index; i--)
        {
            this->myArray[i+1] = this->myArray[i];
        }
        this->myArray[index] = value;
        ++this->size;
    } else {
        this->myArray[this->size] = value;
        ++this->size;
    }
}

void DList::display(ostream& out) const
{
    out << "[";
    for (int i = 0; i < this->size; i++)
    {
        out << this->myArray[i] << ",";
    }
    out << "]" << endl;
}

void DList::append(ElementType value)
{
    if (this->size == capacity)
    {
        capacity *= 2;
        ElementType *arr = new ElementType[capacity];
        for (int i = 0; i < size; i++)
        {
            arr[i] = myArray[i];
        }
        delete [] myArray;
        myArray = new ElementType[capacity];
        for (int i = 0; i < size; i++)
        {
            myArray[i] = arr[i];
        }
        delete [] arr;
    }
    this->myArray[this->size] = value;
    ++this->size;
    return;
}

void DList::remove(ElementType value)
{
    bool in = false;
    for (int i = 0; i < this->size; i++)
    {
        if (this->myArray[i] == value)
        {
            for (int j = i; j < this->size; j++)
            {
                this->myArray[j] = this->myArray[j+1];
            }
            --this->size;
            in = true;
            break;
        }
    }
    if (in == false)
    {
        cerr << value << " not in list" << endl;
    }
    return;
}

void DList::pop(int index)
{
    if (index < -1)
    {
        cerr << "Invalid index" << endl;
        return;
    }
    if (index >= this->size)
    {
        cerr << "pop index out of range" << endl;
        return;
    }
    if (index == -1)
    {
        --this->size;
    } else {
        for (int i = index; i < this->size; i++)
        {
            this->myArray[i] = this->myArray[i+1];
        }
        --this->size;
    }
    return;
}

void DList::clear()
{
    this->size = 0;
}

void DList::sort(bool reverse)
{
    if (reverse == false)
    {
        //选择排序 从小到大
        for (int i = 0; i < this->size; i++)
        {
            ElementType min = this->myArray[i];
            int index = i;
            for (int j = i + 1; j < this->size; j++)
            {
                if (this->myArray[j] < min)
                {
                    min = this->myArray[j];
                    index = j;
                }
            }
            this->myArray[index] = this->myArray[i];
            this->myArray[i] = min;
        }
        
    } else {
        //冒泡排序 从大到小
        for (int i = 0; i < this->size; i++)
        {
            for (int j = 0; j < this->size - i - 1; j++)
            {
                ElementType tmp = this->myArray[j];
                if (this->myArray[j] < this->myArray[j + 1])
                {
                    this->myArray[j] = this->myArray[j + 1];
                    this->myArray[j + 1] = tmp;
                }
            }
        }
    }
    return;
}

void DList::reverse()
{
    int font = 0;
    int back = this->size - 1;

    for (; font < back; font++, back--)
    {
        ElementType tmp = this->myArray[font];
        this->myArray[font] = this->myArray[back];
        this->myArray[back] = tmp;
    }
}

ElementType& DList::operator[](int index)
{
    if (index < 0 || index > this->size)
    {
        cerr << "bad index" << endl;
        ElementType tmp;
        exit(1);
    }
    return this->myArray[index];
}

ostream& operator<<(ostream& out, DList& s)
{
    s.display(out);
    return out;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值