Skip to content

Commit 52d3fdd

Browse files
committed
update ch1
1 parent 342109b commit 52d3fdd

File tree

40 files changed

+409
-352
lines changed

40 files changed

+409
-352
lines changed

ch1/20_ResizingArrayQueue/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code ResizingArrayQueue} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
ResizingArrayQueue<string> queue;
1115
string tmp;
1216
while (file >> tmp) {

ch1/21_LinkedQueue/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
#include <fstream>
33
#include "../head/LinkedQueue.h"
44

5-
#include <memory>
65

76
using namespace std;
87

9-
8+
/**
9+
* Unit tests the {@code LinkedQueue} data type.
10+
*
11+
* @param args the command-line arguments
12+
*/
1013
int main() {
11-
// unique_ptr<int> p(nullptr);
12-
// cout << (p == nullptr) << endl;
13-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
14+
ifstream file("./data/tobe.txt");
1415
LinkedQueue<string> queue;
1516
string tmp;
1617
while (file >> tmp) {

ch1/22_Queue/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code Queue} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
Queue<string> queue;
1115
string tmp;
1216
while (file >> tmp) {

ch1/23_ResizingArrayBag/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code ResizingArrayBag} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
ResizingArrayBag<string> bag;
1115
bag.add("Hello");
1216
bag.add("World");

ch1/24_LinkedBag/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code LinkedBag} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
LinkedBag<string> bag;
1115
string tmp;
1216
while (file >> tmp) {

ch1/25_Bag/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code Bag} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
9-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tobe.txt");
13+
ifstream file("./data/tobe.txt");
1014
Bag<string> bag;
1115
string tmp;
1216
while (file >> tmp) {

ch1/26_Stopwatch/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
using namespace std;
66

7-
7+
/**
8+
* Unit tests the {@code Bag} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
812
int main() {
913
int n = 10000;
1014
// sum of square roots of integers from 1 to n using Math.sqrt(x).

ch1/28_LinearRegression/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
using namespace std;
55

6-
6+
/**
7+
* simple test
8+
* @return
9+
*/
710
int main() {
811
vector<double> a{1.0, 2.0, 3.0, 4.1}, b{1.01, 2.01, 2.99, 4.0};
912
LinearRegression lr(a, b);

ch1/29_ThreeSum/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std;
1313
* @param args the command-line arguments
1414
*/
1515
int main() {
16-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/2Kints.txt");
16+
ifstream file("./data/2Kints.txt");
1717
vector<int> vec;
1818
int tmp;
1919
while (file >> tmp)

ch1/30_ThreeSumFast/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std;
1313
* @param args the command-line arguments
1414
*/
1515
int main() {
16-
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/1Kints.txt");
16+
ifstream file("./data/1Kints.txt");
1717
vector<int> vec;
1818
int tmp;
1919
while (file >> tmp)

ch1/31_DoublingTest/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <istream>
2-
#include <fstream>
32
#include "../head/DoublingTest.h"
43

54
using namespace std;

ch1/33_QuickFindUF/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std;
1313
* @param args the command-line arguments
1414
*/
1515
int main() {
16-
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tinyUF.txt");
16+
fstream file("./data/tinyUF.txt");
1717
int n, p, q;
1818
file >> n;
1919
QuickFindUF uf(n);
@@ -22,5 +22,5 @@ int main() {
2222
uf.union_op(p, q);
2323
cout << p << " " << q << endl;
2424
}
25-
cout << uf.getcount() << " components" << endl;
25+
cout << uf.count_() << " components" << endl;
2626
}

ch1/34_QuickUnionUF/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std;
1313
* @param args the command-line arguments
1414
*/
1515
int main() {
16-
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tinyUF.txt");
16+
fstream file("./data/tinyUF.txt");
1717
int n, p, q;
1818
file >> n;
1919
QuickUnionUF uf(n);
@@ -22,5 +22,5 @@ int main() {
2222
uf.union_op(p, q);
2323
cout << p << " " << q << endl;
2424
}
25-
cout << uf.getcount() << " components" << endl;
25+
cout << uf.count_() << " components" << endl;
2626
}

ch1/35_WeightedQuickUnionUF/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std;
1313
* @param args the command-line arguments
1414
*/
1515
int main() {
16-
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/largeUF.txt");
16+
fstream file("./data/largeUF.txt");
1717
int n, p, q;
1818
file >> n;
1919
WeightedQuickUnionUF uf(n);
@@ -22,5 +22,5 @@ int main() {
2222
uf.union_op(p, q);
2323
cout << p << " " << q << endl;
2424
}
25-
cout << uf.getcount() << " components" << endl;
25+
cout << uf.count_() << " components" << endl;
2626
}

ch1/36_UF/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std;
1313
* @param args the command-line arguments
1414
*/
1515
int main() {
16-
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/largeUF.txt");
16+
fstream file("./data/largeUF.txt");
1717
int n, p, q;
1818
file >> n;
1919
UF uf(n);
@@ -22,5 +22,5 @@ int main() {
2222
uf.union_op(p, q);
2323
cout << p << " " << q << endl;
2424
}
25-
cout << uf.getcount() << " components" << endl;
25+
cout << uf.count_() << " components" << endl;
2626
}

ch1/head/Accumulator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ class Accumulator {
5151
* Returns the mean of the data values.
5252
* @return the mean of the data values
5353
*/
54-
double mean() {
54+
double mean() const {
5555
return mu;
5656
}
5757

5858
/**
5959
* Returns the sample variance of the data values.
6060
* @return the sample variance of the data values
6161
*/
62-
double var() {
62+
double var() const {
6363
if (n <= 1) return numeric_limits<double>::quiet_NaN();
6464
return sumval / (n - 1);
6565
}
@@ -68,15 +68,15 @@ class Accumulator {
6868
* Returns the sample standard deviation of the data values.
6969
* @return the sample standard deviation of the data values
7070
*/
71-
double stddev() {
71+
double stddev() const {
7272
return sqrt(var());
7373
}
7474

7575
/**
7676
* Returns the number of data values.
7777
* @return the number of data values
7878
*/
79-
int count() {
79+
int count() const {
8080
return n;
8181
}
8282

ch1/head/Bag.h

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#ifndef CH1_BAG_H
22
#define CH1_BAG_H
33

4-
#include <memory>
5-
6-
using std::unique_ptr;
7-
84

95
/**
106
* The {@code Bag} class represents a bag (or multiset) of
@@ -34,6 +30,14 @@ class Bag {
3430
*/
3531
Bag() : first(nullptr), n(0) {}
3632

33+
~Bag() {
34+
while (first) {
35+
auto tmp = first;
36+
first = first->next;
37+
delete (tmp);
38+
}
39+
}
40+
3741
/**
3842
* Is this bag empty?
3943
* @return true if this bag is empty; false otherwise
@@ -55,36 +59,39 @@ class Bag {
5559
* @param item the item to add to this bag
5660
*/
5761
void add(T item) {
58-
auto oldfirst = move(first);
59-
first.reset(new Node());
60-
first->item = item;
61-
first->next = move(oldfirst);
62+
auto tmp = first;
63+
first = new Node(item);
64+
first->next = tmp;
6265
n++;
6366
}
6467

6568
private:
6669
class Node {
6770
public:
71+
Node() : next(nullptr) {}
72+
73+
Node(T item) : item(item), next(nullptr) {}
74+
6875
T item;
69-
unique_ptr<Node> next;
76+
Node *next;
7077
};
7178

7279
// an iterator
7380
class Iterator {
74-
unique_ptr<Node> data;
81+
Node *data;
7582
int pos;
7683
int n;
7784
public:
7885
// initial iterator
79-
Iterator(Node *data_, int pos_, int n_) : pos(pos_), n(n_) { data.reset(data_); }
86+
Iterator(Node *data_, int pos_, int n_) : pos(pos_), n(n_), data(data_) {}
8087

8188
// get current value
82-
T &operator*() { return data.get()->item; }
89+
T &operator*() { return data->item; }
8390

8491
// next iterator
8592
Iterator &operator++() {
8693
if (++pos == n)pos = 0;
87-
data = move(data->next);
94+
data = data->next;
8895
return *this;
8996
}
9097

@@ -94,22 +101,20 @@ class Bag {
94101

95102
// a const iterator
96103
class ConstIterator {
97-
unique_ptr<Node> data;
104+
Node *data;
98105
int pos;
99106
int n;
100107
public:
101108
// initial iterator
102-
ConstIterator(Node *data_, int pos_, int n_) : pos(pos_), n(n_) {
103-
data.reset(data_);
104-
}
109+
ConstIterator(Node *data_, int pos_, int n_) : pos(pos_), n(n_), data(data_) {}
105110

106111
// get current value
107-
const T &operator*() { return data.get()->item; }
112+
const T &operator*() { return data->item; }
108113

109114
// next iterator
110115
ConstIterator &operator++() {
111116
if (++pos == n)pos = 0;
112-
data = move(data->next);
117+
data = data->next;
113118
return *this;
114119
}
115120

@@ -123,17 +128,17 @@ class Bag {
123128
* @return an iterator that iterates over the items in the bag in arbitrary order
124129
*/
125130

126-
Iterator begin() { return {first.get(), 0, n + 1}; }
131+
Iterator begin() { return {first, 0, n + 1}; }
127132

128133
Iterator end() { return {nullptr, n, n + 1}; }
129134

130-
ConstIterator begin() const { return {first.get(), 0, n + 1}; }
135+
ConstIterator begin() const { return {first, 0, n + 1}; }
131136

132137
ConstIterator end() const { return {nullptr, n, n + 1}; }
133138

134139
private:
135140
int n; // number of elements in bag
136-
unique_ptr<Node> first; // beginning of bag
141+
Node *first; // beginning of bag
137142

138143
};
139144

0 commit comments

Comments
 (0)