c++实现单链表

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once

//单向链表
#include <iostream>
#include <assert.h>

using namespace std;
typedef int DataType;


struct ListNode
{
    DataType _data;
    ListNode* _next;

    ListNode(DataType x)
        :_data(x)
        ,_next(NULL)
    {}
};

typedef ListNode Node;

class Slist
{

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

    ~Slist()
    {
        Destory();
    }

    Slist(const Slist& l)
        :_tail(NULL)
        ,_head(NULL)
    {
        Node* cur = l._head;
        if (cur == NULL)    //链表为空
        {
            return;
        }
        while (cur)
        {
            this->PushBack(cur->_data);
            cur = cur->_next;
        }
    }

    /*Slist& operator=(const Slist& s)//有问题
    {
        if (this != &s)
        {
            _head = NULL;
            _tail = NULL;
            Node* cur = s._head;
            while (cur)
            {
                if (_head == NULL)
                {
                    _head = new Node(s._head->_data);
                    _tail = _head;
                }
                else
                {
                    _tail->_next = new Node(cur->_data);
                    _tail = _tail->_next;
                }
                PushBack(cur->_data);
                cur = cur->_next;
            }
            _tail->_next = NULL;
        }
        return *this;
    }*/

    void Swap(Slist& s)
    {
        swap(_head ,s._head);
        swap(_tail,s._tail);
    }

    //Slist& operator=(const Slist& s)  //现代写法
    //{
    //  if (this != &s)
    //  {
    //      Slist tmp(s);
    //      Swap(tmp);
    //  }
    //  return *this;
    //}

    //Slist& operator=(Slist& s)    //现代写法优化
    //{
    //  if (this != &s)
    //  {
    //      swap(_head,s._head);
    //      swap(_tail,s._tail);
    //  }
    //  return *this;
    //}

    void PushBack(DataType x)     //1
    {
        //链表为空
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        //有节点
        else 
        {
            _tail->_next = new Node(x);
            _tail = _tail->_next;
        }   
    }


    void PopBack()              //1
    {
        //链表为空
        if (_head == NULL)
        {
            return;
        }
        //一个节点
        else if(_head->_next == NULL)//if(_head == _tail)
        {
            delete _tail;
            _head = _tail = NULL;
        }
        //多个节点
        else 
        {
            Node* cur = _head;
            while(cur->_next != _tail)
            {
                cur = cur->_next;
            }
            delete _tail;
            _tail = cur;
            _tail->_next = NULL;
        }
    }

    void PushFront(DataType x)  //1
    {
        //链表为空
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }

        //不为空
        else 
        {
            Node* tmp = _head;
            _head = new Node(x);
            _head->_next = tmp;
        }
    }

    void PopFront()         //1
    {
        //链表为空
        if (_head == NULL)
        {
            return;
        }
        //一个节点
        else if(_head->_next == NULL)//if(_head == _tail)
        {
            delete _head;
            _head = _tail = NULL;
        }
        //多个节点
        else 
        {
            Node* tmp = _head;
            _head = tmp->_next;
            delete tmp;
        }
    }
    //在pos前面插入
    void Insert(Node* pos,DataType x)
    {
        assert(pos);
        if (pos == _head)
        {
            this->PushFront(x);
        }
        else
        {
            Node* cur = _head;
            while (cur->_next != pos)
            {
                cur = cur->_next;
            }
            cur->_next = new Node(x);
            cur->_next->_next = pos;
        }
    }

    void Erase(Node* pos)//删除当前节点
    {
        assert(pos);
        if (_head == pos)
        {
            this->PopBack();
        }
        else
        {
            Node* cur = _head;
            while (cur->_next != pos)
            {
                cur = cur-> _next;
            }
            cur->_next = pos->_next;

            delete pos;
        }
    }


    Node* Find(DataType x)
    {
        assert(_head);
        Node* cur = _head;

        while(cur)
        {
            if (cur->_data == x)
            {
                return cur;
            }
            cur = cur->_next;
        }

    }

    void Print()            //1
    {
        Node* cur = _head;
        while (cur)
        {
            cout<<cur->_data<<" ";
            cur = cur->_next;
        }
        cout<<endl;
    }

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

    int length()
    {
        if (_head == NULL)
        {
            return 0;
        }
        else
        {
            int count = 0;
            Node* cur = _head;
            while (cur)
            {
                count++;
                cur = cur->_next;
            }
            return count;
        }
    }

    void Reverse()
    {
        if (_head == NULL || _head->_next == NULL)
        {
            return;
        }
        Node* cur = _head;
        Node* pre = _head;
        Node* del = _head->_next;

        while (cur->_next)
        {
            PushFront(cur->_next->_data);
            cur = cur->_next;
        }
        while (del)
        {
            Node* pdel = del;
            del = del ->_next;
            delete pdel;
        }

        _tail = pre;
        _tail->_next = NULL;
    }

private:
    Node* _head;
    Node* _tail;
};
#define _CRT_SECURE_NO_WARNINGS 1

#include "LinkedList.h"


#include <iostream>
using namespace std;
Slist l1;
Slist l2;
void test1()
{   
    //Pushback()
    //Popback();

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

    /* l2.PushBack(3);
     l2.PushBack(3);
     l2.PushBack(3);
     l2.PushBack(3);*/




     l2 = l1;
     /*l1.Reverse();*/
    /* l1.Find(2);*/

     //l1.Erase( l1.Find(2));
    /* l1.Insert(l1.Find(2),5);*/
     l2.Print();


     /*l1.PopBack();                
     l1.PopBack();              
     l1.PopBack();          
     l1.PopBack();              
     l1.PopBack();              
     l1.Print();    */
}
//

void test2()
{
    l1.PushFront(1);
    l1.PushFront(2);
    l1.PushFront(3);
    l1.PushFront(4);
    l1.Print(); 

    l1.PopFront();
    l1.PopFront();          //1
    l1.PopFront();
    l1.PopFront();          //1
    l1.PopFront();          //1
    //1
    l1.Print(); 



}
int main()
{


    test1();
    //test2();
    return 0 ;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值