Dictionary
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
两个文件夹都是对“字典类”的实现,具体的跳表和散列在list文件夹的其他部分
|
12
List/Dictionary/sortedArrayList/dictionary.h
Normal file
12
List/Dictionary/sortedArrayList/dictionary.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <utility>
|
||||||
|
template <class K, class E>
|
||||||
|
class Dictionary {
|
||||||
|
public:
|
||||||
|
virtual ~Dictionary() = default;
|
||||||
|
virtual bool empty() const = 0;
|
||||||
|
virtual int size() const = 0;
|
||||||
|
virtual std::pair<K, E>* find(const K&) const = 0;
|
||||||
|
virtual void erase(const K&) = 0;
|
||||||
|
virtual void insert(const std::pair<K, E>&) = 0;
|
||||||
|
};
|
135
List/Dictionary/sortedArrayList/main.cpp
Normal file
135
List/Dictionary/sortedArrayList/main.cpp
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
#include "sortedArrayList.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/*
|
||||||
|
private:
|
||||||
|
std::pair<K, E>* element;
|
||||||
|
int arrayLength;
|
||||||
|
int currentSize;
|
||||||
|
public:
|
||||||
|
~sortedArrayList() { delete[] element; }
|
||||||
|
sortedArrayList(int a = 10);
|
||||||
|
sortedArrayList(const sortedArrayList& x);
|
||||||
|
|
||||||
|
sortedArrayList& operator=(const sortedArrayList& x);
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const sortedArrayList& x);
|
||||||
|
|
||||||
|
virtual bool empty() const override { return arrayLength == 0; }
|
||||||
|
virtual int size() const override { return arrayLength; }
|
||||||
|
virtual std::pair<const K, E>* find(K& x) const override;
|
||||||
|
virtual void erase(const K& x) override;
|
||||||
|
virtual void insert(const std::pair<K, E>& x) override;
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
sortedArrayList<string, int> a;
|
||||||
|
pair<string, int> p[50];
|
||||||
|
p[0] = pair<string, int>("Zhang", 19);
|
||||||
|
p[1] = pair<string, int>("Wang", 20);
|
||||||
|
p[2] = pair<string, int>("Li", 21);
|
||||||
|
p[3] = pair<string, int>("Zhao", 190);
|
||||||
|
p[4] = pair<string, int>("Qian", 191);
|
||||||
|
p[5] = pair<string, int>("Sun", 192);
|
||||||
|
p[6] = pair<string, int>("Li", 193);
|
||||||
|
p[7] = pair<string, int>("Zhou", 194);
|
||||||
|
p[8] = pair<string, int>("Wu", 195);
|
||||||
|
p[9] = pair<string, int>("Zheng", 196);
|
||||||
|
p[10] = pair<string, int>("Feng", 197);
|
||||||
|
for (int i = 0; i < 11; i++) {
|
||||||
|
a.insert(p[i]);
|
||||||
|
cout << a << endl;
|
||||||
|
}
|
||||||
|
a.erase("Wang");
|
||||||
|
cout << a << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
4: { Qian, 191 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
4: { Qian, 191 }
|
||||||
|
5: { Sun, 192 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
4: { Qian, 191 }
|
||||||
|
5: { Sun, 192 }
|
||||||
|
6: { Li, 193 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
4: { Qian, 191 }
|
||||||
|
5: { Sun, 192 }
|
||||||
|
6: { Li, 193 }
|
||||||
|
7: { Zhou, 194 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
4: { Qian, 191 }
|
||||||
|
5: { Sun, 192 }
|
||||||
|
6: { Li, 193 }
|
||||||
|
7: { Zhou, 194 }
|
||||||
|
8: { Wu, 195 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
4: { Qian, 191 }
|
||||||
|
5: { Sun, 192 }
|
||||||
|
6: { Li, 193 }
|
||||||
|
7: { Zhou, 194 }
|
||||||
|
8: { Wu, 195 }
|
||||||
|
9: { Zheng, 196 }
|
||||||
|
|
||||||
|
0: { Zhang, 19 }
|
||||||
|
1: { Wang, 20 }
|
||||||
|
2: { Li, 21 }
|
||||||
|
3: { Zhao, 190 }
|
||||||
|
4: { Qian, 191 }
|
||||||
|
5: { Sun, 192 }
|
||||||
|
6: { Li, 193 }
|
||||||
|
7: { Zhou, 194 }
|
||||||
|
8: { Wu, 195 }
|
||||||
|
9: { Zheng, 196 }
|
||||||
|
10: { Feng, 197 }
|
||||||
|
|
||||||
|
0: { Wang, 20 }
|
||||||
|
1: { Li, 21 }
|
||||||
|
2: { Zhao, 190 }
|
||||||
|
3: { Qian, 191 }
|
||||||
|
4: { Sun, 192 }
|
||||||
|
5: { Li, 193 }
|
||||||
|
6: { Zhou, 194 }
|
||||||
|
7: { Wu, 195 }
|
||||||
|
8: { Zheng, 196 }
|
||||||
|
9: { Feng, 197 }
|
||||||
|
*/
|
107
List/Dictionary/sortedArrayList/sortedArrayList.h
Normal file
107
List/Dictionary/sortedArrayList/sortedArrayList.h
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include "dictionary.h"
|
||||||
|
|
||||||
|
template <class K, class E>
|
||||||
|
class sortedArrayList : public Dictionary<K, E> {
|
||||||
|
private:
|
||||||
|
std::pair<K, E>* element;
|
||||||
|
int arrayLength;
|
||||||
|
int currentSize;
|
||||||
|
public:
|
||||||
|
~sortedArrayList() { delete[] element; }
|
||||||
|
sortedArrayList(int a = 10);
|
||||||
|
sortedArrayList(const sortedArrayList& x);
|
||||||
|
|
||||||
|
sortedArrayList& operator=(const sortedArrayList<K, E>& x);
|
||||||
|
|
||||||
|
//ע<>⣺<EFBFBD><E2A3BA>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ﲻ<EFBFBD><EFB2BB>дconst sortedArrayList&<K ,E> x<><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><CDB1>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const sortedArrayList& x) {
|
||||||
|
for (int i = 0; i < x.currentSize; i++) {
|
||||||
|
os << i << ": { " << x.element[i].first << ", " << x.element[i].second << " }" << std::endl;
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool empty() const override { return currentSize == 0; }
|
||||||
|
virtual int size() const override { return currentSize; }
|
||||||
|
virtual std::pair<K, E>* find(const K& x) const override;
|
||||||
|
virtual void erase(const K& x) override;
|
||||||
|
virtual void insert(const std::pair<K, E>& x) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class K, class E>
|
||||||
|
inline sortedArrayList<K, E>::sortedArrayList(int a) : arrayLength(a), currentSize(0)
|
||||||
|
{
|
||||||
|
element = new std::pair<K, E>[a];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class K, class E>
|
||||||
|
inline sortedArrayList<K, E>::sortedArrayList(const sortedArrayList<K, E>& x) : arrayLength(x.arrayLength), currentSize(x.currentSize)
|
||||||
|
{
|
||||||
|
element = new std::pair<K, E>[arrayLength];
|
||||||
|
for (int i = 0; i < currentSize; i++) {
|
||||||
|
element[i] = x.element[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class K, class E>
|
||||||
|
inline sortedArrayList<K, E>& sortedArrayList<K, E>::operator=(const sortedArrayList<K, E>& x)
|
||||||
|
{
|
||||||
|
if (this != &x) {
|
||||||
|
delete[] element;
|
||||||
|
arrayLength = x.arrayLength;
|
||||||
|
currentSize = x.currentSize;
|
||||||
|
|
||||||
|
element = new std::pair<K, E>[arrayLength];
|
||||||
|
for (int i = 0; i < currentSize; i++) {
|
||||||
|
element[i] = x.element[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class K, class E>
|
||||||
|
inline std::pair<K, E>* sortedArrayList<K, E>::find(const K& x) const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < currentSize; i++) {
|
||||||
|
if (element[i].first == x) return &element[i];
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class K, class E>
|
||||||
|
inline void sortedArrayList<K, E>::erase(const K& x)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < currentSize; i++) {
|
||||||
|
if (element[i].first == x) {
|
||||||
|
//Move Forward
|
||||||
|
for (int j = i; j < currentSize; j++) {
|
||||||
|
element[j - 1] = element[j];
|
||||||
|
}
|
||||||
|
currentSize--;
|
||||||
|
element[currentSize] = std::pair<K, E>();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class K, class E>
|
||||||
|
inline void sortedArrayList<K, E>::insert(const std::pair<K, E>& x)
|
||||||
|
{
|
||||||
|
if (currentSize == arrayLength) {
|
||||||
|
//Add Length
|
||||||
|
arrayLength *= 2;
|
||||||
|
std::pair<K, E>* tmp = new std::pair<K, E>[arrayLength];
|
||||||
|
for (int i = 0; i < currentSize; i++) {
|
||||||
|
tmp[i] = element[i];
|
||||||
|
}
|
||||||
|
delete[] element;
|
||||||
|
element = tmp;
|
||||||
|
}
|
||||||
|
element[currentSize] = x;
|
||||||
|
currentSize++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template class sortedArrayList<std::string, int>;
|
0
List/skipNode/skipNode跳表
Normal file
0
List/skipNode/skipNode跳表
Normal file
Reference in New Issue
Block a user