-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse.cpp
43 lines (36 loc) · 913 Bytes
/
Reverse.cpp
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
40
41
42
43
// 1. Reverse a string without using a built-in function
// 2. Reverse a linked list without using a built-in function
// 3. Use standard function to reverse vector
// 4. Implement unit test checking correctness of N1 Reverse(string) implementation
#include <string>
#include <list>
#include <vector>
// N1
std::string ReverseString(const std::string& str) {
std::string res = str;
for (int i = 0; i < str.size()/2; i++) {
res[i] = str[str.size() - 1 - i];
res[str.size() - 1 - i] = str[i];
}
return res;
}
// N2
void ReverseList1(std::list<int>& l) {
std::list<int> res;
for(auto i : l) {
res.push_front(i);
}
l = res;
}
// N3
void ReverseVector(std::vector<int>& v) {
std::reverse(v.begin(), v.end());
}
// N4
bool ReverseLinkTest(std::list<int>& e) {
std::list<int> etl = e;
std::reverse(etl.begin(), etl.end());
std::list<int> comp = e;
ReverseList1(comp);
return (comp == etl);
}