用C++实现单链表的基本函数以及增删查改

用C++实现双链表的增删查改以及双向链表的逆转

C++实现顺序表基本函数以及增删查改

单链表:
链表中的数据是以节点来表示的,每个结点的构成:元素( 数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。

#include<iostream>
#include<windows.h>
#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)
    {
        Node*cur = s._head;
        while (cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    //l1=l2(传统写法)
    SList&operator=(const SList &l)
    {
        if (this != &l)
        {
            this->Destroy();
            Node*head = l._head;
            while (head)
            {
                PushBack(head->_data);
                head = head->_next;
            }
            return *this;
        }
    }
    //l1=l2(现代写法)
    SList&operator=(SList l)
    {
        swap(_head, l._head);
        swap(_tail, l._tail);
        return *this;

    }
    ~SList()
    {
        Destroy();
    }

    void Destroy()
    {
        Node*cur = _head;
        while (cur)
        {
            Node*next = cur->_next;
            delete cur;
            cur = next;
        }
        _head = _tail = NULL;

    }
   //增删查改
    void PushBack(DataType x)
    {
        if (_head == 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* tmp = _head;
            while (tmp->_next->_next != NULL)
            {
                tmp = tmp->_next;
            }
            _tail = tmp;
            tmp->_next = NULL;
        }
    }
    void PushFront(DataType x)
    {
        if (_head == NULL)
        {
            _head=_tail = new Node(x);
        }
        else
        {
            Node *tmp = new Node(x);
            tmp->_next = _head;
            _head = tmp;
        }

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



    }
        // 插入一个节点在pos的前面 
    void Insert(Node* pos, DataType x)
    {
        assert(pos);
        Node*cur = _head;
        while (cur->_next != pos)
        {
            cur = cur->_next;
        }
        Node*tmp = new Node(x);
        cur->_next = tmp;
        tmp->_next = pos;
    }
    Node* Find(DataType x)//查找X
    {
        Node*tmp = _head;
        while (tmp)
        {
            if (tmp->_data == x)
            {
                return tmp;
            }
            tmp = tmp->_next;

        }
        return NULL;

    }
    void Erase(Node* pos)
    {
        assert(pos);
        if(pos->_next==NULL)
        {
            PopBack();
        }
        else
        {

            Node*cur = _head;
            while (cur->_next != pos)
            {
                cur = cur->_next;

            }

            cur->_next = pos->_next;
            delete pos;
        }

    }
    void Print()
    {
        Node*tmp = _head;
        while (tmp != NULL)
        {
            cout << tmp->_data << " ";
            tmp = tmp->_next;
        }
        cout << endl;
    }

private:
    Node* _head;
    Node* _tail;
};
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值