一、前言
- 字典介绍参阅:https://blog.csdn.net/qq_41453285/article/details/103448760
- 本文代码下载:
- 方式1:公众号【多栖技术控小董】回复【3579】免费获取下载链接。
- 方式2:CSDN下载链接https://download.csdn.net/download/qq_41453285/12019815
- 方式3:Github下载链接https://github.com/dongyusheng/Interview-algorithm/tree/master/c%2B%2BAlgorithms(进入之后下载里面的sortedChain.zip文件)
二、代码实现
- 我们的字典采用键值升序的方式来对字典进行排序。插入字典时,如果键已经存在,则更新值;如果键不存在,则插入键
头文件
#include <iostream> #include <string> #include <utility> using std::cout; using std::cin; using std::endl; using std::string; using std::pair;
链表节点定义
template<class K,class E> struct pairNode { typedef pair<const K, E> pairType; pairType element; pairNode<K, E>* next; pairNode(const pairType& thePair):element(thePair){} pairNode(const pairType& thePair, pairNode<K, E>* theNext) :element(thePair) { next = theNext; } };
字典抽象类定义
template<class K,class E> class dictionary { public: virtual ~dictionary() = default; virtual bool empty()const = 0; //判断字典是否为空 virtual int size()const = 0; //返回字典中数对的数目 virtual std::pair<const K, E>* find(const K&)const = 0; //返回匹配数对的指针 virtual void erase(const K&) = 0; //删除匹配的数对 virtual void insert(const std::pair<const K, E>&) = 0; //往字典中插入一个数对 };
字典类定义
template<class K,class E> class sortedChain :public dictionary<K, E> { public: sortedChain(); ~sortedChain(); bool empty()const override; int size()const override; std::pair<const K, E>* find(const K& theKey)const override; void erase(const K& theKey) override; void insert(const std::pair<const K, E>& thePair) override; private: pairNode<K, E>* firstNode; int dSize; }; template<class K, class E> sortedChain<K, E>::sortedChain() { this->firstNode = nullptr; this->dSize = 0; } template<class K, class E> sortedChain<K, E>::~sortedChain() { pairNode<K, E>* tempNode; while (this->firstNode) { tempNode = this->firstNode->next; delete this->firstNode; this->firstNode = tempNode; } } template<class K, class E> bool sortedChain<K, E>::empty()const { return this->dSize == 0; } template<class K, class E> int sortedChain<K, E>::size()const { return this->dSize; } template<class K, class E> std::pair<const K, E>* sortedChain<K, E>::find(const K& theKey)const { pairNode<K, E>* tempNode = this->firstNode; //遍历字典 while ((tempNode != nullptr) && (tempNode->element.first != theKey)) tempNode = tempNode->next; //如果找到了就返回 if ((tempNode != nullptr) && (tempNode->element.first == theKey)) return &tempNode->element; //没有找到就返回空 return nullptr; } template<class K, class E> void sortedChain<K, E>::erase(const K& theKey) { pairNode<K, E>* eraseNode = this->firstNode; pairNode<K, E>* beforeEraseNode = nullptr; //遍历字典 while ((eraseNode != nullptr) && (eraseNode->element.first < theKey)) { beforeEraseNode = eraseNode; eraseNode = eraseNode->next; } //找到了就删除 if ((eraseNode != nullptr) && (eraseNode->element.first == theKey)) { //如果删除的是头结点 if (eraseNode == this->firstNode){ this->firstNode = eraseNode->next; } else{ beforeEraseNode->next = eraseNode->next; } delete eraseNode; this->dSize--; } } template<class K, class E> void sortedChain<K, E>::insert(const std::pair<const K, E>& thePair) { pairNode<K, E>* afterInsertNode = this->firstNode; pairNode<K, E>* beforeInsertNode = nullptr; //遍历字典 while ((afterInsertNode != nullptr) && (afterInsertNode->element.first < thePair.first)) { beforeInsertNode = afterInsertNode; afterInsertNode = afterInsertNode->next; } //如果键已经存在,更新值 if ((afterInsertNode != nullptr) && (afterInsertNode->element.first == thePair.first)) { afterInsertNode->element.second = thePair.second; return; } //如果键不存在,插入新节点 pairNode<K, E>* insertNode = new pairNode<K,E>(thePair, afterInsertNode); if (beforeInsertNode == nullptr) this->firstNode = insertNode; else beforeInsertNode->next = insertNode; this->dSize++; }
主函数
int main() { sortedChain<int, char> *mySortedChain = new sortedChain<int, char>(); mySortedChain->insert(std::pair<int, char>(1,'a')); mySortedChain->insert(std::pair<int, char>(2, 'b')); mySortedChain->insert(std::pair<int, char>(3, 'c')); std::cout << "Current size " << mySortedChain->size() << std::endl; mySortedChain->erase(1); std::cout << "Current size " << mySortedChain->size() << std::endl; std::pair<const int, char> *findPair; findPair = mySortedChain->find(2); std::cout << "Find success,key is " << findPair->first << ",value is " << findPair->second << std::endl; return 0; }