【C++】 单链表 .cpp

之前,在C语言阶段使用了C编写单链表,简单易懂,那么,今天使用C++再次编写单链表,旨在对比两者之间的区别和异同:

下面就是cpp实现的代码:

SList.h文件:

#pragma once

typedef int DataType;

class SListNode
{
    friend class SList;

public:
    SListNode(DataType x)
        :_data(x)
        ,_next(NULL)
    {}

private:
    SListNode* _next;
    DataType _data;
};
class SList
{
public:
    SList()
        :_head(NULL)
        ,_tail(NULL)
    {}

    ~SList()
    {
        Clear();
    }

    SList(const SList& s)
        :_head(NULL)
        ,_tail(NULL)
    {
        SListNode* cur = s._head;
        while(cur)
        {
            this->PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    
    //赋值函数的现代写法
    SList& operator=(SList s)
    {
        swap(_head,s._head);
        swap(_tail,s._tail);

        return *this;
    }

    /*  赋值函数的传统写法
    SList& operator=(const SList &s)
    {
        if(this != &s)
        {
            this->Clear();
            SListNode* cur = s._head;
            while(cur)
            {
                this->PushBack(cur->_data);
                cur = cur->_next;
            }
        }
        return *this;
    }
    */

public:
    void PushBack(DataType x)
    {
        if(_head == NULL)
        {
            _head = new SListNode(x);
            _tail = _head;
        }
        else
        {
            _tail->_next = new SListNode(x);
            _tail = _tail->_next;        
        }

    }

    void PopBack()
    {
        if(_head == NULL)  // 空
        {
            return;
        }
        else if(_head->_next == NULL)  //一个节点
        {
            delete _head;
            _head = NULL;
        }
        else   //多个节点
        {
            SListNode *cur = _head;
            SListNode *prev = NULL;
            while(cur->_next)
            {
                prev = cur;
                cur = cur->_next;
            }
            free(cur);
            prev->_next = NULL;
        }
    }

    void PushFront(DataType x)
    {
        if(_head == NULL)   // ①.为空
        {
            _head = new SListNode(x);
            _head->_next = NULL;
        }
        else              //②.不为空
        {
            SListNode* tmp = new SListNode(x);
            tmp->_next = _head;
            _head = tmp; // 新的头节点
        }
    }

    void PopFront()
    {
        if(_head == NULL)  //空
        {
            return;
        }
        else if(_head->_next == NULL) //一个节点
        {
            delete _head;
            _head = NULL;
        }
        else              //多个节点
        {
            SListNode* tmp = _head;
            _head = _head->_next;
            delete tmp;
        }
    }

    void Insert(SListNode* pos,DataType x)
    {
        assert(pos);

        SListNode* tmp = new SListNode(x);
        tmp->_next = pos->_next;
        pos->_next = tmp;    
    }

    SListNode* Find(DataType x)
    {
        if(_head == NULL)
        {
            return NULL;
        }
        else
        {
            SListNode* cur = _head;
            while(cur)
            {
                if(cur->_data == x)
                {
                    return cur;
                }
                cur = cur->_next;
            }
        }
    }

    void Erase(SListNode* pos)
    {
        assert(pos);
        assert(_head);

        if(_head == pos)
        {
            _head = _head->_next;
            delete pos;
            return;
        }
        SListNode* prev = _head;
        while(prev)
        {
            if(prev->_next == pos)
            {
                prev->_next = pos->_next;
                delete pos;
                break;
            }
            prev = prev->_next;
        }
    }

    void Clear()
    {
        SListNode* cur = _head;

        while(cur)
        {
            SListNode* del = cur;
            cur = cur->_next;
            delete del;
        }
        
        _head = NULL;
        _tail = NULL;
    }

    void PrintSList()
    {
        SListNode* cur = _head;

        while(cur)
        {
            cout<<cur->_data<<"->";
            cur = cur->_next;
        }

        cout<<"NULL"<<endl;
    }

private:
    SListNode* _head; //指向第一个节点的指针
    SListNode* _tail; //指向最后一个节点的指针
};

void Test1()
{
    SList s1;
    s1.PushBack(1);//是将s1传向this指针
    s1.PushBack(2);
    s1.PushBack(3);
    s1.PushBack(4);
    s1.PushBack(5);
    s1.PrintSList();

    SList s2(s1);//测试拷贝构造
    s2.PrintSList();

    SList s3;
    s3 = s2; //测试赋值运算符重载
    s3.PrintSList();

    s1.PopBack();
    s1.PrintSList();

    s2.PopFront();
    s2.PrintSList();

    s3.Insert(s3.Find(3),6);
    s3.PrintSList();

    s3.Erase(s3.Find(3));
    s3.PrintSList();
}

SList.cpp文件:

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

#include "SList.h"

int main()
{
    Test1();
    return 0;
}


本文出自 “Vs吕小布” 博客,谢绝转载!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值