博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构C++(8)字典——链表实现(linkDictionary)
阅读量:6921 次
发布时间:2019-06-27

本文共 4056 字,大约阅读时间需要 13 分钟。

异常类 同  。

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

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

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

1 #pragma once 2  3 template
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
* find(const K &theKey) const = 0; //查找键为key的数对11 virtual void erase(const K &theKey) = 0;12 virtual void insert(const std::pair
& thePair) = 0;13 };

类 linkDictionary 的定义在 linkDictionary.h 中:

1 #pragma once  2 #include 
3 #include
4 #include "pairNode.h" 5 #include "dictionary.h" 6 #include "myExceptions.h" 7 8 9 template
10 class linkDictionary : public dictionary
11 { 12 public: 13 linkDictionary() { headerNode = nullptr; dictionarySize = 0; } 14 ~linkDictionary(); 15 bool empty() const; 16 int size() const; 17 std::pair
* find(const K &theKey) const; //查找键为key的数对 18 void erase(const K &theKey); 19 void insert(const std::pair
& thePair); 20 21 void output(std::ostream &out); 22 protected: 23 pairNode
*headerNode; 24 int dictionarySize; 25 }; 26 27 template
28 linkDictionary
::~linkDictionary() 29 { 30 pairNode
*Del = headerNode; 31 while (Del != nullptr) 32 { 33 headerNode = Del->next; 34 delete Del; 35 Del = headerNode; 36 } 37 } 38 39 template
40 bool linkDictionary
::empty() const 41 { 42 return dictionarySize == 0; 43 } 44 45 template
46 int linkDictionary
::size() const 47 { 48 return dictionarySize; 49 } 50 51 template
52 std::pair
* linkDictionary
::find(const K &theKey) const //查找键为key的数对 53 { 54 pairNode
*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
64 void linkDictionary
::erase(const K &theKey) 65 { 66 pairNode
*Tmp = nullptr; 67 pairNode
*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
90 void linkDictionary
::insert(const std::pair
& thePair) 91 { 92 pairNode
*Tmp = nullptr; 93 pairNode
*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
*newNode = new pairNode
(thePair, New);104 if (Tmp == nullptr) //要插入到首节点105 headerNode = newNode;106 else107 Tmp->next = newNode;108 }109 dictionarySize++;110 }111 112 template
113 void linkDictionary
::output(std::ostream &out)114 {115 out << "Key ->" << " value" << std::endl;116 pairNode
*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
125 std::ostream &operator<<(std::ostream &out, linkDictionary
&cLinkDictionary)126 {127 cLinkDictionary.output(out);128 return out;129 }

参考文献:

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

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

你可能感兴趣的文章
20172304 2017-2018《程序设计与数据结构》实验1报告
查看>>
windows双机调试
查看>>
J实现时间格式的转换(附加对象的转换)
查看>>
Marshal Code Into Another Thread(STAThread)
查看>>
V-MODEL指令实现方法
查看>>
webpack打包的原理的优势?
查看>>
java对于Redis中jedis的操作
查看>>
移动 H5 首屏秒开优化方案探讨
查看>>
redis的数据持久化方案
查看>>
2015年度精品 最新力作32位和64位xp,win7,win8,win10系统下载(电脑城专用版)
查看>>
hdu Wooden Sticks
查看>>
图像分类学习笔记
查看>>
Python学习笔记(二)
查看>>
语音信号处理之(一)动态时间规整(DTW)
查看>>
Python3 operator模块关联代替Python2 cmp() 函数
查看>>
温故知新系列(一)冒泡排序
查看>>
Web服务之LNMMP架构及动静分离实现
查看>>
java作业6
查看>>
从基础到分析,聊一聊企业报表平台的建设规划!
查看>>
java中的AIO
查看>>