【C++】用类实现单向单链表的尾插PushBack(),尾删PopBack(),打印PrintSlist()。

建立源文件,命名为:Slist.cpp。
#include"Slist.h"
int main()
{
    Test();
    system("pause");
    return 0;
}


建立头文件,命名为:Slist.h。

#ifndef __SLISH_H__
#define __SLIST_H__

#include<iostream>
using namespace std;


typedef int DataType;

class SlistNode
{
    friend class Slist;
public:
    SlistNode(DataType x)
        :_next(NULL)
        , _data(x)
    {}
private:
    DataType _data;
    SlistNode* _next;
};


class Slist
{
public:
    Slist()
        :_head(NULL)
        , _tail(NULL)
    {}

    Slist(const Slist& s)
        :_head(NULL)
        , _tail(NULL)
    {
        SlistNode* cur = s._head;
        while (cur)
        {
            this->PushBack(cur->_data);
            cur = cur->_next;
        }
    }

    Slist& operator= (const Slist& s)
    {
        Slist tmp;
        SlistNode* pcur = _head;
        while (pcur)
        {
            SlistNode* del = pcur;
            pcur = pcur->_next;
            delete del;
            del = NULL;
        }
        tmp = s;
        SlistNode* cur = s._head;
        while (cur)
        {
            this->PushBack(cur->_data);
            cur = cur->_next;
        }    
    }

    ~Slist()
    {
        SlistNode* cur = _head;
        while (cur)
        {
            SlistNode* del = cur;
            cur = cur->_next;
            delete del;
            del = NULL;
        }
    }

    void PushBack(DataType x)
    {
        //0  1多
        if (_head == NULL)
        {
            _head = new SlistNode(x);
            _tail = _head;
        }
        else
        {
            /*_tail->_next = new SlistNode(x);
            _tail = _tail->_next;    */    
            SlistNode* cur = new SlistNode(x);
            _tail->_next = cur;
            _tail = cur;
        }
    }

    void PopBack()
    {
        if (_head == _tail)
        {
            if (_head == NULL)
            {
                return;
            }
            else
            {
                delete _head;
                _head = NULL;
                _tail = NULL;
            }
        }
        else
        {
            SlistNode* cur = _head;
            while (cur)
            {
                SlistNode* _next = cur->_next;
                if (_next == _tail)
                {
                    delete _tail;
                    _tail = NULL;
                    _tail = cur;
                    _tail->_next = NULL;
                }
                cur = cur->_next;
            }
        }
    }

    void PrintSlist()
    {
        if (_head== NULL)
        {
            return;
        }
        else
        {
            SlistNode* cur = _head;
            while (cur)
            {
                cout << cur->_data << " ";
                cur = cur->_next;
            }
            cout << endl;
        }

    }
private:
    SlistNode* _head;
    SlistNode* _tail;
};


void Test()
{
    Slist s;
    s.PushBack(1);
    s.PushBack(2);
    s.PushBack(3);
    s.PushBack(4);
    s.PushBack(5);
    s.PrintSlist();

    s.PopBack();
    s.PrintSlist();

}

#endif    //__SLIST_H__


本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1747325

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值