Skip to content

Commit 448d8e3

Browse files
committed
Add stack.h, queue.h
1 parent b47eacd commit 448d8e3

File tree

13 files changed

+439
-5
lines changed

13 files changed

+439
-5
lines changed

.vscode/settings.json

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,57 @@
1414
"string_view": "cpp",
1515
"unordered_map": "cpp",
1616
"utility": "cpp",
17-
"algorithm": "cpp"
17+
"algorithm": "cpp",
18+
"__bit_reference": "cpp",
19+
"__config": "cpp",
20+
"__debug": "cpp",
21+
"__errc": "cpp",
22+
"__functional_base": "cpp",
23+
"__locale": "cpp",
24+
"__mutex_base": "cpp",
25+
"__node_handle": "cpp",
26+
"__nullptr": "cpp",
27+
"__string": "cpp",
28+
"__threading_support": "cpp",
29+
"__tuple": "cpp",
30+
"atomic": "cpp",
31+
"bit": "cpp",
32+
"cctype": "cpp",
33+
"chrono": "cpp",
34+
"clocale": "cpp",
35+
"complex": "cpp",
36+
"cstdarg": "cpp",
37+
"cstddef": "cpp",
38+
"cstdint": "cpp",
39+
"cstdio": "cpp",
40+
"cstdlib": "cpp",
41+
"cstring": "cpp",
42+
"ctime": "cpp",
43+
"cwchar": "cpp",
44+
"cwctype": "cpp",
45+
"exception": "cpp",
46+
"functional": "cpp",
47+
"ios": "cpp",
48+
"iosfwd": "cpp",
49+
"iostream": "cpp",
50+
"istream": "cpp",
51+
"limits": "cpp",
52+
"locale": "cpp",
53+
"mutex": "cpp",
54+
"new": "cpp",
55+
"numbers": "cpp",
56+
"numeric": "cpp",
57+
"optional": "cpp",
58+
"ostream": "cpp",
59+
"random": "cpp",
60+
"ratio": "cpp",
61+
"sstream": "cpp",
62+
"stdexcept": "cpp",
63+
"streambuf": "cpp",
64+
"system_error": "cpp",
65+
"tuple": "cpp",
66+
"type_traits": "cpp",
67+
"typeinfo": "cpp"
1868
},
1969
"files.exclude": {
2070
"**/.dSYM": true

II Sorting and Order Statistics/randomized_select.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,29 @@ T& randomized_select(T* const A, unsigned p, unsigned r, unsigned i) {
1515
return randomized_select(A, q + 1, r, i - k);
1616
}
1717

18+
template <typename T>
19+
T& randomized_select_mate(T* const A, unsigned p, unsigned r, unsigned i) {
20+
while (p != r) {
21+
auto q = randomized_partition(A, p, r);
22+
auto k = q - p + 1;
23+
if (i == k)
24+
return A[q];
25+
else if (i < k)
26+
r = q - 1;
27+
else {
28+
p = q + 1;
29+
i -= k;
30+
}
31+
}
32+
return A[p];
33+
}
34+
1835
template <typename T>
1936
T& randomized_select(T* const A, unsigned N, unsigned i) {
2037
return randomized_select(A, 0, N - 1, i);
2138
}
2239

2340
template <typename T, unsigned N>
2441
T& randomized_select(T (&A)[N], unsigned i) {
25-
return randomized_select(A, N, i);
42+
return randomized_select_mate(A, 0, N - 1, i);
2643
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#include "insertion_sort.h"

II Sorting and Order Statistics/test_randomized_select.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ int main() {
1919
}
2020
cout << endl;
2121

22-
cout << randomized_select(dn.get(), num, 3) << endl;
23-
cout << randomized_select(n, 3) << endl;
22+
cout << randomized_select(dn.get(), num, 5) << endl;
23+
cout << randomized_select(n, 5) << endl;
24+
}

III Data Structures/Widget.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
// a class for test.
3+
4+
#include <iostream>
5+
using std::cout;
6+
using std::endl;
7+
8+
class Widget {
9+
public:
10+
Widget() { cout << "Widget()" << endl; }
11+
Widget(const Widget& rhs) { cout << "Widget(const Widget&)" << endl; }
12+
Widget(Widget&& rhs) { cout << "Widget(Widget&&)" << endl; }
13+
~Widget() { cout << "~Widget()" << endl; }
14+
Widget& operator=(const Widget&) {
15+
cout << "operator=(const Widget&)" << endl;
16+
return *this;
17+
}
18+
Widget& operator=(Widget&&) {
19+
cout << "operator=(Widget&&)" << endl;
20+
return *this;
21+
}
22+
};

III Data Structures/arrayList.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
template <typename T>
4+
class arrayList {
5+
public:
6+
typedef std::size_t size_t;
7+
arrayList();
8+
arrayList(const arrayList& rhs);
9+
arrayList(arrayList&& rhs);
10+
~arrayList();
11+
arrayList& operator=(const arrayList& rhs);
12+
arrayList& operator=(arrayList&& rhs);
13+
void swap(arrayList& rhs);
14+
15+
protected:
16+
size_t length;
17+
T* p;
18+
};
19+
20+
template <typename T>
21+
arrayList<T>::arrayList() : length(10), p(new T[length]) {}
22+
23+
template <typename T>
24+
arrayList<T>::arrayList(const arrayList& rhs)
25+
: length(rhs.length), p(new T[length]) {
26+
for (size_t i = 0; i < length; i++) p[i] = rhs.p[i];
27+
}
28+
29+
template <typename T>
30+
arrayList<T>::arrayList(arrayList&& rhs) : length(rhs.length), p(rhs.p) {
31+
rhs.p = nullptr;
32+
}
33+
34+
template <typename T>
35+
arrayList<T>::~arrayList() {
36+
delete[] p;
37+
}
38+
39+
template <typename T>
40+
arrayList<T>& arrayList<T>::operator=(const arrayList& rhs) {
41+
if (this != &rhs) {
42+
arrayList t = rhs;
43+
swap(t);
44+
} // t is destroyed, including old elments of this.
45+
return *this;
46+
}
47+
48+
template <typename T>
49+
arrayList<T>& arrayList<T>::operator=(arrayList&& rhs) {
50+
swap(rhs);
51+
return *this;
52+
}
53+
54+
template <typename T>
55+
void arrayList<T>::swap(arrayList& rhs) {
56+
using std::swap;
57+
swap(p, rhs.p);
58+
swap(length, rhs.length);
59+
}

III Data Structures/myExceptions.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
// exception classes for various error types
3+
4+
#include <iostream>
5+
#include <string>
6+
7+
// illegal parameter value
8+
class illegalParameterValue {
9+
public:
10+
illegalParameterValue(std::string theMessage = "Illegal parameter value") {
11+
message = theMessage;
12+
}
13+
void outputMessage() { std::cout << message << std::endl; }
14+
15+
private:
16+
std::string message;
17+
};
18+
19+
// illegal input data
20+
class illegalInputData {
21+
public:
22+
illegalInputData(std::string theMessage = "Illegal data input") {
23+
message = theMessage;
24+
}
25+
void outputMessage() { std::cout << message << std::endl; }
26+
27+
private:
28+
std::string message;
29+
};
30+
31+
// illegal index
32+
class illegalIndex {
33+
public:
34+
illegalIndex(std::string theMessage = "Illegal index") {
35+
message = theMessage;
36+
}
37+
void outputMessage() { std::cout << message << std::endl; }
38+
39+
private:
40+
std::string message;
41+
};

III Data Structures/queue.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#pragma once
2+
3+
#include "arrayList.h"
4+
#include "myExceptions.h"
5+
6+
template <typename T>
7+
class queue : private arrayList<T> {
8+
public:
9+
using typename arrayList<T>::size_t;
10+
queue();
11+
queue(const queue& rhs);
12+
queue(queue&& rhs);
13+
queue& operator=(const queue& rhs);
14+
queue& operator=(queue&& rhs);
15+
~queue() {}
16+
17+
bool empty() const;
18+
void enqueue(const T& x);
19+
template <typename... Ts>
20+
void enqueue(Ts&&... params);
21+
T dequeue();
22+
23+
private:
24+
using arrayList<T>::length;
25+
using arrayList<T>::p;
26+
size_t head;
27+
size_t size;
28+
void enlarge();
29+
size_t tail() const { return (head + size) % length; }
30+
};
31+
32+
template <typename T>
33+
void queue<T>::enqueue(const T& x) {
34+
enlarge();
35+
p[tail()] = x;
36+
size++;
37+
}
38+
39+
template <typename T>
40+
template <typename... Ts>
41+
void queue<T>::enqueue(Ts&&... params) {
42+
enlarge();
43+
p[tail()] = T(std::forward<Ts>(params)...);
44+
// 对于 local objects,上一步中的拷贝赋值可能会优化为移动赋值。
45+
size++;
46+
}
47+
48+
template <typename T>
49+
T queue<T>::dequeue() {
50+
if (empty()) {
51+
throw illegalIndex("Underflow. queue is empty!");
52+
}
53+
auto x = p[head++];
54+
head %= length;
55+
size--;
56+
return x;
57+
}
58+
59+
template <typename T>
60+
queue<T>::queue() : arrayList<T>(), head(0), size(0) {}
61+
62+
template <typename T>
63+
queue<T>::queue(const queue& rhs)
64+
: arrayList<T>(rhs), head(rhs.head), size(rhs.size) {}
65+
66+
template <typename T>
67+
queue<T>::queue(queue&& rhs)
68+
: arrayList<T>(std::move(rhs)), head(rhs.head), size(rhs.size) {}
69+
70+
template <typename T>
71+
queue<T>& queue<T>::operator=(const queue& rhs) {
72+
arrayList<T>::operator=(rhs);
73+
head = rhs.head;
74+
size = rhs.size;
75+
return *this;
76+
}
77+
78+
template <typename T>
79+
queue<T>& queue<T>::operator=(queue&& rhs) {
80+
arrayList<T>::operator=(std::move(rhs));
81+
head = rhs.head;
82+
size = rhs.size;
83+
return *this;
84+
}
85+
86+
template <typename T>
87+
bool queue<T>::empty() const {
88+
return size == 0;
89+
}
90+
91+
template <typename T>
92+
void queue<T>::enlarge() {
93+
if (size == length) {
94+
T* tp = new T[length * 2];
95+
for (size_t i = 0; i < size; i++) tp[i] = p[(head + i) % length];
96+
delete[] p;
97+
p = tp;
98+
length *= 2;
99+
head = 0;
100+
}
101+
}

0 commit comments

Comments
 (0)