C++简单实现顺序表和单链表

顺序表

#include 
#include 
using namespace std;

typedef int DataType;

class SeqList
{
public:
    SeqList()
        : _array(NULL)
        , _size(0)
        , _capacity(0)
    {}
    SeqList(const SeqList& s)
    {
        _array = (DataType*)malloc(sizeof(DataType)*s._size);
        memcpy(_array, s._array, sizeof(DataType)*s._size);
        _size = _capacity = s._size;
    }
    SeqList& operator=(SeqList s)
    {
        if (this != &s)
        {
            SeqList tmp(s);
            swap(_array, tmp._array);
            swap(_size, tmp._size);
            swap(_capacity, tmp._capacity);
        }
    }
    ~SeqList()
    {
        if (_array)
        {
            free(_array);
            _size = _capacity = 0;
            _array = NULL;
        }
    }

    void Swap(SeqList& s)
    {
        swap(_array, s._array);
        swap(_size, s._size);
        swap(_capacity, s._capacity);
    }
    void PushBack(DataType x)
    {
        Insert(_size, x);
    }
    void PopBack()
    {
        assert(_size > 0);
        --_size;
    }
    void PushFront(DataType x)
    {
        Insert(0, x);
    }
    void PopFront()
    {
        Erase(0);
    }
    void Insert(size_t pos, DataType x)
    {
        assert(pos <= _size);
        CheckCapcacity();
        for (int end = _size - 1; end >= (int)pos; --end)
        {
            _array[end + 1] = _array[end];
        }
        _array[pos] = x;
        ++_size;
    }
    void Erase(size_t pos)
    {
        assert(pos < _size);
        for (size_t i = pos+1; i < _size; ++i)
        {
            _array[i-1] = _array[i];
        }
        --_size;
    }
    DataType& operator[](size_t pos)
    {
        assert(pos < _size);
        return _array[pos];
    }
    void CheckCapcacity()
    {
        if (_size == _capacity)
        {
            _capacity = 2 * _capacity + 3;
            _array = (DataType*)realloc(_array, _capacity*sizeof(DataType));
            assert(_array);
        }
    }
    void Print()
    {
        for (size_t i = 0; i < _size; ++i)
        {
            cout << _array[i] << " ";
        }
        cout << endl;
    }
private:
    DataType* _array;
    size_t _size;
    size_t _capacity;
};

无头单链表

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

typedef int DataType;

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)
    {

        Copy(s);
    }
    SList& operator=(const SList& s)
    {
        if (this != &s)
        {
            Destory();
            Copy(s);
        }
        return *this;
    }
    ~SList()
    {
        Destory();
    }
    Node* Copy(const SList& s)
    {
        Node* cur = s._head;
        while (cur != NULL)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    Node* Destory()
    {
        Node* cur = _head;
        while (_head != NULL)
        {
            cur = _head;
            _head = cur->_next;
            delete cur;
        }
        _head = _tail = NULL;
    }
    void PushBack(DataType x)
    {
        if (_head == NULL && _tail == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            _tail->_next = new Node(x);
            _tail = _tail->_next;
        }
    }
    void PopBack()
    {
        if (_head == NULL)
        {
            return;
        }
        else if (_head->_next = NULL)
        {
            delete _head;
            _head = _tail = NULL;
        }
        else
        {
            Node* prev = _head;
            while (prev->_next->_next != NULL)
            {
                prev = prev->_next;
            }
            _tail = prev;
            prev->_next = NULL;
        }
    }
    void PushFront(DataType x)
    {
        if (_head == NULL && _tail == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Node* tmp = new Node(x);
            tmp->_next = _head;
            _head = tmp;
        }
    }
    void PopFront()
    {
        if (_head == NULL)
        {
            return;
        }
        Node* prev = _head;
        prev = _head->_next;
        delete prev;

    }
        // 插入一个节点在pos的前面 
    void Insert(Node* pos, DataType x)
    {
        assert(pos);
        if (pos == _head)
        {
            PushFront(x);
        }
        else
        {
            Node* prev = _head;
            while (prev->_next != pos)
            {
                prev = prev->_next;
            }
            Node* tmp = new Node(x);
            prev->_next = tmp;
            tmp->_next = pos;
        }
    }
    void Erase(Node* pos)
    {
        assert(pos);
        if (pos == _head)
        {
            PopFront();
        }
        else if (pos == _tail)
        {
            PopBack();
        }
        else
        {
            Node* prev = _head;
            while (prev->_next != pos)
            {
                prev = prev->_next;
            }
            prev->_next = pos;
            delete pos;
        }
    }
    void Print()
    {
        Node* tmp = _head;
        while (tmp != NULL)
        {
            cout << tmp->_data << " ";
            tmp = tmp->_next;
        }
        cout << endl;
    }

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

你可能感兴趣的:(CPP)