数据结构C++(8)字典——链表实现(linkDictionary)

异常类 同 数据结构C++(1)线性表——数组实现(arrayList) 。

节点类型 pairNode 定义在 pairNode.h 中:

 1 #pragma once
 2 
 3 
 4 template<typename K, typename V>
 5 struct pairNode
 6 {
 7     std::pair<const K, V> element;
 8     pairNode<K, V> *next;
 9 
10     pairNode(const std::pair<const K, V>& thePair) :element(thePair) {}        //element 委托构造函数
11     pairNode(const std::pair<const K, V>& thePair, pairNode<K, V>* theNext) :element(thePair)
12     {
13         next = theNext;
14     }
15 };

字典的抽象基类 dictionary 定义在 dictionary.h 中:

 1 #pragma once
 2 
 3 template<typename K, typename V>
 4 class dictionary
 5 {
 6 public:
 7     virtual ~dictionary() {};
 8     virtual bool empty() const = 0;
 9     virtual int size() const = 0;
10     virtual std::pair<const K, V>* find(const K &theKey) const = 0;    //查找键为key的数对
11     virtual void erase(const K &theKey) = 0;
12     virtual void insert(const std::pair<const K, V>& thePair) = 0;
13 };

类 linkDictionary 的定义在 linkDictionary.h 中:

  1 #pragma once
  2 #include <iostream>
  3 #include <sstream>
  4 #include "pairNode.h"
  5 #include "dictionary.h"
  6 #include "myExceptions.h"
  7 
  8 
  9 template<typename K, typename V>
 10 class linkDictionary : public dictionary<K, V>
 11 {
 12 public:
 13     linkDictionary() { headerNode = nullptr; dictionarySize = 0; }
 14     ~linkDictionary();
 15     bool empty() const;
 16     int size() const;
 17     std::pair<const K, V>* find(const K &theKey) const;    //查找键为key的数对
 18     void erase(const K &theKey);
 19     void insert(const std::pair<const K, V>& thePair);
 20 
 21     void output(std::ostream &out);
 22 protected:
 23     pairNode<K, V> *headerNode;
 24     int dictionarySize;
 25 };
 26 
 27 template<typename K, typename V>
 28 linkDictionary<K, V>::~linkDictionary()
 29 {
 30     pairNode<K, V> *Del = headerNode;
 31     while (Del != nullptr)
 32     {
 33         headerNode = Del->next;
 34         delete Del;
 35         Del = headerNode;
 36     }
 37 }
 38 
 39 template<typename K, typename V>
 40 bool linkDictionary<K, V>::empty() const
 41 {
 42     return dictionarySize == 0;
 43 }
 44 
 45 template<typename K, typename V>
 46 int linkDictionary<K, V>::size() const
 47 {
 48     return dictionarySize;
 49 }
 50 
 51 template<typename K, typename V>
 52 std::pair<const K, V>* linkDictionary<K, V>::find(const K &theKey) const    //查找键为key的数对
 53 {
 54     pairNode<K, V> *Tmp = headerNode;
 55     while ((Tmp != nullptr) && (Tmp->element.first != theKey))
 56         Tmp = Tmp->next;
 57     if ((Tmp != nullptr) && (Tmp->element.first == theKey))
 58         return &Tmp->element;
 59 
 60     return nullptr;
 61 }
 62 
 63 template<typename K, typename V>
 64 void linkDictionary<K, V>::erase(const K &theKey)
 65 {
 66     pairNode<K, V> *Tmp = nullptr; 
 67     pairNode<K, V> *Del = headerNode;
 68     while ((Del != nullptr) && (Del->element.first < theKey))
 69     {
 70         Tmp = Del;
 71         Del = Del->next;
 72     }
 73 
 74     if ((Del == nullptr) || (Del->element.first != theKey))        //要删除的数对不存在
 75     {
 76         std::ostringstream s;
 77         s << "theKey = " << theKey << "inexistence";
 78         throw keyInexistence(s.str());
 79     }
 80     if (Tmp == nullptr)                //要删除的数对是首节点
 81         headerNode = Del->next;
 82     else
 83         Tmp->next = Del->next;
 84 
 85     delete Del;
 86     dictionarySize--;
 87 }
 88 
 89 template<typename K, typename V>
 90 void linkDictionary<K, V>::insert(const std::pair<const K, V>& thePair)
 91 {
 92     pairNode<K, V> *Tmp = nullptr;
 93     pairNode<K, V> *New = headerNode;
 94     while ((New != nullptr) && (New->element.first < thePair.first))
 95     {
 96         Tmp = New;
 97         New = New->next;
 98     }
 99     if ((New != nullptr) && (New->element.first == thePair.first))        //键为key的数对存在,修改值
100         New->element.second = thePair.second;
101     else    //不存在,创建新节点,插入
102     {
103         pairNode<K, V> *newNode = new pairNode<K, V>(thePair, New);
104         if (Tmp == nullptr)            //要插入到首节点
105             headerNode = newNode;
106         else
107             Tmp->next = newNode;
108     }
109     dictionarySize++;
110 }
111 
112 template<typename K, typename V>
113 void linkDictionary<K, V>::output(std::ostream &out)
114 {
115     out << "Key  ->" << "  value" << std::endl;
116     pairNode<K, V> *Tmp = headerNode;
117     while (Tmp != nullptr)
118     {
119         out << Tmp->element.first << "      " << Tmp->element.second << std::endl;
120         Tmp = Tmp->next;
121     }
122 }
123 
124 template<typename K, typename V>
125 std::ostream &operator<<(std::ostream &out, linkDictionary<K, V> &cLinkDictionary)
126 {
127     cLinkDictionary.output(out);
128     return out;
129 }

参考文献:

[1].Sartaj Sahni. 数据结构、算法与应用[M]. 机械工业出版社, 2000.

转载于:https://www.cnblogs.com/peformer/p/8040541.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值