数据结构实战开发教程(五)再论智能指针、循环链表的实现、双向链表的实现、双向循环链表的实现、Linux内核链表剖析

本文深入探讨了智能指针的实现,包括其设计思路、计数机制和在链表中的应用。接着介绍了循环链表和双向链表的概念、实现细节以及在约瑟夫问题中的应用。此外,文章还详细剖析了Linux内核链表的结构、宏定义及其移植和使用方法,以及如何基于Linux内核链表实现双向循环链表。
摘要由CSDN通过智能技术生成

二十七、再论智能指针(上)

1、思考

  • 使用智能指针( SmartPointer )替换单链表( LinkList )中的原生指针是否可行?

2、问题出在哪里?

  • SmartPointer的设计方案
    • 指针生命周期结束时主动释放堆空间
    • 一片堆空间最多只能由一个指针标识
    • 杜绝指针运算和指针比较

3、新的设计方案

是时候创建新的智能指针了!

4、新的设计方案

  • Pointer智能指针的抽象父类(模板)
    • 纯虚析构函数virtual ~Pointer() = 0;
    • 重载operator -> ()
    • 重载operator * ()

5、编程实验:智能指针的新方案

Pointer.h

SmartPointer.h

6、To be continued

二十八、再论智能指针(下)

1、完成SharedPointer类的具体实现

 

2、SharedPointer设计要点

  • 类模板
    • 通过计数机制( ref )标识堆内存
      • 堆内存被指向时:ref++
      • 指针被置空时:ref--
      • ref == 0 时:释放堆内存

3、计数机制原理剖析

4、SharedPointer类的声明

5、智能指针的比较

由于SharedPointer支持多个对象同时指向一片堆空间;因此,必须支持比较操作

6、编程实验:智能指针的新成员

SharedPointer.h

#ifndef SHAREDPOINTER_H_
#define SHAREDPOINTER_H_
/*******************************************************************************
 *                              Include _Files                                  *
 *******************************************************************************/
#include <cstdlib>
#include "Pointer.h"
#include "Exception.h"
/*******************************************************************************
 *                             Type Definition                                 *
 *******************************************************************************/
namespace DTLib
{

template < typename T >
class SharedPointer : public Pointer<T>
{
protected:
    int* m_ref;                                                             // 计数机制成员指针

    // 获取当前的智能指针地址、计数变量地址、计数变量 +1
    void assign(const SharedPointer<T>& obj)
    {
        this->m_ref = obj.m_ref;                                            // 获取共用的计数变量地址
        this->m_pointer = obj.m_pointer;                                    // 获取共用的堆空间地址

        if( this->m_ref )
        {
            (*this->m_ref)++;                                               // 计数变量 +1
        }
    }

public:
    // 构造函数
    SharedPointer(T* p = NULL) : m_ref(NULL)
    {
        if( p )                                                             
        {
            this->m_ref = static_cast<int*>(std::malloc(sizeof(int)));      // 申请计数变量堆空间

            if( this->m_ref )
            {
                *(this->m_ref) = 1;                                         // 计数变量 +1
                this->m_pointer = p;                                        // 获取p对应的堆空间
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create SharedPointer object ...");
            }
        }
    }

    SharedPointer(const SharedPointer<T>& obj) : Pointer<T>(NULL)
    {
        assign(obj);                                                        // 获取当前的智能指针地址、计数变量地址、计数变量 +1
    }

    SharedPointer<T>& operator= (const SharedPointer<T>& obj)
    {
        if( this != &obj )
        {
            clear();                                                        // 让当前的智能指针置空,计数变量 -1
            assign(obj);                                                    // 获取当前的智能指针地址、计数变量地址、计数变量 +1
        }

        return *this;
    }

    // 让当前的智能指针置空,计数变量 -1
    void clear()
    {
        T* toDel = this->m_pointer;
        int* ref = this->m_ref;

        this->m_pointer = NULL;                                             // 智能指针指向的堆空间地址 置 NULL
        this->m_ref = NULL;                                                 // 智能指针指向的计数变量地址 置 NULL

        if( ref )
        {
            (*ref)--;                                                       // 计数变量 -1

            if( *ref == 0 )                                                 // 计数变量为 0
            {
                free(ref);                                                  // 释放共用的计数变量的堆空间

                delete toDel;                                               // 释放共用的堆空间
            }
        }
    }

    ~SharedPointer()
    {
        clear();                                                            // 让当前的智能指针置空,计数变量 -1
    }
};

// 支持比较操作  智能指针地址 <==> 智能指针地址 <==> 类对象地址
template < typename T >
static bool operator == (const T* l, const SharedPointer<T>& r)
{
    return (l == r.get());
}

template < typename T >
static bool operator != (const T* l, const SharedPointer<T>& r)
{
    return (l != r.get());
}

template < typename T >
static bool operator == (const SharedPointer<T>& l, const T* r)
{
    return (l.get() == r);
}

template < typename T >
static bool operator != (const SharedPointer<T>& l, const T* r)
{
    return (l.get() != r);
}

template < typename T >
static bool operator == (const SharedPointer<T>& l, const SharedPointer<T>& r)
{
    return (l.get() == r.get());
}

template < typename T >
static bool operator != (const SharedPointer<T>& l, const SharedPointer<T>& r)
{
    return !(l == r);
}
}

#endif // SHAREDPOINTER_H_

7、智能指针的使用军规

  • 只能用来指向堆空间中的单个变量(对象)
  • 不同类型的智能指针对象不能混合使用
  • 不要使用delete释放智能指针指向的堆空间

8、小结

  • SharedPointer最大程度的模拟了原生指针的行为
  • 计数机制确保多个智能指针合法的指向同一片堆空间
  • 智能指针只能用于指向堆空间中的内存
  • 不同类型的智能指针不要混合使用
  • 堆对象的生命周期由智能指针进行管理

二十九、循环链表的实现

1、什么是循环链表?

  • 概念上
    • 任意数据元素都有一个前驱和一个后继
    • 所有的数据元素的关系构成一个逻辑上的环
  • 实现上
    • 循环链表是一种特殊的单链表
    • 尾结点的指针域保存了首结点的地址

2、循环链表的逻辑构成

3、循环链表的继承层次结构

4、循环链表的实现思路

  • 通过模板定义CircleList类,继承自LinkList
  • 定义内部函数last_to_first(),用于将单链表首尾相连
  • 特殊处理:首元素的插入操作和删除操作
  • 重新实现:清空操作和遍历操作

5、循环链表的实现要点

  • 插入位置为0时:
    • 头结点和尾结点均指向新结点
    • 新结点成为首结点插入链表
  • 删除位置为0时:
    • 头结点和尾结点指向位置为1的结点
    • 安全销毁首结点

6、编程实验:循环链表的实现

CircleList.h

#ifndef CIRCLELIST_H_
#define CIRCLELIST_H_
/*******************************************************************************
 *                              Include _Files                                  *
 *******************************************************************************/
#include "LinkList.h"
/*******************************************************************************
 *                             Type Definition                                 *
 *******************************************************************************/
namespace DTLib
{

template < typename T >
class CircleList : public LinkList<T>
{
protected:
    typedef typename LinkList<T>::Node Node;                        // 使用typename的原因:编译器无法辨识标识符究竟是什么(是一个类型还是一个成员名称(静态数据成员或者静态函数))

    int mod(int i) const                                            // 取余
    {
        return (this->m_length == 0) ? 0 : (i % this->m_length);
    }

    Node* last() const                                              // 尾结点的指针
    {
        return this->position(this->m_length-1)->next;
    }

    void last_to_first() const                                      // 将链表首尾相连
    {
        last()->next = this->m_header.next;                         // 尾结点指向首结点
    }

public:
/**********************************************************************
* Function:        insert()
* Description:      插入新结点(函数重载)
* Input:            i                   在位置i插入新结点(默认插在最后)
*                   e                   带插入结点的数据(value)
* Output:           
* Return:           bool                判断插入结点位置是否合法
* Others:           异常                申请内存是否成功
* Modify   Date     Version    Author      Modification
* ------------------------------------------------------------
*       2022/03/19    1.0                         Create        
**********************************************************************/
    bool insert(const T& e)
    {
        return insert(this->m_length, e);
    }

    bool insert(int i, const T& e)
    {
        bool ret = true;

        i = i % (this->m_length + 1);                               // 取余 

        ret = LinkList<T>::insert(i, e);                            // 调用父类的插入函数

        if( ret && (i == 0) )                                       // 处理特殊情况(首结点)
        {
            last_to_first();                                        // 将链表首尾相连
        }

        return ret;
    }

/**********************************************************************
* Function:        remove()
* Description:      删除结点
* Input:            i                   在位置i删除结点
* Output:           
* Return:           bool                判断删除结点位置是否合法
* Others:           
* Modify   Date     Version    Author      Modification
* ------------------------------------------------------------
*       2022/03/19    1.0                         Create        
****************************
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值