加入收藏 | 设为首页 | 会员中心 | 我要投稿 莱芜站长网 (https://www.0634zz.com/)- 云连接、建站、智能边缘云、设备管理、大数据!
当前位置: 首页 > 综合聚焦 > Linux > 正文

用Linux内核链表实现DTLib中的双向循环链表

发布时间:2023-02-20 10:28:44 所属栏目:Linux 来源:互联网
导读:本篇内容介绍了如何用Linux内核链表来实现DTLib中的双向循环链表的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! 继承关系如下图所示 如何用Linux内核
  本篇内容介绍了“如何用Linux内核链表来实现DTLib中的双向循环链表”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
 
  继承关系如下图所示
 
  如何用Linux内核链表来实现DTLib中的双向循环链表
 
  下来我们来讲讲它的设计思路:数据结点之间在逻辑上构成双向循环链表,头结点仅用于结点的定位。如下图所示
 
  如何用Linux内核链表来实现DTLib中的双向循环链表

  DualCircleList 源码
 
  #ifndef DUALCIRCLELIST_H
  #define DUALCIRCLELIST_H
   
  #include "DualLinkList.h"
  #include "LinuxList.h"
   
  namespace DTLib
  {
   
  template < typename T >
  class DualCircleList : public DualLinkList<T>
  {
  protected:
      struct Node : public Object
      {
          list_head head;
          T value;
      };
   
      list_head m_header;
      list_head* m_current;
   
      list_head* position(int i) const
      {
          list_head* ret = const_cast<list_head*>(&m_header);
   
          for(int p=0; p<i; p++)
          {
              ret = ret->next;
          }
   
          return ret;
      }
   
      int mod(int i) const
      {
          return (this->m_length == 0) ? 0 : (i % this->m_length);
      }
  public:
      DualCircleList()
      {
          this->m_length = 0;
          this->m_step = 1;
   
          m_current = NULL;
   
          INIT_LIST_HEAD(&m_header);
      }
   
      bool insert(const T& e)
      {
          return insert(this->m_length, e);
      }
   
      bool insert(int i, const T& e)
      {
          bool ret = true;
          Node* node = new Node();
   
          i = i % (this->m_length + 1);
   
          if(node != NULL)
          {
              node->value = e;
   
              list_add_tail(&node->head, position(i)->next);
   
              this->m_length++;
          }
          else
          {
              THROW_EXCEPTION(NoEnoughMemoryException, "No memory to insert new element ...");
          }
   
          return ret;
      }
   
      bool remove(int i)
      {
          bool ret = true;
   
          i = mod(i);
   
          ret = ((0 <= i) && (i < this->m_length));
   
          if( ret )
          {
              list_head* toDel = position(i)->next;
   
              if( m_current == toDel )
              {
                  m_current = toDel->next;
              }
   
              list_del(toDel);
   
              this->m_length--;
   
              delete list_entry(toDel, Node, head);
          }
   
          return ret;
      }
   
      bool set(int i, const T& e)
      {
          bool ret = true;
   
          i = mod(i);
   
          ret = ((0 <= i) && (i < this->m_length));
   
          if( ret )
          {
              list_entry(position(i)->next, Node, head)->value = e;
          }
   
          return ret;
      }
   
      T get(int i) const
      {
          T ret;
   
          if( get(i, ret) )
          {
              return ret;
          }
          else
          {
              THROW_EXCEPTION(indexoutofboundsexception, "Invaild parameter i to get element ...");
          }
      }
   
      bool get(int i, T& e) const
      {

(编辑:莱芜站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读