文章标题

使用C++实现双向链表和顺序表

include

using namespace std;

typedef int Datetype;
struct Node//双向链表
{
Node(const Datetype & ch)
:_pnext(NULL)
, _pfront(NULL)
, _date(ch)
{

}
    Node* _pnext;
    Node* _pfront;
    Datetype _date;

};

class List
{
public:
List()
:_phead(NULL)//头结点初始化
{}
void Pushback(const Datetype & date)//尾插
{
if (_phead == NULL)
{
_phead = new Node(date);//有问题
_ptail = _phead;
}
else{
Node* tmp = _ptail;
_ptail->_pnext = new Node(date);
_ptail = _ptail->_pnext;
_ptail->_pfront = tmp;

    }
}
List(size_t p, Datetype &date)
{
    while (p)
    {
        Pushback(date);
        p--;
    }
}
void Popback()
{
    if (_phead == NULL)
    {
        return;
    }
    else if (_phead->_pnext == NULL)
    {
        delete _ptail;
        _phead = _ptail = NULL;
    }
    else{
        Node* tmp = _ptail;
        _ptail = _ptail->_pfront;
        delete tmp;
        _ptail->_pnext = NULL;
    }
}
void popback(size_t a1)
{
    size_t a = 0;
    Node *tmp = _phead;
    while (tmp != NULL)
    {
        tmp = tmp->_pnext;
        a++;
    }
    if (a1 < a)
    {
        return ;
    }
    else{
        while (a1)
        {
            Popback();
            a1--;

        }
    }
}
void Pushfrout(const Datetype date)
{
    if (_phead == NULL)
    {
        _phead = new Node(date);
        _phead = _ptail;
    }
    else{
        Node* tmp = _phead;
        _phead = new Node(date);
        _phead->_pnext = tmp;
        _phead->_pfront = NULL;
    }
}
void Popfrout()
{
    if (_phead == NULL)
    {
        return;
    }
    else if(_phead->_pnext==NULL){
        delete _phead;
        _phead = _ptail = NULL;
    }
    else{
        Node* tmp = _phead;
        _phead = _phead->_pnext;
        delete tmp;
        _phead->_pfront = NULL;
    }
}
Node* Find(const Datetype& data)
{
    Node *tmp = _phead;
    while (tmp != _ptail)
    {
        if (tmp->_date == data)
        {
            return tmp;
        }
        tmp = tmp->_pnext;
    }
    return;
}
void Insert(Node* pos, const Datetype& data)
{
    Node* tmp = new Node(data);
    Node* pos1 = pos;
    pos->_pnext = tmp;
    tmp->_pfront = pos1;
    tmp->_pnext = pos1->_pnext->_pfront;
    pos1->_pnext->_pfront = tmp;


}
void Erase(Node* pos)
{
    Node*tmp = pos;
    pos->_pfront->_pnext = pos->_pnext;
    pos->_pnext->_pfront = pos->_pfront;
    delete tmp;
    pos = NULL;
}
size_t Size()
{
    Node* tmp = _phead; size_t a = 0;
    while (tmp != _ptail)
    {
        tmp = tmp->_pnext;
        a++;
    }
    return a;
}
List(const List& l)
{
    Node* tmp = l._phead;
    while (tmp != l._ptail)
    {
        this->Pushback(tmp->_date);
        tmp = tmp->_pnext;
    }
}
List& operator=( List& l)
{
    size_t a1 = this->Size();
    size_t a2 = l.Size();
    if (a1 == a2)
    {
        Node* tmp = l._phead;
        while (tmp != l._ptail)
        {
            _phead->_date = l._phead->_date;
            tmp = tmp->_pnext;

        }
    }
    else if (a1 > a2)
    {
        size_t key = a1 - a2;
        while (key)
        {
            this->Popback();
                key--;
        }
        while (a2)
        {
            _phead->_date = l._phead->_date;
            a2--;
        }
    }
    else
    {
        size_t key = a2 - a1;
        while (key)
        {
            l.Popback();
            key--;
        }
        while (a2)
        {
            _phead->_date = l._phead->_date;
            a2--;
        }
    }
}
~List()
{
    Node*tmp = _phead;
    while (tmp != NULL)
    {
        Node* tmp1 = tmp;
        tmp = tmp->_pnext;
        delete tmp1;
    }
}

private:
Node* _phead;
Node* _ptail;
};

include //顺序表

using namespace std;
typedef int DataType;
class SeqList
{
public:
SeqList()
: _pData(new DataType[3])
, _capacity(3)
, _size(0)
{}

//SeqList(size_t n, DataType value);
//  // 浅拷贝 三大big 
//  SeqList(const SeqList& s)
//{
//      _pData = new DataType[s._capacity];
//      _capacity = s._capacity;

//      // 方式一: 
//      memcpy(_pData, s._pData, s._size*sizeof(DataType));

//      // 方式二: 
//      _size = 0;
//      for (; _size < s._size; ++_size)
//          _pData[_size] = s._pData[_size];
//  }

SeqList& operator=(const SeqList& s)
{
    delete _pData;
    _pData = new DataType[s._capacity];
    _capacity = s._capacity;
    _size = s._size;
    memcpy(_pData, s._pData, s._size*sizeof(DataType));


}
~SeqList()
{
    delete _pData;
    _pData = NULL;
}
void* PushBack(const DataType& data)
{
    _CheckCapacity();
    size_t tmp = _size;
    DataType* a = _pData;
    while (tmp)
    {
        a++;
        tmp--;

    }
    *a = data;

}
void PopBack()
{
    _size--;
}
void Insert(size_t pos, const DataType& data)
{
    _CheckCapacity();
    if (pos > _capacity)
    {
        return;
    }
    size_t tmp = _capacity;
    DataType* a = _pData;
    size_t tmp1 = _size;
    while (tmp1)
    {
        a++;
        tmp1--;
    }
    while (pos != tmp)
    {
        *a = *(a - 1);
        a--;
        tmp--;
    }
    *a = data;
    _size++;
}
void Erase(size_t pos)
{
    size_t tmp1 = _size-pos;
    DataType* a = _pData;
    while (pos)
    {
        a++;
        pos--;
    }
    while (tmp1)
    {
        *(a - 1) = *a;
        tmp1--;
    }
    _size--;
}
int Find(const DataType& data)
{
    DataType* a = _pData;
    size_t tmp = _size;
    while (tmp)
    {
        if (*a == data)
        {
            return 1;
        }
        tmp--;
    }
    return -1;
}
size_t Size()const
{
    return _size;
}
size_t Capacity()const
{
    return _capacity;
}
bool Empty()const
{
    if (_size == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
void Clear()
{
    _size = 0;
}
    // 把顺序表中的有效元素改变到size个 
void Resize(size_t size, const DataType& data)
{
    DataType* a = _pData;
    size_t tmp = _size;
    if (_size >= size)
    {
        _size = size;
    }
    else{
        size_t tmp1 = size-_size;

        while (tmp)
        {
            a++;
            tmp--;
        }
        a--;
        while (tmp1)
        {
            *a = data;
            a++;
        }
    }
}
void Display()
{
    size_t tmp = 0;
    while (tmp==_size)
    {
        cout << _pData[tmp]<<"->";
        tmp--;
    }
    cout << "over" << endl;
}
DataType& operator[](size_t index)
{
    DataType* a = _pData;
    while (index)
    {
        a++;
        index--;
    }
    a--;
    return *a;
}
const DataType& operator[](size_t index)const
{
    DataType* a = _pData;
    while (index)
    {
        a++;
        index--;
    }
    a--;
    return *a;
}
DataType& Front()
{
    return *_pData;
}
const DataType& Front()const
{
    return *_pData;
}
DataType& Back()
{
    return (_pData[_size]);
}
const DataType& Back()const
{
    return (_pData[_size]);
}

private:
void _CheckCapacity()
{
if (_size == _capacity)
{
DataType* pTemp = new DataType[_capacity * 2];
for (size_t idx = 0; idx < _size; ++idx)
pTemp[idx] = _pData[idx];

        delete[] _pData;
        _pData = pTemp;
        _capacity *= 2;
    }
}

private:
DataType* _pData;
size_t _capacity;
size_t _size;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值