564-数据结构(单向循环链表)

单向循环链表

在这里插入图片描述
在这里插入图片描述
如果某个节点的next地址域存储的是头节点的地址。
此时就证明这个节点是单向循环链表的末尾节点了。

为了方便尾插法,我们再定义一个tail指针指向尾节点,这样尾插法的时间复杂度而是O(1)
在这里插入图片描述

定义单向循环链表

private:
    struct Node//定义节点类型
    {
        Node(int data = 0) : data_(data), next_(nullptr) {}
        int data_;
        Node* next_;
    };

    Node* head_;//指向头节点
    Node* tail_;//指向末尾节点

构造函数和析构函数

构造函数图解:
在这里插入图片描述

//单向循环链表
class CircleLink
{
public:
    CircleLink()//构造函数 
    {
        head_ = new Node();
        tail_ = head_;
        head_->next_ = head_;
    }
    ~CircleLink()//析构函数 
    {
        Node* p = head_->next_;
        while (p != head_)
        {
            head_->next_ = p->next_;
            delete p;
            p = head_->next_;
        }
        delete head_;
    }

析构函数图解:
在这里插入图片描述
在这里插入图片描述
最后删除到只剩下头节点
在这里插入图片描述

尾插法

//尾插法  O(1)
void InsertTail(int val)
{
    Node* node = new Node(val);
    node->next_ = tail_->next_;//node->next_ = head_;
    tail_->next_ = node;//连起来
    tail_ = node;//更新tail_指针指向新的尾节点
}

头插法

//头插法
void InsertHead(int val)
{
    Node* node = new Node(val);
    node->next_ = head_->next_;
    head_->next_ = node;
    if (node->next_ == head_)//原本是空链表,现在插入1个新节点,所以要更新tail了
    {
        tail_ = node;
    }
}

原本是空链表,现在插入1个新节点,所以要更新tail指针了
在这里插入图片描述
如果原本不是空链表,头插法就不用更新tail指针了
在这里插入图片描述

删除节点

双指针思想
在这里插入图片描述
在这里插入图片描述
如果我们要删除的节点是67,我们现在就可以删除了。
关键是我们还要更新tail指针。
如果我们要删除的是尾节点,就要重置tail了
在这里插入图片描述
在这里插入图片描述

所以,我们要判断删除的节点是不是末尾节点

//删除节点
void Remove(int val)
{
    Node* q = head_;
    Node* p = head_->next_;

    while (p != head_)
    {
        if (p->data_ == val)
        {
            //找到删除节点   head 
            //                 q     
            q->next_ = p->next_;
            delete p;
            if (q->next_ == head_)//删除的是末尾节点
            {
                tail_ = q;
            }
            return;
        }
        else
        {
            q = p;
            p = p->next_;
        }
    }
}

查询

//查询
bool Find(int val) const
{
    Node* p = head_->next_;
    while (p != head_)
    {
        if (p->data_ == val)
        {
            return true;
        }
    }
    return false;
}

打印链表

//打印链表
void Show() const
{
    Node* p = head_->next_;
    while (p != head_)
    {
        cout << p->data_ << " ";
        p = p->next_;
    }
    cout << endl;
}

测试代码

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

//单向循环链表
class CircleLink
{
public:
    CircleLink()//构造函数 
    {
        head_ = new Node();
        tail_ = head_;
        head_->next_ = head_;
    }
    ~CircleLink()//析构函数 
    {
        Node* p = head_->next_;
        while (p != head_)
        {
            head_->next_ = p->next_;
            delete p;
            p = head_->next_;
        }
        delete head_;
    }

public:
    //尾插法  O(1)
    void InsertTail(int val)
    {
        Node* node = new Node(val);
        node->next_ = tail_->next_;//node->next_ = head_;
        tail_->next_ = node;//连起来 
        tail_ = node;//更新tail_指针指向新的尾节点
    }

    //头插法
    void InsertHead(int val)
    {
        Node* node = new Node(val);
        node->next_ = head_->next_;
        head_->next_ = node;
        if (node->next_ == head_)//原本是空链表,现在插入1个新节点,所以要更新tail了
        {
            tail_ = node;
        }
    }

    //删除节点
    void Remove(int val)//删除的是末尾节点
    {
        Node* q = head_;
        Node* p = head_->next_;

        while (p != head_)
        {
            if (p->data_ == val)
            {
                //找到删除节点   head 
                //                 q     
                q->next_ = p->next_;
                delete p;
                if (q->next_ == head_)
                {
                    tail_ = q;
                }
                return;
            }
            else
            {
                q = p;
                p = p->next_;
            }
        }
    }

    //查询
    bool Find(int val) const
    {
        Node* p = head_->next_;
        while (p != head_)
        {
            if (p->data_ == val)
            {
                return true;
            }
        }
        return false;
    }

    //打印链表
    void Show() const
    {
        Node* p = head_->next_;
        while (p != head_)
        {
            cout << p->data_ << " ";
            p = p->next_;
        }
        cout << endl;
    }

private:
    struct Node
    {
        Node(int data = 0) : data_(data), next_(nullptr) {}
        int data_;
        Node* next_;
    };

    Node* head_;//指向头节点
    Node* tail_;//指向末尾节点
};


int main()
{
    CircleLink clink;
    srand(time(NULL));

    clink.InsertHead(100);

    for (int i = 0; i < 10; i++)
    {
        clink.InsertTail(rand() % 100);
    }

    clink.InsertTail(200);
    clink.Show();

    clink.Remove(200);
    clink.Show();

    clink.InsertTail(300);
    clink.Show();
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林林林ZEYU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值