Change
This commit is contained in:
@@ -0,0 +1 @@
|
||||
两个文件夹都是对“字典类”的实现,具体的跳表和散列在list文件夹的其他部分
|
||||
12
LinearList/Dictionary/sortedArrayList/dictionary.h
Normal file
12
LinearList/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
LinearList/Dictionary/sortedArrayList/main.cpp
Normal file
135
LinearList/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
LinearList/Dictionary/sortedArrayList/sortedArrayList.h
Normal file
107
LinearList/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>;
|
||||
160
LinearList/Queue/arrayQueue/arrayQueue.h
Normal file
160
LinearList/Queue/arrayQueue/arrayQueue.h
Normal file
@@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "queue.h"
|
||||
//As a Circle
|
||||
template <class T>
|
||||
class arrayQueue : public Queue<T> {
|
||||
private:
|
||||
T* element;
|
||||
int queueFront;
|
||||
int queueBack;
|
||||
int arrayLength;
|
||||
public:
|
||||
virtual ~arrayQueue() { delete[] element; }
|
||||
virtual bool empty() const override;
|
||||
virtual int size() const override;
|
||||
virtual T& front() override;
|
||||
virtual T& back() override;
|
||||
virtual void pop() override;
|
||||
virtual void push(const T& theElement) override;
|
||||
void showQueue() const;
|
||||
void showArray() const;
|
||||
|
||||
arrayQueue(int a = 10);
|
||||
arrayQueue(const arrayQueue& x);
|
||||
arrayQueue& operator=(const arrayQueue& x);
|
||||
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline bool arrayQueue<T>::empty() const
|
||||
{
|
||||
return arrayLength == 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline int arrayQueue<T>::size() const
|
||||
{
|
||||
return arrayLength;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T& arrayQueue<T>::front()
|
||||
{
|
||||
return element[queueFront];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T& arrayQueue<T>::back()
|
||||
{
|
||||
return element[queueBack];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void arrayQueue<T>::pop()
|
||||
{
|
||||
if (queueFront == queueBack) {
|
||||
std::cout << "Empty Queue" << std::endl;
|
||||
}
|
||||
queueFront = (queueFront + 1) % arrayLength;
|
||||
element[queueFront].~T();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void arrayQueue<T>::push(const T& theElement)
|
||||
{
|
||||
if ((queueBack + 1) % arrayLength == queueFront) {
|
||||
//Add Length: Remember it's a Circle
|
||||
T* tmp = new T[arrayLength * 2];
|
||||
int start = (queueFront + 1) % arrayLength;
|
||||
int cnt = 1;
|
||||
if (start < 2)
|
||||
{
|
||||
for (int i = 0; i < arrayLength; i++) {
|
||||
tmp[i] = element[i];
|
||||
}
|
||||
}
|
||||
//from queueFront to arrayLength, all copy to last;
|
||||
|
||||
else {
|
||||
for (int i = 0; i < arrayLength; i++) {
|
||||
tmp[i] = element[i];
|
||||
}
|
||||
for (int i = arrayLength * 2 - 1; i >= queueFront; i--) {
|
||||
tmp[i] = element[arrayLength - cnt];
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
queueFront = start + arrayLength - 1;
|
||||
queueBack = start - 2;
|
||||
arrayLength *= 2;
|
||||
delete[] element;
|
||||
element = tmp;
|
||||
}
|
||||
queueBack = (queueBack + 1) % arrayLength;
|
||||
element[queueBack] = theElement;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void arrayQueue<T>::showQueue() const
|
||||
{
|
||||
int tmp = queueFront;
|
||||
while (tmp != queueBack) {
|
||||
tmp = (tmp + 1) % arrayLength;
|
||||
element[tmp] < 0 ? std::cout << -1 << " " : std::cout << element[tmp] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void arrayQueue<T>::showArray() const
|
||||
{
|
||||
for (int i = 0; i < arrayLength; i++) {
|
||||
std::cout << std::left << std::setw(4) << i;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
for (int i = 0; i < arrayLength; i++) {
|
||||
element[i] < 0 ? std::cout << std::left << std::setw(4) << -1 : std::cout << std::left << std::setw(4) << element[i];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline arrayQueue<T>::arrayQueue(int a): arrayLength(a), queueBack(0), queueFront(0)
|
||||
{
|
||||
element = new T[a];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline arrayQueue<T>::arrayQueue(const arrayQueue& x): arrayLength(x.arrayLength), queueBack(x.queueBack), queueFront(x.queueFront)
|
||||
{
|
||||
element = new T[arrayLength];
|
||||
int tmp = queueFront;
|
||||
while (tmp != queueBack) {
|
||||
element[tmp] = x.element[tmp];
|
||||
tmp = (tmp + 1) % arrayLength;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline arrayQueue<T>& arrayQueue<T>::operator=(const arrayQueue& x)
|
||||
{
|
||||
if (this != &x) {
|
||||
delete[] element;
|
||||
arrayLength = x.arrayLength;
|
||||
queueBack = x.queueBack;
|
||||
queueFront = x.queueFront;
|
||||
element = new T[arrayLength];
|
||||
int tmp = queueFront;
|
||||
while (tmp != queueBack) {
|
||||
element[tmp] = x.element[tmp];
|
||||
tmp = (tmp + 1) % arrayLength;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Can't Copy Itself" << std::endl;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
67
LinearList/Queue/arrayQueue/main.cpp
Normal file
67
LinearList/Queue/arrayQueue/main.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "arrayQueue.h"
|
||||
using namespace std;
|
||||
int main() {
|
||||
arrayQueue<int> a(10);
|
||||
cout << "If a is empty? : " << a.empty() << endl;
|
||||
a.push(1); a.push(2); a.push(3); a.push(4); a.push(5);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.pop();
|
||||
a.pop();
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.push(6); a.push(7); a.push(8); a.push(9); a.push(10);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.push(11);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.push(12);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.push(13); a.push(14); a.push(15);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.push(16); a.push(17); a.push(18); a.push(19); a.push(20);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.push(21);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
a.push(22);
|
||||
a.showArray();
|
||||
a.showQueue();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
This is the result, Perfectly match
|
||||
If a is empty? : 0
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
-1 1 2 3 4 5 -1 -1 -1 -1
|
||||
1 2 3 4 5
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
-1 1 2 3 4 5 -1 -1 -1 -1
|
||||
3 4 5
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
10 1 2 3 4 5 6 7 8 9
|
||||
3 4 5 6 7 8 9 10
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
10 11 2 3 4 5 6 7 8 9
|
||||
3 4 5 6 7 8 9 10 11
|
||||
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
||||
10 11 12 0 0 1 40 0 161 -1 10 11 2 3 4 5 6 7 8 9
|
||||
3 4 5 6 7 8 9 10 11 12
|
||||
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
||||
10 11 12 13 14 15 40 0 161 -1 10 11 2 3 4 5 6 7 8 9
|
||||
3 4 5 6 7 8 9 10 11 12 13 14 15
|
||||
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
||||
10 11 12 13 14 15 16 17 18 19 20 11 2 3 4 5 6 7 8 9
|
||||
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
||||
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
||||
10 11 12 13 14 15 16 17 18 19 20 21 2 3 4 5 6 7 8 9
|
||||
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
||||
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
||||
10 11 12 13 14 15 16 17 18 19 20 21 22 0 0 1 80 0 408 -1 10 11 12 13 14 15 16 17 18 19 20 21 2 3 4 5 6 7 8 9
|
||||
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
||||
*/
|
||||
13
LinearList/Queue/arrayQueue/queue.h
Normal file
13
LinearList/Queue/arrayQueue/queue.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
//Abstract Class
|
||||
template <class T>
|
||||
class Queue {
|
||||
public:
|
||||
virtual ~Queue() = default;
|
||||
virtual bool empty() const = 0;
|
||||
virtual int size() const = 0;
|
||||
virtual T& front() = 0;
|
||||
virtual T& back() = 0;
|
||||
virtual void pop() = 0;
|
||||
virtual void push(const T& theElement) = 0;
|
||||
};
|
||||
0
LinearList/Queue/chainQueue/p211
Normal file
0
LinearList/Queue/chainQueue/p211
Normal file
0
LinearList/STL-List/main.cpp
Normal file
0
LinearList/STL-List/main.cpp
Normal file
7
LinearList/Stack/appliance/StarRail.cpp
Normal file
7
LinearList/Stack/appliance/StarRail.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
int main(){
|
||||
|
||||
return 0;
|
||||
}
|
||||
35
LinearList/Stack/appliance/braket-match.cpp
Normal file
35
LinearList/Stack/appliance/braket-match.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
|
||||
void isMatch(const string& s){
|
||||
stack<int> t;
|
||||
//bool error = false;
|
||||
for(int i = 0; i < s.length(); i++){
|
||||
if(s[i] == '('){
|
||||
t.push(i);
|
||||
}
|
||||
if(s[i] == ')'){
|
||||
try{
|
||||
if(t.empty()) throw std::runtime_error("Error: Braket \" ) \" No Match");
|
||||
t.pop();
|
||||
}
|
||||
catch(const runtime_error& e){
|
||||
cout << e.what() << endl;
|
||||
//error = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(t.empty()) cout << "Match" << endl;
|
||||
else cout << "Error: Braket \" ( \" No Match" << endl;
|
||||
}
|
||||
|
||||
int main(){
|
||||
string s;
|
||||
cin >> s;
|
||||
isMatch(s);
|
||||
return 0;
|
||||
}
|
||||
BIN
LinearList/Stack/appliance/braket-match.exe
Normal file
BIN
LinearList/Stack/appliance/braket-match.exe
Normal file
Binary file not shown.
92
LinearList/Stack/appliance/hanoi.cpp
Normal file
92
LinearList/Stack/appliance/hanoi.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
|
||||
void hanoi(int n, int a, int b, int c){
|
||||
if(n == 1){
|
||||
cout << "Move 1 from stack " << a << " to " << b << "(n == 1)" << endl;
|
||||
}
|
||||
if(n > 1){
|
||||
hanoi(n - 1, a, b, c);
|
||||
cout << "Move " << n << " from stack " << a << " to " << c << endl;
|
||||
hanoi(n - 1, b, c, a);
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
hanoi(1, 1, 2, 3);
|
||||
cout << endl;
|
||||
hanoi(2, 1, 2, 3);
|
||||
cout << endl;
|
||||
hanoi(3, 1, 2, 3);
|
||||
cout << endl;
|
||||
hanoi(4, 1, 2, 3);
|
||||
cout << endl;
|
||||
hanoi(5, 1, 2, 3);
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 2 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 2 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 3 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 2 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 2 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 3 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 2 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 4 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 2 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 3 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 2 from stack 3 to 2
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 2 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 3 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 2 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 4 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 2 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 3 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 2 from stack 3 to 2
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 5 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
Move 2 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 3 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 2 from stack 3 to 2
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 4 from stack 2 to 1
|
||||
Move 1 from stack 3 to 1(n == 1)
|
||||
Move 2 from stack 3 to 2
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 3 from stack 3 to 2
|
||||
Move 1 from stack 1 to 2(n == 1)
|
||||
Move 2 from stack 1 to 3
|
||||
Move 1 from stack 2 to 3(n == 1)
|
||||
*/
|
||||
BIN
LinearList/Stack/appliance/hanoi.exe
Normal file
BIN
LinearList/Stack/appliance/hanoi.exe
Normal file
Binary file not shown.
98
LinearList/Stack/arrayStack/arraystack.h
Normal file
98
LinearList/Stack/arrayStack/arraystack.h
Normal file
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include "stack.h"
|
||||
|
||||
template <class T>
|
||||
class arrayStack : public Stack<T> {
|
||||
private:
|
||||
T* stack;
|
||||
int stackTop;
|
||||
int length;
|
||||
public:
|
||||
arrayStack(int initialCapacity = 10);
|
||||
virtual ~arrayStack() { delete[] stack; }
|
||||
|
||||
virtual bool empty() const override { return stackTop == -1; }
|
||||
virtual int size() const override { return stackTop + 1; }
|
||||
virtual T& top() override;
|
||||
virtual void pop() override;
|
||||
virtual void push(const T& theElement) override;
|
||||
virtual void show() const;
|
||||
arrayStack& operator=(const arrayStack& x);
|
||||
arrayStack(const arrayStack& x);
|
||||
|
||||
};
|
||||
|
||||
template <class T>
|
||||
arrayStack<T>::arrayStack(int initialCapacity) {
|
||||
length = initialCapacity;
|
||||
if(length < 0) {
|
||||
throw std::invalid_argument("Initial Error: Length < 0");
|
||||
length = 1;
|
||||
}
|
||||
stack = new T[length];
|
||||
stackTop = -1;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
T& arrayStack<T>::top() {
|
||||
if (stackTop == -1) throw std::invalid_argument("Top Error: No Element in Stack");
|
||||
return stack[stackTop];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void arrayStack<T>::pop() {
|
||||
if (stackTop == -1) throw std::invalid_argument("Pop Error: Empty Stack");
|
||||
if constexpr (!std::is_trivially_destructible<T>::value) {
|
||||
stack[stackTop].~T(); // ֻ<><D6BB><EFBFBD><EFBFBD> T <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>
|
||||
}
|
||||
stackTop--;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void arrayStack<T>::push(const T& theElement) {
|
||||
if(stackTop == length - 1){
|
||||
T* tmp = new T[length * 2];
|
||||
for(int i = 0; i < length; i++){
|
||||
tmp[i] = stack[i];
|
||||
}
|
||||
delete[] stack;
|
||||
stack = tmp;
|
||||
length *= 2;
|
||||
}
|
||||
stackTop++;
|
||||
stack[stackTop] = theElement;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
arrayStack<T>::arrayStack(const arrayStack& x) {
|
||||
length = x.length;
|
||||
stackTop = x.stackTop;
|
||||
stack = new T[length];
|
||||
for (int i = 0; i <= stackTop; ++i) {
|
||||
stack[i] = x.stack[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
arrayStack<T>& arrayStack<T>::operator=(const arrayStack<T>& x) {
|
||||
if (this != &x) {
|
||||
delete[] stack;
|
||||
length = x.length;
|
||||
stackTop = x.stackTop;
|
||||
stack = new T[length];
|
||||
for (int i = 0; i <= stackTop; ++i) {
|
||||
stack[i] = x.stack[i];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void arrayStack<T>::show() const {
|
||||
for (int i = 0; i <= stackTop; i++) {
|
||||
std::cout << stack[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
43
LinearList/Stack/arrayStack/main.cpp
Normal file
43
LinearList/Stack/arrayStack/main.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include "stack.h"
|
||||
#include "arraystack.h"
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
TO TEST:
|
||||
private:
|
||||
T* stack;
|
||||
int stackTop;
|
||||
int length;
|
||||
public:
|
||||
arrayStack(int initialCapacity = 10);
|
||||
virtual ~arrayStack() { delete[] stack; }
|
||||
|
||||
virtual bool empty() const override { return stackTop == -1; }
|
||||
virtual int size() const override { return stackTop + 1; }
|
||||
virtual T& top() override;
|
||||
virtual void pop() override;
|
||||
virtual void push(const T& theElement) override;
|
||||
arrayStack& operator=(const arrayStack& x);
|
||||
arrayStack(const arrayStack& x);
|
||||
*/
|
||||
int main(){
|
||||
arrayStack<char> s(10);
|
||||
s.push('a');
|
||||
s.show();
|
||||
s.push('b');
|
||||
s.show();
|
||||
s.push('c');
|
||||
s.show();
|
||||
s.pop();
|
||||
s.show();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
a
|
||||
a b
|
||||
a b c
|
||||
a b
|
||||
*/
|
||||
13
LinearList/Stack/arrayStack/stack.h
Normal file
13
LinearList/Stack/arrayStack/stack.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
template <class T>
|
||||
class Stack {
|
||||
public:
|
||||
virtual ~Stack(){}
|
||||
virtual bool empty() const = 0;
|
||||
virtual int size() const = 0;
|
||||
virtual T& top() = 0;
|
||||
virtual void pop() = 0;
|
||||
virtual void push(const T& theElement) = 0;
|
||||
virtual void show() const = 0;
|
||||
};
|
||||
127
LinearList/Stack/chainStack/chainStack.h
Normal file
127
LinearList/Stack/chainStack/chainStack.h
Normal file
@@ -0,0 +1,127 @@
|
||||
#ifndef CHAIN_STACKH
|
||||
#define CHAIN_STACKH
|
||||
#include <iostream>
|
||||
#include "stack.h"
|
||||
#include "node.h"
|
||||
|
||||
template <class T>
|
||||
class chainStack : public Stack<T>{
|
||||
private:
|
||||
Node<T>* first;
|
||||
Node<T>* last;
|
||||
int stackTop;
|
||||
public:
|
||||
chainStack() = delete;
|
||||
chainStack(Node<T>* f = nullptr);
|
||||
chainStack(const chainStack<T>& x);
|
||||
virtual bool empty() const;
|
||||
virtual int size() const;
|
||||
virtual T& top();
|
||||
virtual void pop();
|
||||
virtual void push(const T& theElement);
|
||||
virtual void show() const;
|
||||
~chainStack();
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline chainStack<T>::chainStack(Node<T>* f) :first(f), stackTop(0), last(nullptr) {
|
||||
Node<T>* p = first;
|
||||
while (p) {
|
||||
++stackTop;
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline chainStack<T>::chainStack(const chainStack<T>& x)
|
||||
{
|
||||
Node<T>* run = x.first;
|
||||
Node<T>* tnext = nullptr;
|
||||
Node<T>* tprev = nullptr;
|
||||
while (run) {
|
||||
Node<T>* tmp = new Node<T>(run->element, tprev, tnext);
|
||||
if(tprev) tprev->next = tmp;
|
||||
if (!tprev) first = tmp;
|
||||
tprev = tmp;
|
||||
run = run->next;
|
||||
}
|
||||
last = tprev;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline bool chainStack<T>::empty() const
|
||||
{
|
||||
return (stackTop == 0);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline int chainStack<T>::size() const
|
||||
{
|
||||
return stackTop;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T& chainStack<T>::top()
|
||||
{
|
||||
return last->element;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void chainStack<T>::pop()
|
||||
{
|
||||
if (stackTop == 0)
|
||||
throw std::invalid_argument("Pop Error: No Element in Stack");
|
||||
if (last) {
|
||||
Node<T>* tmp = last;
|
||||
last = last->prev;
|
||||
if (last) {
|
||||
last->next = nullptr;
|
||||
}
|
||||
else {
|
||||
first = nullptr;
|
||||
}
|
||||
delete tmp;
|
||||
stackTop--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void chainStack<T>::push(const T& theElement) {
|
||||
Node<T>* tmp = new Node<T>(theElement, last, nullptr);
|
||||
if (last) last->next = tmp;
|
||||
else first = tmp;
|
||||
last = tmp;
|
||||
stackTop++;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void chainStack<T>::show() const {
|
||||
Node<T>* p = first;
|
||||
while (p) {
|
||||
std::cout << p->element << " ";
|
||||
p = p->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline chainStack<T>::~chainStack()
|
||||
{
|
||||
Node<T>* p = first;
|
||||
Node<T>* that;
|
||||
while (p) {
|
||||
that = p;
|
||||
p = that->next;
|
||||
delete that;
|
||||
}
|
||||
first = nullptr;
|
||||
last = nullptr;
|
||||
stackTop = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // !CHAIN_STACKH
|
||||
42
LinearList/Stack/chainStack/main.cpp
Normal file
42
LinearList/Stack/chainStack/main.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include "node.h"
|
||||
#include "stack.h"
|
||||
#include "chainStack.h"
|
||||
using namespace std;
|
||||
/*
|
||||
TO TEST:
|
||||
private:
|
||||
Node<T>* first;
|
||||
Node<T>* last;
|
||||
int stackTop;
|
||||
public:
|
||||
chainStack() = delete;
|
||||
chainStack(Node<T>* f = nullptr);
|
||||
chainStack(const chainStack<T>& x);
|
||||
virtual bool empty() const;
|
||||
virtual int size() const;
|
||||
virtual T& top();
|
||||
virtual void pop();
|
||||
virtual void push(const T& theElement);
|
||||
virtual void show() const;
|
||||
~chainStack()
|
||||
*/
|
||||
|
||||
int main() {
|
||||
chainStack<char> s(nullptr);
|
||||
s.push('a');
|
||||
s.show();
|
||||
s.push('b');
|
||||
s.show();
|
||||
s.push('z');
|
||||
s.show();
|
||||
s.pop();
|
||||
s.show();
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
a
|
||||
a b
|
||||
a b z
|
||||
a b
|
||||
*/
|
||||
24
LinearList/Stack/chainStack/node.h
Normal file
24
LinearList/Stack/chainStack/node.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
template <class T>
|
||||
class Node {
|
||||
public:
|
||||
T element;
|
||||
Node<T>* next;
|
||||
Node<T>* prev;
|
||||
|
||||
public:
|
||||
Node(); // Ĭ<>Ϲ<EFBFBD><CFB9><EFBFBD>
|
||||
Node(const T& e, Node<T>* n = nullptr, Node<T>* p = nullptr); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Node(const Node<T>& x); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>죨dz<ECA3A8><C7B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
~Node() = default;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
Node<T>::Node() : element(), next(nullptr) , prev(nullptr){}
|
||||
|
||||
template<class T>
|
||||
Node<T>::Node(const T& e, Node<T>* n, Node<T>* p) : element(e), next(p), prev(n){}
|
||||
|
||||
template<class T>
|
||||
Node<T>::Node(const Node<T>& x) : element(x.element), next(nullptr), prev(nullptr){}
|
||||
13
LinearList/Stack/chainStack/stack.h
Normal file
13
LinearList/Stack/chainStack/stack.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
template <class T>
|
||||
class Stack {
|
||||
public:
|
||||
virtual ~Stack() = default;
|
||||
virtual bool empty() const = 0;
|
||||
virtual int size() const = 0;
|
||||
virtual T& top() = 0;
|
||||
virtual void pop() = 0;
|
||||
virtual void push(const T& theElement) = 0;
|
||||
virtual void show() const = 0;
|
||||
};
|
||||
14
LinearList/skipList/main.cpp
Normal file
14
LinearList/skipList/main.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include "skipList.h"
|
||||
|
||||
using namespace std;
|
||||
int main() {
|
||||
skipList<int, string> a(100, 2, 0.5);
|
||||
pair<int, string>* p = new pair<int, string>[50];
|
||||
p[0] = pair<int, string>(1, "Test");
|
||||
p[1] = pair<int, string>(2, "For");
|
||||
a.insert(p[0]);
|
||||
a.insert(p[1]);
|
||||
a.showLv0List();
|
||||
return 0;
|
||||
}
|
||||
143
LinearList/skipList/skipList.h
Normal file
143
LinearList/skipList/skipList.h
Normal file
@@ -0,0 +1,143 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "skipNode.h"
|
||||
|
||||
template <class K, class E>
|
||||
class skipList {
|
||||
private:
|
||||
float cutOff; //ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int levels; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ķǿ<C4B7><C7BF><EFBFBD><EFBFBD><EFBFBD>
|
||||
int dSize; //<2F>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD>
|
||||
int maxLevel; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
K tailKey; //<2F><><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD>
|
||||
skipNode<K, E>* headerNode; //ͷ<><CDB7><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
||||
skipNode<K, E>* tailNode; //β<>ڵ<EFBFBD>ָ<EFBFBD><D6B8>
|
||||
skipNode<K, E>** last; //last[i] <20><>ʾi<CABE><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>
|
||||
|
||||
public:
|
||||
skipList(K largeKey, int maxPairs, float prob); //<2F><><EFBFBD><EFBFBD>
|
||||
std::pair<K, E>* find(const K& theKey) const; //<2F><>Key<65><79><EFBFBD><EFBFBD>
|
||||
int level() const; //<2F>ּ<EFBFBD>
|
||||
skipNode<K, E>* search(const K& theKey); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
|
||||
void insert(const std::pair<K, E>& thePair); //<2F><><EFBFBD><EFBFBD>
|
||||
void erase(const K& theKey); //<2F><><EFBFBD><EFBFBD>
|
||||
void showWholeList() const; //<2F><>ʾȫ<CABE><C8AB><EFBFBD>ڵ<EFBFBD>
|
||||
void showLv0List() const; //<2F><>ʾ<EFBFBD><CABE><EFBFBD>ײ<EFBFBD><D7B2>ڵ㣨ȫ<E3A3A8><C8AB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
|
||||
};
|
||||
|
||||
template<class K, class E>
|
||||
inline skipList<K, E>::skipList(K largeKey, int maxPairs, float prob)
|
||||
{
|
||||
cutOff = prob * RAND_MAX;
|
||||
maxLevel = (int)ceil(logf((float)maxPairs)) / logf(1 / prob) - 1;
|
||||
levels = 0;
|
||||
dSize = 0;
|
||||
tailKey = largeKey;
|
||||
|
||||
std::pair<K, E> tailPair;
|
||||
tailPair.first = tailKey;
|
||||
headerNode = new skipNode<K, E>(tailPair, maxLevel + 1);
|
||||
tailNode = new skipNode<K, E>(tailPair, 0);
|
||||
last = new skipNode<K, E>* [maxLevel + 1];
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, headerNode = tailNode
|
||||
for (int i = 0; i <= maxLevel; i++) {
|
||||
headerNode->next[i] = tailNode;
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class E>
|
||||
inline std::pair<K, E>* skipList<K, E>::find(const K& theKey) const
|
||||
{
|
||||
if (theKey >= tailKey) return nullptr;
|
||||
skipNode<K, E>* beforeNode = headerNode;
|
||||
for (int i = levels; i >= 0; i--) {
|
||||
while (beforeNode->next[i]->element.first < theKey) {
|
||||
beforeNode = beforeNode->next[i];
|
||||
}
|
||||
}
|
||||
if (beforeNode->next[0]->element.first == theKey) {
|
||||
return &beforeNode->next[0]->element;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<class K, class E>
|
||||
inline int skipList<K, E>::level() const
|
||||
{
|
||||
int lev = 0;
|
||||
while (rand() <= cutOff) lev++;
|
||||
return (lev <= maxLevel) ? lev : maxLevel;
|
||||
}
|
||||
|
||||
template<class K, class E>
|
||||
inline skipNode<K, E>* skipList<K, E>::search(const K& theKey)
|
||||
{
|
||||
skipNode<K, E>* beforeNode = headerNode;
|
||||
for (int i = levels; i >= 0; i++) {
|
||||
while (beforeNode->next[i]->element.first < theKey) {
|
||||
beforeNode = beforeNode->next[i];
|
||||
}
|
||||
last[i] = beforeNode;
|
||||
}
|
||||
return beforeNode->next[0];
|
||||
}
|
||||
|
||||
template<class K, class E>
|
||||
inline void skipList<K, E>::insert(const std::pair<K, E>& thePair)
|
||||
{
|
||||
if (thePair.first >= tailKey) {
|
||||
std::cout << "Key = " << thePair.first << " Must be < " << tailKey;
|
||||
}
|
||||
skipNode<K, E>* theNode = search(thePair.first);
|
||||
if (theNode->element.first == thePair.first) {
|
||||
theNode->element.second = thePair.second;
|
||||
return;
|
||||
}
|
||||
|
||||
int theLevel = level();
|
||||
|
||||
if (theLevel > levels) {
|
||||
theLevel = ++levels;
|
||||
last[theLevel] = headerNode;
|
||||
}
|
||||
|
||||
skipNode<K, E>* newNode = new skipNode<K, E>(thePair, theLevel + 1);
|
||||
for (int i = 0; i <= theLevel; i++) {
|
||||
newNode->next[i] = last[i]->next[i];
|
||||
last[i]->next[i] = newNode;
|
||||
}
|
||||
|
||||
dSize++;
|
||||
return;
|
||||
}
|
||||
|
||||
template<class K, class E>
|
||||
inline void skipList<K, E>::erase(const K& theKey)
|
||||
{
|
||||
if (theKey >= tailKey) return;
|
||||
|
||||
skipNode<K, E>* theNode = search(theKey);
|
||||
if (theNode->element.first != theKey) return;
|
||||
|
||||
for (int i = 0; i <= levels && last[i]->next[i] == theNode; i++) {
|
||||
last[i]->next[i] = theNode->next[i];
|
||||
|
||||
}
|
||||
|
||||
while (levels > 0 && headerNode->next[levels] == tailNode) levels--;
|
||||
|
||||
delete theNode;
|
||||
dSize--;
|
||||
}
|
||||
|
||||
template<class K, class E>
|
||||
inline void skipList<K, E>::showLv0List() const
|
||||
{
|
||||
skipNode<K, E>* tmp = headerNode->next[0];
|
||||
while (tmp != tailNode) {
|
||||
std::cout << "{" << tmp->element.first << ", " << tmp->element.second << "}" << std::endl;
|
||||
tmp = tmp->next[0];
|
||||
}
|
||||
}
|
||||
0
LinearList/skipList/skipList跳表
Normal file
0
LinearList/skipList/skipList跳表
Normal file
13
LinearList/skipList/skipNode.h
Normal file
13
LinearList/skipList/skipNode.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include <utility>
|
||||
template <class K, class E>
|
||||
class skipNode {
|
||||
public:
|
||||
std::pair<K, E> element;
|
||||
skipNode<K, E>** next; //指针数组 -> 高速公路
|
||||
//指针数组记录 该节点 在每一层的 下一个节点。
|
||||
public:
|
||||
skipNode(const std::pair<K, E>& thePair, int size) : element(thePair) {
|
||||
next = new skipNode<K, E>* [size];
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user