算法导论10.2-5 使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH

本文介绍了一种使用单向循环链表来实现基本字典操作的方法,包括插入(INSERT)、删除(DELETE)及查找(SEARCH)。通过定义链表节点结构,并实现关键算法,确保了链表作为数据结构的有效性和实用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH


[cpp]  view plain  copy
  1. #ifndef _SINGLY_CIRCULAR_LINKED_LIST_H  
  2. #define _SINGLY_CIRCULAR_LINKED_LIST_H  
  3.   
  4. /**************************************************************** 
  5. 10.2-5 使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH 
  6. *****************************************************************/  
  7.   
  8. template <class T>  
  9. class SinglyCircularLinkedList  
  10. {  
  11. public:  
  12.     // 一个表示链表结点的数据结构  
  13.     class Node{  
  14.     public:  
  15.         // 只有 StackUseSinglyLinkedList 才可以构造这个类型的对象  
  16.         // 访问这个类型的私有成员  
  17.         friend class SinglyCircularLinkedList < T > ;  
  18.         // 结点的值  
  19.         T value;  
  20.     private:  
  21.         // 默认构造函数,要求类型 T 也要有默认构造函数  
  22.         Node() :_next(nullptr){}  
  23.         // 将构造函数设置为私有的,防止在外部创建这个类型的对象  
  24.         Node(const T& e) :_next(nullptr), value(e){}  
  25.         // 指向下一个元素,如果这个值为空,  
  26.         // 表示本结点是链表中的最后一个元素  
  27.         Node* _next;  
  28.     };  
  29.   
  30.     SinglyCircularLinkedList();  
  31.     ~SinglyCircularLinkedList();  
  32.   
  33.     // 测试链表是否为空,空链表的头结点的 _next 指向自身  
  34.     bool empty() const { return _head->_next == _head; }  
  35.   
  36.     void insert(const T&);  
  37.   
  38.     void remove(const T&);  
  39.   
  40.     Node* search(const T&) const;  
  41.   
  42.     void print() const;  
  43. private:  
  44.     Node* _head;  
  45. };  
  46.   
  47. template <class T>  
  48. SinglyCircularLinkedList<T>::SinglyCircularLinkedList()  
  49. {  
  50.     // 为头结点分配空间,初始头结点的 _next 指向自身  
  51.     _head = new Node();  
  52.     _head->_next = _head;  
  53. }  
  54.   
  55. template <class T>  
  56. typename SinglyCircularLinkedList<T>::Node* SinglyCircularLinkedList<T>::search(const T& element) const{  
  57.     // 从链表头开始搜索再回到链表头  
  58.     auto node = _head->_next;  
  59.     while (node != _head){  
  60.         if (node->value == element) return node;  
  61.         node = node->_next;  
  62.     }  
  63.     return nullptr;  
  64. }  
  65.   
  66. template <class T>  
  67. void SinglyCircularLinkedList<T>::insert(const T& element){  
  68.     // 在链表的头部插入元素,新元素指向头结点的下一个元素,头结节指向新元素  
  69.     // 为新元素分配空间,当从元素从链表中删除时要释放这个元素占用的空间  
  70.     Node* node = new Node(element);  
  71.     node->_next = _head->_next;  
  72.     _head->_next = node;  
  73. }  
  74.   
  75. template <class T>  
  76. void SinglyCircularLinkedList<T>::remove(const T& element){  
  77.     // 必找到目标元素的前驱结点  
  78.     auto node = _head->_next;  
  79.     auto prevNode = _head;  
  80.     while (node != _head)  
  81.     {  
  82.         if (node->value == element){  
  83.             prevNode->_next = node->_next;  
  84.             delete node;  
  85.             break;  
  86.         }  
  87.         prevNode = node;  
  88.         node = node->_next;  
  89.     }  
  90. }  
  91.   
  92. template <class T>  
  93. void SinglyCircularLinkedList<T>::print() const{  
  94.     auto node = _head->_next;  
  95.     while (node != _head){  
  96.         std::cout << node->value << " ";  
  97.         node = node->_next;  
  98.     }  
  99.     std::cout << std::endl;  
  100. }  
  101.   
  102. template <class T>  
  103. SinglyCircularLinkedList<T>::~SinglyCircularLinkedList(){  
  104.     // 将链表中元素占用的空间释放  
  105.     Node* node = _head->_next;  
  106.     while (node != _head)  
  107.     {  
  108.         Node *n = node->_next;  
  109.         delete node;  
  110.         node = n;  
  111.     }  
  112.     // 释放头结点占用的空间  
  113.     delete _head;  
  114. };  
  115.   
  116. #endif  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值