异常类 同 数据结构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.