【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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章