c++实现单链表,双向链表,顺序表的接口(非模板)

在以前使用c实现单链表的时候,没有使用函数的复用去完成相应的接口操作,这次使用尽量多的复用去完成链表和顺序表的操作(包括故意使用insert()去复用pushback()这个逻辑是错的,只是为了 体现复用的简便)

单链表

struct SListNode
{
    SListNode* _next;
    DataType _data;
    SListNode(DataType x)
        :_data(x)
        ,_next(NULL)
    {}
};
class SList
{
    typedef SListNode Node;
public:
    SList()
        :_head(NULL)
        , _tail(NULL)
    {}
    SList(const SList& s)
        :_head(NULL)
        , _tail(NULL)
    {
        Node*cur = s._head;
        while (cur->_next)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    SList& operator=(const SList& s)
    {
        Node*tmp=_head;
        if(tmp)
        {
            tmp=tmp->next;
            delete _head;
            _head=tmp;
        }
        _head=NULL;
        Node*cur = s._head;
        while (cur->_next)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    ~SList()
    {
        while (_head != _tail)
        {
            Node*tmp = _head;
            delete[] _head;
            _head = tmp;
        }
        delete[] _tail;
        _head = _tail = NULL;
    }
    void PushBack(DataType x)
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        _tail->_next = new Node(x);
        _tail = _tail->_next;
    }
    void PopBack()
    {
        if (_head == NULL)
            return;
        else if (_head = _tail)
        {
            delete _head;
            _tail = _head = NULL;
        }
        else
        {
            Node*tmp = _head;
            while (tmp->_next->_next)
            {
                tmp = tmp->_next;
            }
            delete[]_tail;
            _tail = tmp;
            _tail->_next = NULL;
        }
    }
    void PushFront(DataType x)
    {
        Node*tmp = new Node(x);
        tmp->_next = _head;
        _head = tmp;
    }
    void PopFront()
    {
        if (_head == NULL)
            return;
        else if (_head = _tail)
        {
            delete _head;
            _tail = _head = NULL;
        }
        else
        {
            Node*tmp = _head->_next;
            delete[] _head;
            _head = tmp;
        }
    }
        // 插入一个节点在pos的前面 
    void Insert(Node* pos, DataType x)
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        else if (_head == _tail)
        {
            _head = new Node(x);
            _head->_next = _tail;
        }
        else
        {
            Node *tmp = _head;
            while (tmp->_next != pos)
            {
                tmp = tmp->_next;
            }
            Node*tail = new Node(x);
            tmp->_next = tail;
            tail->_next = pos;
        }
    }
    void Erase(Node* pos)
    {
        if (_head == NULL)
            return;
        else if (_head = _tail)
        {
            delete[] _head;
            _head = _tail = NULL;
        }
        else if (pos->_next == NULL)
        {
            PopBack();
        }
        Node *tmp = _head;
        while (tmp->_next != pos)
        {
            tmp = tmp->_next;
        }
        tmp->_next = pos->_next;
        delete[] pos;
    }
    void Print()
    {
        Node*tmp = _head;
        while (tmp)
        {
            cout << tmp->_data;
        }
        cout << endl;
    }

private:
    Node* _head;
    Node* _tail;
};

双向链表

typedef int DataType;
struct ListNode
{
    ListNode* _next;
    ListNode* _prev;
    DataType _data;
    ListNode(DataType x)
        :_data(x)
        , _next(NULL)
        , _prev(NULL)
    {}
};

class List
{
    typedef ListNode Node;
public:
    List()
        : _head(NULL)
        , _tail(NULL)
    {}
    List(const List&s)
        :_head(NULL)
        ,_tail(NULL)
    {
        Node*cur = s._head;
        while (cur->_next)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }

    List& operator=(const List& s)
    {
            SList(const SList& s)
        :_head(NULL)
        , _tail(NULL)
    {
        Node*cur = s._head;
        while (cur->_next)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    SList& operator=(const SList& s)
    {
        Node*tmp=_head;
        if(tmp)
        {
            tmp=tmp->next;
            delete _head;
            _head=tmp;
        }
        _head=NULL;
        Node*cur = s._head;
        while (cur->_next)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    ~List()
    {
        while (_head != _tail)
        {
            Node*tmp = _head;
            delete[] _head;
            _head = tmp;
        }
        delete[] _tail;
        _head = _tail = NULL;
    }
    void PushBack(DataType x)
    {
        Insert(_tail, x);
    }
    void PopBack()
    {
        Erase(_tail);
    }
    void PushFront(DataType x)
    {
        Insert(_head, x);
    }
    void PopFront()
    {
        Erase(_head);
    }
    // 在pos的前面插入一个 
    void Insert(Node* pos, DataType x)
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        else if (_head == _tail)
        {
            _head = new Node(x);
            _head->_next = _tail;
            _tail->_prev = _head;
        }
        else
        {
            Node*tmp = new Node(x);
            Node*prev = pos->_prev;
            prev->_next = tmp;
            tmp->_prev = prev;
            tmp->_next = pos;
            pos->_prev = tmp;
        }
    }
    void Erase(Node* pos)
    {
        if (_head == NULL)
            return;
        else if (_head = _tail)
        {
            delete[] _head;
            _head = _tail = NULL;
        }
        else if (pos->_next == NULL)
        {
            Node *tmp = pos->_prev;
            delete[] _tail;
            _tail = tmp;
        }
        else if (pos->_prev == NULL)
        {
            Node *tmp = pos->_next;
            delete _head;
            _head = tmp;
        }
        else
        {
            Node*prev = pos->_prev;
            Node*next = pos->_next;
            prev->_next = next;
            next->_prev = prev;
            delete[] pos;
        }
    }
    Node* Find(DataType x)
    {
        Node*tmp = _head;
        while (tmp)
        {
            if (tmp->_data == x)
            {
                return tmp;
            }
            tmp = tmp->_next;
        }
        return NULL;
    }
    void Reverse()
    {
        Node *tmp = _head;
        Node *cur = _head;
        while (_head)
        {
            cur->_next = _head->_prev;
            cur->_prev = _head->_next;
            _head = _head->_next;
        }
        _tail = tmp;
    }
private:
    Node* _head;
    Node* _tail;
};

顺序表

typedef int DataType;

class SeqList
{
public:
    SeqList(size_t size=1)
    {
        _array = (DataType*)malloc(sizeof(size)*size);
        _size = size;
        _capacity = size;
    }
    SeqList(const SeqList& s)
    {
        _array = (DataType*)malloc(_size);
        for (size_t i = 0; i++; i < _size)
        {
            _array[i] = s._array[i];
        }
        _size = s._size;
        _capacity = s._size;
    }
    ~SeqList()
    {
        free (_array);
    }
    void Swap(SeqList s)
    {
        swap(_array, s._array);
        _size = s._size;
        _capacity = s._capacity;
    }
    void PushBack(DataType x)
    {
        CheckCapcacity();
        _array[_size] = x;
        _size++;
    }
    void PopBack()
    {
        _size--;
    }
    void PushFront(DataType x)
    {
        CheckCapcacity();
        for (size_t i = _size; i--; i > 0)
        {
            _array[_size] = _array[_size - 1];
        }
        _array[0] = x;
    }
    void PopFront()
    {
        for (int i = 1; i++; i < _size)
        {
            _array[i] = _array[i - 1];
        }
        _size--;
    }
    void Insert(int pos, DataType x)
    {
        CheckCapcacity();
        if (pos == 0)
        {
            PushFront(x);
        }
        else
        {
            for (int i = _size; i--; i > pos)
            {
                _array[i] = _array[i - 1];
            }
            _array[pos] = x;
        }
        _size++;
    }
    void Erase(size_t pos)
    {
        if (pos == _size - 1)
            PopBack();
        for (int i = pos; i++;i<_size)
        {
            _array[i] = _array[i + 1];
        }
        _size--;
    }
    DataType& operator[](size_t pos)
    {
        return _array[pos];
    }
    void CheckCapcacity()
    {
        if (_size = _capacity)
        {
            DataType*tmp = new DataType(_capacity * 2);
            for (size_t i = 0; i <= _size; i++)
            {
                tmp[i] = _array[i];
            }
            swap(tmp, _array);
            _capacity *= 2;
        }
    }
    void Print()
    {
        for (size_t i = 0; i++; i < _size)
        {
            cout<<_array[i];
        }
        cout << endl;
    }
private:
    DataType* _array;
    size_t _size;
    size_t _capacity;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值