在這之前,先說一個概念:字典。
字典(dictionary)是由一些形如(key,value)的數對所組成的集合,其中key是關鍵字,value是與關鍵字key對應的值(另一種說法是,value是值,這個值所對應的關鍵字就是key)。任意兩個數對,其關鍵字都不等。有關字典的一些基本操作如下:
- 確定字典是否爲空
- 確定字典有多少數對
- 尋找一個指定了關鍵字的數對
- 插入一個數對
- 刪除一個指定了關鍵字的數對
一個字典是數對的集合,每個數對都由一個詞和它的值組成。一個詞的值包括詞的意思、發音、詞源等。
比如字典中的單詞date,在字典中的內容就是date,the point if time at which a transation or event takes place,其中,date就是關鍵字。
一個多重字典和上述的字典類似,不同的是,多重字典中,兩個或多個數對可以具有相同的關鍵字。
下面是字典的具體實現:
#include<iostream> #include"sortedchain.h" using namespace std; int main(void) { sortedChain<int,int> z; pair<int,int> p; p.first = 2;p.second = 10; z.insert(p); p.first = 10;p.second = 50; z.insert(p); p.first = 6;p.second = 30; z.insert(p); p.first = 8;p.second = 40; z.insert(p); p.first = 1;p.second = 5; z.insert(p); p.first = 12;p.second = 60; z.insert(p); cout<<"The chain is" << z <<endl; cout <<"Its size is"<<z.size() <<endl; cout<<"Element associated with 1 is "<<z.find(1)->second<<endl; cout<<"Element associated with 6 is "<<z.find(6)->second<<endl; cout<<"Element associated with 12 is "<<z.find(12)->second<<endl; z.erase(1); z.erase(2); z.erase(6); z.erase(12); cout<<"Deleted 1,2,6,12"<<endl; cout<<"The chain is "<<z<<endl; cout<<"Its size is "<<z.size()<<endl; return 0; }
/* * 數值對節點的定義 * pairNode.h */ #ifndef PAIRNODE_H #define PAIRNODE_H #include<iostream> using namespace std; 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;} }; #endif // PAIRNODE_H
/* * 字典的抽象類,定義了字典操作需要的基本函數 * 其中,K代表關鍵字,E代表值 * dictionary.h * */ #ifndef DICTIONARY_H #define DICTIONARY_H #include"pairnode.h" #include"iostream" using namespace std; template<class K,class E>//K代表關鍵字,E代表值 class dictionary { public: virtual ~dictionary(){} virtual bool empty() const = 0;//如果字典爲空,返回true virtual int size() const = 0;//返回字典中數值對的數量 //返回某關鍵字所對應的值 virtual pair<const K,E>* find(const K&) const = 0; //刪除某關鍵字對應的數值對 virtual void erase(const K&) = 0; //將某對數值對插入字典中 virtual void insert(const pair<const K,E>&) = 0; }; #endif // DICTIONARY_H
/* * */ #ifndef SORTEDCHAIN_H #define SORTEDCHAIN_H #include<iostream> #include"dictionary.h" #include"pairnode.h" using namespace std; template<class K,class E> class sortedChain : public dictionary<K,E> { public: sortedChain() {firstNode = NULL;dSize = 0;} ~sortedChain(); bool empty() const{return dSize == 0;}//有序鏈表是否爲空 int size() const{return dSize;}//返回有序鏈表元素個數 pair<const K,E>* find(const K&) const;//查找K值對應的某個元素對 void erase(const K&);//刪除K值對應的某個元素對 void insert(const pair<const K,E>&);//插入元素對 void output(ostream& out) const;//輸出鏈表元素 protected: pairNode<K,E>* firstNode;//指向鏈表中的第一個節點 int dSize;//字典中元素的個數,一個元素等於一個數對 }; template<class K,class E> sortedChain<K,E>::~sortedChain()//析構函數,刪除所有節點 { while(firstNode != NULL)//順次刪除所有節點 { pairNode<K,E>* nextNode = firstNode->next; delete firstNode; firstNode = nextNode; } } template<class K,class E> pair<const K,E>* sortedChain<K,E>::find(const K& theKey) const//查找theKey對應的數對 { pairNode<K,E>* currentNode = firstNode; while(currentNode != NULL && currentNode->element.first != theKey) currentNode = currentNode->next; if(currentNode != NULL && currentNode->element.first == theKey) return ¤tNode->element; return NULL; } template<class K,class E> void sortedChain<K,E>::insert(const pair<const K, E>& thePair) { pairNode<K,E> *p = firstNode, *tp = NULL; while (p != NULL && p->element.first < thePair.first) { tp = p; p = p->next; } if(p != NULL && p->element.first == thePair.first) { p->element.second = thePair.second; return; } pairNode<K,E> *newNode = new pairNode<K,E>(thePair,p); if(tp == NULL) firstNode = newNode; else tp->next = newNode; dSize++; return; } template<class K,class E> void sortedChain<K,E>::erase(const K& theKey) { pairNode<K,E> *p = firstNode, *tp = NULL; while(p != NULL && p->element.first < theKey) { tp = p; p = p->next; } if(p != NULL && p->element.first == theKey) { if(tp == NULL) firstNode = p->next; else tp->next = p->next; delete p; dSize--; } } template<class K,class E> void sortedChain<K,E>::output(ostream &out) const { for(pairNode<K,E>* currentNode = firstNode; currentNode != NULL; currentNode = currentNode->next) out <<currentNode->element.first<<" " <<currentNode->element.second<<" "; } template<class K,class E> ostream& operator<<(ostream& out,const sortedChain<K,E>& x) { x.output(out); return out; } #endif // SORTEDCHAIN_H
(adsbygoogle = window.adsbygoogle || []).push({}); function googleAdJSAtOnload() { var element = document.createElement("script"); element.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"; element.async = true; document.body.appendChild(element); } if (window.addEventListener) { window.addEventListener("load", googleAdJSAtOnload, false); } else if (window.attachEvent) { window.attachEvent("onload", googleAdJSAtOnload); } else { window.onload = googleAdJSAtOnload; }