C++实现单链表

链表相对于顺序表,有一个指向关系,当前节点的指针指向下一个节点,单链表是单向指向关系。此外,还有双链表,是双向指向关系。
链表主要实现数据的增删查改。具体代码实现如下:

//C++实现单链表
#pragma once
#include<iostream>
#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)
    {}
     //s1(s)
    SList(const SList& s)
        :_head(NULL)
        ,_tail(NULL)
    {
        Node* cur = s._head;
        while(cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
     //s=s1
    SList& operator=(const SList& s)
    {
        if(this != &s)//先销毁空间,再拷贝
        {
            Destroy();
            Node* cur = s._head;
            while(cur)
            {
                this->PushBack(cur->_data);
                cur = cur->_next;
            }
        }
        return *this;
    }

    ~SList()
    {
        Destroy();
    }

    void PushBack(DataType x)
    {
        if(_head == NULL)
            _head = _tail = new Node(x);
        else
        {
            Node* tmp = new Node(x);
            _tail->_next = tmp;
            _tail = tmp;
        }
    }

    void PopBack()
    {
        if(_head == NULL)//空节点
            return;
        else if(_head == _tail)//1个节点
        {
            delete _head;
            _head = _tail = NULL;
        }
        else           //多个节点
        {
            Node* tailPrev = _head;
            while(tailPrev->_next->_next)
            {
                tailPrev = tailPrev->_next;
            }
            delete _tail;
            _tail = tailPrev;
            _tail->_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 == NULL)
            return;
        else if(_head == _tail)
        {
            delete _head;
            _head = _tail = NULL;
        }
        else
        {
            Node* del = _head;
            _head = _head->_next;
            delete del;
        }
    }
    // 插入一个节点在pos的前面
    void Insert(Node* pos, DataType x)
    {
        assert(pos);
        if(pos == _head)
        {
            Node* cur = new Node(x);
            cur->_next = pos;
            _head = cur;  //PushFront()
        }
        else
        {
            Node* tmp = new Node(x);
            Node* prev = _head;
            while(prev->_next!=pos)
            {
                prev = prev->_next;
            }
            prev->_next = tmp;
            tmp->_next = pos;
        }
    }

    void Erase(Node* pos)
    {
        assert(pos);
        if(pos == _head)//PopFront()
        {
            Node* del = _head;
            _head = _head->_next;
            delete del;
            del = NULL;
        }
        else if(pos == _tail)//PopBack()
        {
            Node* del = _tail;
            Node* prev = _head;
            while(prev->_next!=_tail)
            {
                prev = prev->_next;
            }
            prev->_next = del;
            delete del;
            del = NULL;
            _tail = prev;
        }
        else  //pos位于中间
        {
            Node* del = pos;
            Node* prev = _head;
            while(prev->_next!=pos)
            {
                prev = prev->_next;
            }
            prev->_next = pos->_next;
            delete del;
            del = NULL;
        }
    }

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

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

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

private:
    Node* _head;
    Node* _tail;
}; 

测试用例:

void TestSList1()
{
    SList l1;

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

    SList l2(l1);
    SList l3;
    l3 = l1;
    l2.Print();
    l3.Print();

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

void TestSList2()
{
    SList l1;

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

    l1.PopFront();
    l1.PopFront();
    l1.PopFront();
    l1.PopFront();
    l1.PopFront();
    l1.Print();
}
void TestSList3()
{
    SList l1;

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

    SListNode* pos = l1.Find(3);
    l1.Insert(pos,33);
    l1.Print();

    l1.Erase(pos);
    l1.Print();

    SListNode* head = l1.Find(4);
    //l1.Insert(head,11);
    //l1.Print();

    l1.Erase(head);
    l1.Print();
}

主函数

#define _CRT_SECURE_NO_WARNINGS 1
//#include"Seqlist.h"
#include"SList.h"

int main()
{
    //TestSeqList1();
    //TestSeqList2();

    //TestSList1();
    //TestSList2();
    TestSList3();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值