c++封装双向链表和顺序表

本文介绍了一种双向链表及顺序表的数据结构实现,包括节点定义、基本操作如插入、删除等,并通过示例展示了如何使用这些数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

双向链表中每个节点结构如下

这里写图片描述

代码实现

#include <iostream>
#include <assert.h>
using namespace std;

typedef int DataType; 
struct Node 
{ 
    Node(const DataType& data) 
        : _pNext(NULL)//指向下一个节点
        , _pPre(NULL)//指向上一个节点
        , _data(data) //存放节点中的数据
    {
    } 

    Node* _pNext; 
    Node* _pPre; 
    DataType _data; 
}; 

class List 
{ 
public: 
    List() 
        : _pHead(NULL)//头节点初始置空 
    {} 
    List(size_t n, DataType data)
    {
        while (n--)
        {
            PushBack(data);
        }
    }
    void PushBack(const DataType& data)
    {
        if(_pHead == NULL)
        {
            _pHead = BuyNode(data);
            _tail = _pHead;
        }
        else
        {
            Node* tmp = _tail;
            _tail->_pNext = BuyNode(data);
            _tail = _tail->_pNext;
            _tail->_pPre = tmp;
        }
    }
    void PopBack()
    {
        if(_pHead == NULL)//空链表
        {
            return ;
        }
        else if (_pHead->_pNext == NULL)//一个节点
        {
            delete _pHead;
            _pHead = _tail = NULL;
        }
        else//多个节点
        {
            Node* tmp = _tail;
            _tail = _tail->_pPre;
            delete tmp;
            _tail->_pNext = NULL;
        }
    }
    void PushFront(const DataType& data)
    {
        if(_pHead == NULL)//空链表
        {
            _pHead = BuyNode(data);
            _tail = _pHead;
        }
        else
        {
            Node* tmp = BuyNode(data);
            tmp->_pNext = _pHead;
            tmp->_pPre = NULL;
            _pHead->_pPre = tmp;
            _pHead = tmp;
        }
    }
    void PopFront()
    {
        if(_tail == NULL)//空链表
        {
            return ;
        }
        else if (_tail->_pPre == NULL)//一个节点
        {
            delete _tail;
            _pHead = _tail = NULL;
        }
        else//多个节点
        {
            Node* tmp = _pHead;
            _pHead = _pHead->_pNext;
            delete tmp;
            _pHead->_pPre = NULL;
        }
    }
    Node* Find(const DataType& data)
    {
        Node* cur=_pHead;
        while (cur)
        {
            if(cur->_data==data)
            {
                return cur;
            }
            cur = cur->_pNext;
        }
    }
    void Insert(Node* pos, const DataType& data)
    {
        assert(pos);
        if (pos==_pHead)
        {
            PushFront(data);
        }
        else
        {
            Node* tmp = BuyNode(data);
            pos->_pPre->_pNext=tmp;
            tmp->_pNext = pos;
            tmp->_pPre = pos->_pPre;
            pos->_pPre = tmp;
        }
    }
    void Erase(Node* pos)
    {
        assert(pos);
        if (pos==_pHead)
        {
            Node* tmp=_pHead->_pNext;
            delete _pHead;
            _pHead = tmp;
            tmp->_pPre = NULL;
        }
        else if (pos==_tail)
        {
            Node* tmp = _tail->_pPre;
            delete _tail;
            _tail = tmp;
            tmp->_pNext = NULL;
        }
        else
        {
            Node* next = pos->_pNext;
            Node* pre = pos->_pPre;
            delete pos;
            pre->_pNext = next;
            next->_pPre = pre;
        }
    }
    size_t Size()
    {
        if (_pHead==NULL)
        {
            return 0;
        }
        size_t count=0;
        Node* cur = _pHead;
        while (cur != NULL)
        {
            cur = cur->_pNext;
            ++count;
        }
        return count;
    }
    bool Empty()const
    {
        if (_pHead==_tail==NULL)
        {
            return true;
        }
        return false;
    }
    List(const List& l)
        :_pHead(NULL)
        , _tail(NULL)
    {
        Node* tmp=l._pHead;
        while(tmp)
        {
            PushBack(tmp->_data);
            tmp = tmp->_pNext;
        }
    }
    List& operator=(const List& l)
    {
        if (this != &l)
        {
            List tmp(l);
            Swap(tmp);
        }
        return *this;
    }
    ~List()
    {
        Node* cur = _pHead;
        while (cur)
        {
            Node* tmp=cur;
            cur = cur->_pNext;
            delete tmp;
        }
    }
    void Swap(List& l)
    {
        swap(_pHead, l._pHead);
        swap(_tail, l._tail);
    }
    void Display()const
    {
        if (_pHead == NULL)
        {
            printf("NULL");
        }
        Node* cur = _pHead;
        while (cur)
        {
            cout << cur->_data << "->";
            cur = cur->_pNext;
        }
        cout <<endl;
        Node* tmp = _tail;
        while (tmp)
        {
            cout<< tmp->_data << "->";
            tmp = tmp->_pPre;
        }
        cout <<endl;
    }
private: 
    Node* BuyNode(const DataType& data) //新建节点
    { 
        return new Node(data); 
    } 
private:
    Node* _pHead; 
    Node* _tail;
}; 


void test1()//尾插、尾删
{
    List l1;
    l1.PushBack(1);
    l1.PushBack(2);
    l1.PushBack(3);
    l1.PushBack(4);
    l1.Display();

    l1.PopBack();
    l1.PopBack();
    l1.PopBack();
    l1.Display();
}
void test2()//头插、头删
{
    List l2;
    l2.PushFront(4);
    l2.PushFront(3);
    l2.PushFront(2);
    l2.PushFront(1);
    l2.Display();

    l2.PopFront();
    l2.PopFront();
    l2.PopFront();
    l2.Display();
}

void test3()//链表复制
{
    List l3;
    l3.PushFront(4);
    l3.PushFront(3);
    l3.PushFront(2);
    l3.PushFront(1);
    l3.Display();
    List l4(l3);
    l4.Display();
    List l5;
    l5 = l4;
    l5.Display();
}

void test4()//任意位置插入
{
    List l6;
    l6.PushBack(1);
    l6.PushBack(2);     
    l6.PushBack(4);
    l6.PushBack(6);
    l6.Display();
    cout << endl;
    Node* pos =l6. Find(1);
    l6.Insert(pos,0);
    Node* pos2 = l6.Find(4);
    l6.Insert(pos2, 3);
    Node* pos3 = l6.Find(6);
    l6.Insert(pos3, 5);
    l6.Display();
}

void test5()//任意位置删除
{
    List l7;
    l7.PushBack(1);
    l7.PushBack(2);
    l7.PushBack(3);
    l7.PushBack(4);
    l7.PushBack(5);
    Node* pos1 = l7.Find(1);
    l7.Erase(pos1);
    Node* pos2 = l7.Find(4);
    l7.Erase(pos2);
    Node* pos3 =l7.Find(5);
    l7.Erase(pos3);

    l7.Display();
}
int main()
{
    //test1();
    //test2();
    //test3();
    //test4();
    test5();
    return 0;
}

顺序表

#include <iostream>
using namespace std;

//1、实现顺序表的下列接口: 
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)
    {
        _pData = new DataType[s._capacity]; 
        _capacity = s._capacity;
        memcpy(_pData, s._pData, s._size*sizeof(DataType)); 
    }
    ~SeqList()
    {
        delete[](_pData);
        _pData=NULL;
    }
    void* PushBack(const DataType& data)
    {
        _size++;
        if(_size>=_capacity)
        {
            _CheckCapacity();
        }
        _pData[_size-1]=data;
    }
    void PopBack()
    {
        _size--;
    }
    void Insert(size_t pos, const DataType& data)
    {
        _size++;
        _CheckCapacity();
        size_t i=_size;
        while(i=_size; i>=pos; --i)
        {
            _pData[i]=_pData[i-1];
        }
        _pData[pos-1]=data ;
    }
    void Erase(size_t pos) 
    {
        _size--;
        while (size_t i=pos-1; i<_size; i--)
        {
            _pData[i]=_pData[i+1];
        }
    }
    int Find(const DataType& data)
    {
        int i=0;
        while (_pData[i++]!=data)
        {}
        if (i==_size-1)
        {
            return -1;
        }
        return 0;
    }
    size_t Size()const
    {
        return _size;
    }
    size_t Capacity()const
    {
        return _capacity;
    }
    bool Empty()const
    {
        if(_size==0)
        {
            return true;
        }
        return false;
    }
    void Clear()
    {
        _size=0;
    }
    // 把顺序表中的有效元素改变到size个 
    void Resize(size_t size, const DataType& data)
    {
        size_t count=_size-size;
        while (count; count>0; count--)
        {
            PopBack();
        }
    }
    DataType& operator[](size_t index)
    {
        return _pData[index];
    }
    const DataType& operator[](size_t index)const
    {
        return _pData[index];
    }
    DataType& Front()
    {
        return _pData[0];
    }
    const DataType& Front()const
    {
        return _pData[0];
    }
    DataType& Back()
    {
        return _pData[_size-1];
    }
    const DataType& Back()const
    {
        return _pData[_size-1];
    }

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; 
}; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值