链表的实现

双链表的实现

#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
struct  ListNode
{
    ListNode* next;
    ListNode* prev;
    DataType  data;

    ListNode(DataType x)
        :next(NULL)
        , prev(NULL)
        , data(x)

    {}
};
typedef ListNode Node;
class List
{
public:
    List()
        :_head(NULL)
        , _tail(NULL)
    {}
    List(const List &L)
        :_head(NULL)
        , _tail(NULL)
        {
        copy(L);
        }
    void copy(const List &L)
    {
        Node*cur = L._head;
        while (cur)
        {
            PushBack(cur->data);
            cur = cur->next;
        }
    }
    List &operator=(const List &L)
    {
        if (this != &L)
        {
            Destory();
            copy(L);
            return *this;
        }
    }
    void Destory()
    {
        if (_head == NULL)
        {
            Node*cur = _head;
            while (_head)
            {
                Node*cur = _head;
                cur = cur->next;
                delete cur;
            }
        }
    }
    ~List()
    {
        Destory();
    }
    void PushBack(DataType x)
    {
        if (_head == NULL)
        {
            Node *tmp = new Node(x);
            tmp->next = tmp->prev=NULL;
            _head = _tail = tmp;
        }
        else
        {
            Node*tmp = new Node(x);
            _tail->next = tmp;
            tmp->prev = _tail;
            _tail = tmp;
        }
    }
    void PopBack()
    {
        if (_head == NULL)
        {
            return;
        }
        else if (_head->next==NULL)
        {
            delete _head;
            _head = _tail = NULL;
        }
        else
        {
            Node*tmp =_tail;
            _tail = _tail->prev;
            _tail->next = NULL;
            delete tmp;
        }
    }
    void PushFront(DataType x)
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Node*tmp = new Node(x);
            tmp->next = _head;
            _head->prev = tmp;
            _head = tmp;
        }


    }
    void PopFront()
    {
        if (_head == NULL)
        {
            return;
        }
        else if (_head->next == NULL)
        {
            _head = _tail = NULL;
        }
        else
        {
            Node*tmp = _head;
            _head = _head->next;
            _head->prev = NULL;
            delete tmp;

        }
    }
    void Insert(Node* pos, DataType x)
    {
        assert(pos);
        if ((pos == _head)||(pos->prev==NULL))
        {
            PushFront(x);
        }
        else
        {
            Node*tmp = new Node(x);
            Node*front = pos->prev;
            tmp->prev = front;
            tmp->next = pos;
            front->next = tmp;
            pos->prev = tmp;
         }
    }
    void Erase(Node* pos)
    {
        assert(pos);
        if (pos ==0||pos->prev)
        {
            PopFront();
        }
        else if (pos == _tail||pos->next==NULL)
        {
            PopBack();
        }
        else
        {
            Node*Front = pos->prev;
            Node*Last = pos->next;
            Front->next = Last;
            Last->prev = Front;
            delete pos;
        }
    }
    void Reserve()
    {
        Node*tmp = _head;
        while (tmp)
        {
            swap(tmp->prev, tmp->next);
            tmp = tmp->prev;
        }
        swap(_head, _tail);
    }
    Node* Find(DataType x)
    {
        Node*tmp = _head;
        while(tmp!=NULL)
        {
            if (tmp->data == x)
            {
                return tmp;
                tmp = tmp->next;
            }
        }
        return NULL;

    }
    void Print()
    {
        Node*cur = _head;
        while (cur)
        {
            cout << cur->data << " ";
            cur=cur->next;
        }
        cout << endl;
    }

private:
    Node* _head;
    Node* _tail;

};
void TestList1()
{
    List L;
    L.PushBack(1);
    L.PushBack(2);
    L.PushBack(3);
    L.PushBack(4);
    L.Print();
    L.PopBack();
    L.PopBack();
    L.PopBack();
    L.PopBack();
    L.Print();

    L.PushFront(4);
    L.PushFront(3);
    L.PushFront(2);
    L.PushFront(1);
    L.Print();
    L.PopFront();
    L.PopFront();
    L.PopFront();
    L.PopFront();
    L.Print();
}
void TestList2()
{
    List L;
    L.PushFront(5);
    L.PushFront(4);
    L.PushFront(2);
    L.PushFront(1);
    L.Find(3);
    Node*tmp = L.Find(3);
    L.Insert(tmp,3);
    L.Print();

}
int main()
{
    //TestList1();
    TestList2();
    system("pause");
    return 0;
}

单链表的实现

#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
struct sListNode
{
    sListNode*_next;
    DataType _data;
    sListNode(DataType x)
        : _data(x)
        , _next(NULL)
    {}
};
typedef sListNode Node;
class sList
{
public:
    sList()
        :_head(NULL)
        , _tail(NULL)
    {}
    sList(const sList &s)
        :_head(NULL)
        , _tail(NULL)
    {
        Copy(s);
    }

    ~sList()
    {
        Destory();
    }
    sList &operator=(const sList &s)
    {
        Destory();
        Copy(s);
        return *this;
    }
    void Copy(const sList &s)
    {
        Node*cur = s._head;
        while (cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }

    }
    void 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) && (_tail==NULL))
        {
            return;
        }
        else
        {
            Node*cur = _head;
            while (cur != NULL)
            {
                cur = cur->_next;
            }
            delete cur;
        }
        _head = _tail = NULL;
    }
    void PushFront(DataType x)//没有数据 有数据
    {
        if ((_head == NULL) && (_tail == NULL))
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Node*cur = new Node(x);
            cur->_next = _head;
            _head=cur;
        }
    }
    void PopFront()//没有节点  有节点
    {
        if ((_head == NULL) && (_tail == NULL))
        {
            return;
        }
        else
        {
                Node*cur = _head;
                _head = _head->_next;
                   delete cur;
                 _head = _tail = NULL;
        }

    }
    Node* Find(DataType x)
    {
        Node*tmp = _head;
        while (tmp)
        {
            if ((tmp->_data) == x)
                return tmp;
            tmp = tmp->_next;
         }
    }
    void Insert(Node* pos, DataType x)
    {
        assert(pos);
        if (pos == 0)
        {
            PushBack(x);
        }
        else
        {
            Node*cur = _head;
            while (cur->_next != pos)
            {
                cur = cur->_next;
            }
            Node*tmp = new Node(x);
            cur->_next = tmp;
            tmp->_next = pos;
        }
    }
    void Erase(Node*pos)
    {
        assert(pos);
        if (pos == 0)
        {
            PopFront();
        }
        else if (pos->_next == NULL)
        {
            PopBack();
        }
        else
        {
            Node*cur = _head;
            while (cur->_next != pos)
            {
                cur = cur->_next;
            }
            Node*tmp = cur->_next;
            cur->_next = tmp->_next;
            delete tmp;
        }

    }
    void Print()
    {
        Node*cur = _head;
        while (cur != NULL)
        {
            cout << cur->_data << " ";
            cur = cur->_next;
        }
          cout << endl;
    }
private:
    Node* _head;
    Node*_tail;
};
void TestsList()
{
    sList s;
    s.PushBack(1);
    s.PushBack(2);
    s.PushBack(3);
    s.PushBack(4);
    s.Print();

    s.PopBack();
    s.PopBack();
    s.PopBack();
    s.PopBack();
    s.Print();

    s.PushFront(4);
    s.PushFront(3);
    s.PushFront(2);
    s.PushFront(1);
    s.Print();

    s.PopFront();
    s.PopFront();
    s.PopFront();
    s.PopFront();
    s.Print();

    s.PushBack(1);
    s.PushBack(2);
    s.PushBack(3);
    s.PushBack(4);
    s.Print();

    Node*tmp = s.Find(3);
    s.Insert(tmp, 3);
    s.Print();
    s.Erase(tmp);
    s.Print();


}
int main()
{
    TestsList();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值