From eccb13de21ec0649ac1d2456e6303d6ca16c2450 Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Sun, 27 Jul 2025 16:22:50 +0800 Subject: [PATCH] Dictionary --- ...Implementation of the Dictionary Interface | 1 + List/Dictionary/sortedArrayList/dictionary.h | 12 ++ List/Dictionary/sortedArrayList/main.cpp | 135 ++++++++++++++++++ .../sortedArrayList/sortedArrayList.h | 107 ++++++++++++++ List/skipNode/skipNode跳表 | 0 5 files changed, 255 insertions(+) create mode 100644 List/Dictionary/Implementation of the Dictionary Interface create mode 100644 List/Dictionary/sortedArrayList/dictionary.h create mode 100644 List/Dictionary/sortedArrayList/main.cpp create mode 100644 List/Dictionary/sortedArrayList/sortedArrayList.h create mode 100644 List/skipNode/skipNode跳表 diff --git a/List/Dictionary/Implementation of the Dictionary Interface b/List/Dictionary/Implementation of the Dictionary Interface new file mode 100644 index 0000000..0d31a0a --- /dev/null +++ b/List/Dictionary/Implementation of the Dictionary Interface @@ -0,0 +1 @@ +两个文件夹都是对“字典类”的实现,具体的跳表和散列在list文件夹的其他部分 \ No newline at end of file diff --git a/List/Dictionary/sortedArrayList/dictionary.h b/List/Dictionary/sortedArrayList/dictionary.h new file mode 100644 index 0000000..7554b92 --- /dev/null +++ b/List/Dictionary/sortedArrayList/dictionary.h @@ -0,0 +1,12 @@ +#pragma once +#include +template +class Dictionary { +public: + virtual ~Dictionary() = default; + virtual bool empty() const = 0; + virtual int size() const = 0; + virtual std::pair* find(const K&) const = 0; + virtual void erase(const K&) = 0; + virtual void insert(const std::pair&) = 0; +}; \ No newline at end of file diff --git a/List/Dictionary/sortedArrayList/main.cpp b/List/Dictionary/sortedArrayList/main.cpp new file mode 100644 index 0000000..ca94a65 --- /dev/null +++ b/List/Dictionary/sortedArrayList/main.cpp @@ -0,0 +1,135 @@ +#include "sortedArrayList.h" +using namespace std; + +/* +private: + std::pair* 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* find(K& x) const override; + virtual void erase(const K& x) override; + virtual void insert(const std::pair& x) override; +*/ + +int main() { + sortedArrayList a; + pair p[50]; + p[0] = pair("Zhang", 19); + p[1] = pair("Wang", 20); + p[2] = pair("Li", 21); + p[3] = pair("Zhao", 190); + p[4] = pair("Qian", 191); + p[5] = pair("Sun", 192); + p[6] = pair("Li", 193); + p[7] = pair("Zhou", 194); + p[8] = pair("Wu", 195); + p[9] = pair("Zheng", 196); + p[10] = pair("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 } +*/ \ No newline at end of file diff --git a/List/Dictionary/sortedArrayList/sortedArrayList.h b/List/Dictionary/sortedArrayList/sortedArrayList.h new file mode 100644 index 0000000..5e4bb7d --- /dev/null +++ b/List/Dictionary/sortedArrayList/sortedArrayList.h @@ -0,0 +1,107 @@ +#pragma once +#include +#include "dictionary.h" + +template +class sortedArrayList : public Dictionary { +private: + std::pair* element; + int arrayLength; + int currentSize; +public: + ~sortedArrayList() { delete[] element; } + sortedArrayList(int a = 10); + sortedArrayList(const sortedArrayList& x); + + sortedArrayList& operator=(const sortedArrayList& x); + + //ע⣺ʹԪʱﲻдconst sortedArrayList& xԭд֮ͱˣʧ + 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* find(const K& x) const override; + virtual void erase(const K& x) override; + virtual void insert(const std::pair& x) override; +}; + +template +inline sortedArrayList::sortedArrayList(int a) : arrayLength(a), currentSize(0) +{ + element = new std::pair[a]; +} + +template +inline sortedArrayList::sortedArrayList(const sortedArrayList& x) : arrayLength(x.arrayLength), currentSize(x.currentSize) +{ + element = new std::pair[arrayLength]; + for (int i = 0; i < currentSize; i++) { + element[i] = x.element[i]; + } +} + +template +inline sortedArrayList& sortedArrayList::operator=(const sortedArrayList& x) +{ + if (this != &x) { + delete[] element; + arrayLength = x.arrayLength; + currentSize = x.currentSize; + + element = new std::pair[arrayLength]; + for (int i = 0; i < currentSize; i++) { + element[i] = x.element[i]; + } + } + return *this; +} + +template +inline std::pair* sortedArrayList::find(const K& x) const +{ + for (int i = 0; i < currentSize; i++) { + if (element[i].first == x) return &element[i]; + } + return nullptr; +} + +template +inline void sortedArrayList::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(); + break; + } + } +} + +template +inline void sortedArrayList::insert(const std::pair& x) +{ + if (currentSize == arrayLength) { + //Add Length + arrayLength *= 2; + std::pair* tmp = new std::pair[arrayLength]; + for (int i = 0; i < currentSize; i++) { + tmp[i] = element[i]; + } + delete[] element; + element = tmp; + } + element[currentSize] = x; + currentSize++; +} + + +template class sortedArrayList; \ No newline at end of file diff --git a/List/skipNode/skipNode跳表 b/List/skipNode/skipNode跳表 new file mode 100644 index 0000000..e69de29