C++——实现Vector&List

1. Vector

< Vector.h >

#ifndef __VECTOR_H__
#define __VECTOR_H__

#include <iostream>
#include <stdlib.h>
#include <string.h>

typedef int DataType;

class Vector
{
public:
    Vector();
    Vector(const Vector& v);
    Vector& operator=(const Vector& v);
    ~Vector();

    size_t Size();
    size_t Capacity();
    void Expand(size_t n);
    void PushBack(DataType x);
    void Reserve(size_t n);
    void PopBack();
    void Insert(size_t pos, DataType x);
    void Erase(size_t pos);
    size_t Find(DataType x);
    void Swap(Vector& v);
    void Show();

private:
    DataType* _first;
    DataType* _finish;
    DataType* _endofstorage;
};

#endif

< Vector.cpp >

#include "Vector.h"

using namespace std;

Vector::Vector()   //构造函数
:_first(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}

Vector::~Vector()
{
    if (_first != nullptr)
    {
        delete[] _first;
    }
    _first = _finish = _endofstorage = nullptr;
}

Vector::Vector(const Vector& other)
{
    if (this != &other){
        DataType* cur = other._first;
        while (cur < other._finish){
            PushBack(*cur);
            cur++;
        }
    }
}

size_t Vector::Size()
{
    return _finish - _first;
}

size_t Vector::Capacity()
{
    return _endofstorage - _first;
}

void Vector::Swap(Vector& v)
{
    swap(_first, v._first);
    swap(_finish, v._finish);
    swap(_endofstorage, v._endofstorage);
}

Vector& Vector::operator=(const Vector& v)
{
    if (this != &v)
    {
        Vector tmp(v);
        Swap(tmp);
    }
    return *this;
}

void Vector::Expand(size_t n)
{
    if (n <= Capacity())
        return;

    DataType* newFirst = new DataType[n];
    DataType* newFinish = newFirst;
    DataType* newEnd = newFirst + n;

    DataType* cur = _first;
    while (cur < _finish){
        *newFinish = *cur;
        cur++;
        newFinish++;
    }

    //释放原空间
    delete[] _first;
    _first = newFirst;
    _finish = newFinish;
    _endofstorage = newEnd;
}

void Vector::PushBack(DataType x)
{
    if (_finish == _endofstorage)
    {
        size_t newCapacity = _endofstorage == nullptr ? 5 : 2 * Capacity();
        Expand(newCapacity);
    }
    *_finish++ = x;
}

void Vector::Reserve(size_t n)
{
    Expand(n);
}

void Vector::PopBack()
{
    if (_finish != nullptr && _finish != _first){
        --_finish;
    }
    else
    {
        printf("The Vactor is empty!\n");
    }
}

void Vector::Insert(size_t pos, DataType x)
{
    if (_first + pos > _finish){ //插入的位置不合法
        printf("The pos is Illegal!\n");
        return;
    }

    if (_finish == _endofstorage){  //如果顺序表满了,扩容
        Expand(2 * Capacity());
    }


    if (_first + pos == _finish){ //插入的位置是_finish, 直接调用尾插
        PushBack(x);
    }
    else
    {
        DataType* cur = _finish;
        while (cur > _first + pos){ //把pos后面的数据全部往后挪一位
            *cur = *(cur - 1);
            --cur;
        }
        *cur = x;
        _finish++;
    }
}

void Vector::Erase(size_t pos)
{
    if (_first + pos >= _finish){ //删除的位置不合法
        printf("The pos is Illegal!\n");
        return;
    }

    if (_first + pos == _finish - 1){ //尾删
        PopBack();
    }
    else
    {
        *(_first + pos) = *(_finish - 1);
        _finish--;
    }
}

size_t Vector::Find(DataType x)
{
    if (_first != nullptr){

        DataType* cur = _first;
        while (cur < _finish){
            if (*cur == x){
                return cur - _first;
            }
            cur++;
        }
        return -1;
    }
}

void Vector::Show()
{
    if (_first == nullptr){
        printf("The Vector is empty!\n");
    }
    else{
        DataType* cur = _first;
        while (cur < _finish){
            printf("%d ", *cur);
            cur++;
        }
        printf("\n");
    }
}

2. List

< List.h >

#ifndef __LIST_H__
#define __LIST_H__


#include<iostream>
using namespace std;

typedef int DataType;
struct ListNode
{
    DataType _data;
    ListNode* _next;
    ListNode* _prev;

    ListNode(DataType data)
        :_data(data)
        , _next(NULL)
        , _prev(NULL)
    {}
};


class List
{
    typedef  ListNode  Node;
public:
    void Show();//打印

    List();//构造
    List(const List& l);//拷贝构造
    void Swap(List& l);//交换
    List& operator=(const List& l);//赋值运算符重载
    ~List();//析构
    void PushBack(DataType x);//尾插
    void PopBack();//尾删
    void PushFront(DataType x);//头插
    void PopFront();//头删
    void Insert(Node* pos, DataType x);//任意位置插入
    void Erase(Node*  pos);//任意位置删除
    Node* Find(DataType x);//查找

private:
    Node* head;
};

#endif

< List.cpp >

#include "List.h"

void List::Show()//打印
{
    if (head->_next == head){
        printf("The List is empty!\n");
        return;
    }else{
        Node* cur = head->_next;
        while (cur != head){
            printf("%d -> ", cur->_data);
            cur = cur->_next;
        }
        printf("nullptr\n");
    }
}

List::List()//构造
:head(new Node(DataType()))
{
    head->_next = head;
    head->_prev = head; 
}


List::List(const List& l)//拷贝构造
{
    if (l.head == nullptr){
        return;
    }
    head = new Node(DataType());
    head->_next = head;
    head->_prev = head;

    Node* cur = l.head->_next;
    while (cur != l.head){
        PushBack(cur->_data);
        cur = cur->_next;
    }

}

void List::Swap(List& l)//交换
{
    swap(head, l.head);
}

List& List::operator=(const List& l)//赋值运算符重载
{
    if (this != &l){
        List tmp(l);
        Swap(tmp);
    }
    return *this;
}


List::~List()//析构
{
    if (head == nullptr){
        return;
    }

    Node* del = head;
    head->_prev->_next = nullptr;

    Node* tmp = del;
    while (del != nullptr){
        tmp = del->_next;
        delete del;
        del = tmp;
    }
    head->_next = nullptr;
    head->_prev = nullptr;
}

void List::PushBack(DataType x)//尾插
{
    if (head == NULL)//如果头为空
    {
        return;
    }

    Node* newNode = new Node(x);
    Node* tail = head->_prev;  //标记尾节点

    newNode->_prev = tail;
    newNode->_next = head;
    head->_prev = newNode;
    tail->_next = newNode;

}

void List::PopBack()//尾删
{
    if (head == nullptr || head->_next == head){
        return;
    }

    Node* del = head->_prev;
    head->_prev = del->_prev;
    del->_prev->_next = head;

    delete del;
}

void List::PushFront(DataType x)//头插
{
    Node* newNode = new Node(x);
    newNode->_prev = head;
    newNode->_next = head->_next; 
    head->_next->_prev = newNode;
    head->_next = newNode;
}

void List::PopFront()//头删
{
    if (head == nullptr || head->_next == head){
        return;
    }

    Node* del = head->_next;
    head->_next = del->_next;
    del->_next->_prev = head;

    delete del;
}

void List::Insert(Node* pos, DataType x)//任意位置插入
{
    if (pos == head->_next)
    {
        PushFront(x);
    }
    else if (pos == head->_prev){
        PushBack(x);
    }
    else{
        Node* newNode = new Node(x);

        newNode->_prev = pos->_prev;
        newNode->_next = pos;
        pos->_prev->_next = newNode;
        pos->_prev = newNode;
    }
}
void List::Erase(Node*  pos)//任意位置删除
{
    if (pos == head){
        return;
    }
    else if (pos == head->_next){
        PopFront();
    }
    else if (pos == head->_prev)
    {
        PopBack();
    }
    else{
        pos->_next->_prev = pos->_prev;
        pos->_prev->_next = pos->_next;
        delete pos;
        pos->_next = nullptr;
        pos->_prev = nullptr;
    }
}

List::Node* List::Find(DataType x)//查找
{
    if (head == nullptr || head->_next == head)
        return nullptr;

    Node* cur = head->_next;
    while (cur != head)
    {
        if (cur->_data == x)
        {
            return cur;
        }

        cur = cur->_next;
    }

    return nullptr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值