數據結構:字典

一、定義:字典(dictionary)是一些元素的結合。每一個元素有一個稱做key的域,不一樣元素的key各不相同。node

其抽象數據類型描述爲:ios

抽象數據類型Dictionaryide

{this

實例:spa

  具備不一樣關鍵字的元素組合code

操做:blog

  Create():建立一個空字典get

  Search(k,x):搜索關鍵字爲k的元素input

  Insert(x):插入元素it

  Delete(k,x):刪除關鍵字爲k的元素

}

有重複元素的字典(dictionary with duplicates)與上面定義的字典類似,只是它容許多個元素有相同的關鍵字。在有重複元素的字典中,在進行搜索和刪除時須要一個規則來消除歧義。也就是說,若是要搜索關鍵字爲k的元素,那麼在全部關鍵字爲k值得元素中應該返回哪個。

字典能夠保存在線性序列(e1,e2,e3...)中,既能夠用公式化描述的方式,也能夠用鏈表的方式實現。

2.代碼實現:

節點:

 1 template<typename E,typename K>
 2 class SortedChainNode
 3 {
 4     friend ostream& operator<< <>(ostream&, const SortedChain<E, K>&);
 5     friend class SortedChain < E, K > ;
 6 public:
 7     SortedChainNode():data(0){ link = NULL; }
 8     SortedChainNode(const K& k,const E& x) :data(x),key(k){ link = NULL; }
 9     SortedChainNode<E,K>* getLink()//獲取下一個Node
10     {
11         if (link)
12         {
13             return link;
14         }
15         return NULL;
16     }
17 private:
18     E data;
19     K key;
20     SortedChainNode<E, K>* link;
21 };
View Code

SortedChain:

  1 template<typename E,typename K>
  2 class SortedChain
  3 {
  4 
  5     friend ostream& operator<< <>(ostream&, const SortedChain<E, K>&);
  6 public:
  7     SortedChain(){ first = NULL; }
  8     SortedChain(const K& k,const E& e)
  9     {
 10         first = new SortedChainNode<E, K>(k, e);
 11     }
 12 
 13     SortedChainNode<E,K>& GetFirst()
 14     {
 15         return *first;
 16     }
 17     virtual ~SortedChain();
 18     bool IsEmpty()const{ return first == NULL; }
 19     int Length() const;
 20     bool Search(const K& k, E& e)const;
 21     SortedChain<E, K>& Delete(const K& k, E& e);
 22     SortedChain<E, K>& Insert(const K& k, const E& e);//容許有重複key
 23     SortedChain<E, K>& DistinctInsert(const K& k,const E& e);//不容許重複
 24 private:
 25     SortedChainNode<E, K>* first;
 26 };
 27 
 28 template<typename E,typename K>
 29 SortedChain<E,K>::~SortedChain()
 30 {
 31     SortedChainNode<E, K>* p = first;
 32     while (first!=NULL)
 33     {
 34         p = first->link;
 35         delete first;
 36         first = p;
 37     }
 38 }
 39 
 40 template<typename E,typename K>
 41 bool SortedChain<E,K>::Search(const K& k, E& e)const
 42 {
 43     SortedChainNode<E, K>* p=first;
 44 
 45     while (p != NULL&&p->key<k)
 46     {
 47         p = p->link;
 48     }
 49 
 50     if (p!=NULL&&p->key==k)
 51     {
 52         e = p->data;
 53         return true;
 54     }
 55 
 56     return false;
 57 }
 58 
 59 template<typename E,typename K>
 60 SortedChain<E,K>& SortedChain<E,K>::Delete(const K& k, E& e)
 61 {
 62     SortedChainNode<E, K>* p = first;
 63     SortedChainNode<E, K>* tp = NULL;
 64     while (p != NULL&&p->key < k)
 65     {
 66         tp = p;
 67         p = p->link;
 68     }
 69 
 70     if (p != NULL&&p->key == k)
 71     {
 72         e = p->data;
 73         if (tp)
 74         {
 75             tp->link = p->link;
 76         }
 77         else
 78             first = p->link;//p是first節點
 79 
 80         delete p;
 81 
 82         return *this;
 83     }
 84 
 85     throw exception("Bad input");//不存在相匹配的元素
 86 }
 87 
 88 template<typename E,typename K>
 89 SortedChain<E,K>& SortedChain<E,K>::Insert(const K& k,const E& e)
 90 {
 91     SortedChainNode<E, K>* p = first;
 92     SortedChainNode<E, K>* tp = NULL;
 93     while (p!=NULL&&p->key<k)
 94     {
 95         tp = p;
 96         p = p->link;
 97     }
 98     
 99     SortedChainNode<E, K>* node = new SortedChainNode<E, K>(k, e);
100     if (tp)
101     {
102         tp->link = node;
103         node->link = p;
104     }
105     else
106     {
107         node->link = first;
108         first = node;
109 
110     }
111 
112     return *this;
113 }
114 
115 template<typename E, typename K>
116 SortedChain<E, K>& SortedChain<E, K>::DistinctInsert(const K& k, const E& e)
117 {
118     SortedChainNode<E, K>* p = first;
119     SortedChainNode<E, K>* tp = NULL;
120     while (p != NULL&&p->key < k)
121     {
122         tp = p;
123         p = p->link;
124     }
125 
126     if (p!=NULL&&p->key==k)
127     {
128         p->data = e;
129         return *this;
130     }
131 
132     SortedChainNode<E, K>* node = new SortedChainNode<E, K>(k, e);
133     if (tp)
134     {
135         tp->link = node;
136         node->link = p;
137     }
138     else
139     {
140         node->link = first;
141         first = node;
142     }
143 
144     return *this;
145 }
146 
147 
148 template<typename E,typename K>
149 ostream& operator<<(ostream& output,const SortedChain<E,K>& s)
150 {
151     SortedChainNode<E, K>* p = s.first;
152     while (p!=NULL)
153     {
154         output << "key: " << p->key << ' ' << "data: " << p->data << endl;
155         p = p->getLink();
156     }
157     return output;
158 }
View Code

完整:

  1 #ifndef SORTEDCHAIN_H
  2 #define SORTEDCHAIN_H
  3 
  4 #include<iostream>
  5 #include<exception>
  6 using namespace std;
  7 template<typename E,typename K>
  8 class SortedChain;
  9 
 10 
 11 template<typename E,typename K>
 12 class SortedChainNode
 13 {
 14     friend ostream& operator<< <>(ostream&, const SortedChain<E, K>&);
 15     friend class SortedChain < E, K > ;
 16 public:
 17     SortedChainNode():data(0){ link = NULL; }
 18     SortedChainNode(const K& k,const E& x) :data(x),key(k){ link = NULL; }
 19     SortedChainNode<E,K>* getLink()//獲取下一個Node
 20     {
 21         if (link)
 22         {
 23             return link;
 24         }
 25         return NULL;
 26     }
 27 private:
 28     E data;
 29     K key;
 30     SortedChainNode<E, K>* link;
 31 };
 32 
 33 
 34 template<typename E,typename K>
 35 class SortedChain
 36 {
 37 
 38     friend ostream& operator<< <>(ostream&, const SortedChain<E, K>&);
 39 public:
 40     SortedChain(){ first = NULL; }
 41     SortedChain(const K& k,const E& e)
 42     {
 43         first = new SortedChainNode<E, K>(k, e);
 44     }
 45 
 46     SortedChainNode<E,K>& GetFirst()
 47     {
 48         return *first;
 49     }
 50     virtual ~SortedChain();
 51     bool IsEmpty()const{ return first == NULL; }
 52     int Length() const;
 53     bool Search(const K& k, E& e)const;
 54     SortedChain<E, K>& Delete(const K& k, E& e);
 55     SortedChain<E, K>& Insert(const K& k, const E& e);//容許有重複key
 56     SortedChain<E, K>& DistinctInsert(const K& k,const E& e);//不容許重複
 57 private:
 58     SortedChainNode<E, K>* first;
 59 };
 60 
 61 template<typename E,typename K>
 62 SortedChain<E,K>::~SortedChain()
 63 {
 64     SortedChainNode<E, K>* p = first;
 65     while (first!=NULL)
 66     {
 67         p = first->link;
 68         delete first;
 69         first = p;
 70     }
 71 }
 72 
 73 template<typename E,typename K>
 74 bool SortedChain<E,K>::Search(const K& k, E& e)const
 75 {
 76     SortedChainNode<E, K>* p=first;
 77 
 78     while (p != NULL&&p->key<k)
 79     {
 80         p = p->link;
 81     }
 82 
 83     if (p!=NULL&&p->key==k)
 84     {
 85         e = p->data;
 86         return true;
 87     }
 88 
 89     return false;
 90 }
 91 
 92 template<typename E,typename K>
 93 SortedChain<E,K>& SortedChain<E,K>::Delete(const K& k, E& e)
 94 {
 95     SortedChainNode<E, K>* p = first;
 96     SortedChainNode<E, K>* tp = NULL;
 97     while (p != NULL&&p->key < k)
 98     {
 99         tp = p;
100         p = p->link;
101     }
102 
103     if (p != NULL&&p->key == k)
104     {
105         e = p->data;
106         if (tp)
107         {
108             tp->link = p->link;
109         }
110         else
111             first = p->link;//p是first節點
112 
113         delete p;
114 
115         return *this;
116     }
117 
118     throw exception("Bad input");//不存在相匹配的元素
119 }
120 
121 template<typename E,typename K>
122 SortedChain<E,K>& SortedChain<E,K>::Insert(const K& k,const E& e)
123 {
124     SortedChainNode<E, K>* p = first;
125     SortedChainNode<E, K>* tp = NULL;
126     while (p!=NULL&&p->key<k)
127     {
128         tp = p;
129         p = p->link;
130     }
131     
132     SortedChainNode<E, K>* node = new SortedChainNode<E, K>(k, e);
133     if (tp)
134     {
135         tp->link = node;
136         node->link = p;
137     }
138     else
139     {
140         node->link = first;
141         first = node;
142 
143     }
144 
145     return *this;
146 }
147 
148 template<typename E, typename K>
149 SortedChain<E, K>& SortedChain<E, K>::DistinctInsert(const K& k, const E& e)
150 {
151     SortedChainNode<E, K>* p = first;
152     SortedChainNode<E, K>* tp = NULL;
153     while (p != NULL&&p->key < k)
154     {
155         tp = p;
156         p = p->link;
157     }
158 
159     if (p!=NULL&&p->key==k)
160     {
161         p->data = e;
162         return *this;
163     }
164 
165     SortedChainNode<E, K>* node = new SortedChainNode<E, K>(k, e);
166     if (tp)
167     {
168         tp->link = node;
169         node->link = p;
170     }
171     else
172     {
173         node->link = first;
174         first = node;
175     }
176 
177     return *this;
178 }
179 
180 
181 template<typename E,typename K>
182 ostream& operator<<(ostream& output,const SortedChain<E,K>& s)
183 {
184     SortedChainNode<E, K>* p = s.first;
185     while (p!=NULL)
186     {
187         output << "key: " << p->key << ' ' << "data: " << p->data << endl;
188         p = p->getLink();
189     }
190     return output;
191 }
192 #endif
View Code
相關文章
相關標籤/搜索