Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete hw02 && 尝试支持了迭代器 #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
GNUmakefile
.cache
114 changes: 88 additions & 26 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,79 @@
#include <memory>

struct Node {
// 这两个指针会造成什么问题?请修复
std::shared_ptr<Node> next;
std::shared_ptr<Node> prev;
/* Q: 这两个指针会造成什么问题?
A: 产生了环形引用,导致无法解构。 */
std::unique_ptr<Node> next;
struct Node* prev = nullptr;
// 如果能改成 unique_ptr 就更好了!

int value;

// 这个构造函数有什么可以改进的?
Node(int val) {
value = val;
}
/* Q: 这个构造函数有什么可以改进的?
A: 添加 explicit 避免隐式转换。 */
explicit Node(int val) : value(val) {}

void insert(int val) {
auto node = std::make_shared<Node>(val);
node->next = next;
auto node = std::make_unique<Node>(val);
node->prev = prev;
if (prev)
prev->next = node;
if (next)
next->prev = node;
next->prev = node.get();
if (prev)
prev->next = std::move(node);
node->next = std::move(next);
}

void erase() {
if (prev)
prev->next = next;
prev->next = std::move(next);
if (next)
next->prev = prev;
}

~Node() {
printf("~Node()\n"); // 应输出多少次?为什么少了?
/* Q: 应输出多少次?为什么少了?
A: 产生了环形引用,导致无法解构。 */
printf("~Node(%d)\n", value);
}
};

struct List {
std::shared_ptr<Node> head;
std::unique_ptr<Node> head;
struct Node* tail;

List() = default;
List() {
auto node_tail = std::make_unique<Node>(0);
tail = node_tail.get();
head = std::move(node_tail);
};

List(List const &other) {
printf("List 被拷贝!\n");
head = other.head; // 这是浅拷贝!
// 请实现拷贝构造函数为 **深拷贝**
auto node_tail = std::make_unique<Node>(0);
tail = node_tail.get();
head = std::move(node_tail);
std::unique_ptr<Node>* last = nullptr;
for (auto curr = other.front(); curr != other.tail; curr = curr->next.get()) {
auto node = std::make_unique<Node>(curr->value);
if (last == nullptr) {
node->next = std::move(head);
head = std::move(node);
last = &head;
} else {
node->next = std::move((*last)->next);
node->prev = last->get();
(*last)->next = std::move(node);
last = &((*last)->next);
}
tail->prev = last->get();
}
}

List &operator=(List const &) = delete; // 为什么删除拷贝赋值函数也不出错?
/* Q: 为什么删除拷贝赋值函数也不出错?
A: 因为这相当于使用拷贝构造函数就地构造出对象,从而进一步调用移动赋值函数完成整个过程。
如果使用 explicit 关键词修饰拷贝构造函数,则会产生编译错误。 */
List &operator=(List const &) = delete;

List(List &&) = default;
List &operator=(List &&) = default;
Expand All @@ -59,16 +86,23 @@ struct List {

int pop_front() {
int ret = head->value;
head = head->next;
if (head->next.get() == tail) {
head = nullptr;
tail->prev = nullptr;
} else {
head = std::move(head->next);
}
return ret;
}

void push_front(int value) {
auto node = std::make_shared<Node>(value);
node->next = head;
auto node = std::make_unique<Node>(value);
if (head)
head->prev = node;
head = node;
head->prev = node.get();
else
tail->prev = node.get();
node->next = std::move(head);
head = std::move(node);
}

Node *at(size_t index) const {
Expand All @@ -78,12 +112,37 @@ struct List {
}
return curr;
}

class iterator : public std::iterator<std::input_iterator_tag, Node, int,
const Node *, int> {
Node *node;

public:
explicit iterator(Node *_node) : node(_node) {}
iterator &operator++() {
node = node->next.get();
return *this;
}
iterator operator++(int) {
iterator retval = *this;
++(*this);
return retval;
}
bool operator==(iterator other) const { return node == other.node; }
bool operator!=(iterator other) const { return !(*this == other); }
reference operator*() const { return node->value; }
};
iterator begin() { return iterator(head.get()); }
iterator end() { return iterator(tail); }
};

void print(List lst) { // 有什么值得改进的?

/* Q: 有什么值得改进的?
A: 改为引用传递,减少一次深拷贝。 */
void print(List &lst) {
printf("[");
for (auto curr = lst.front(); curr; curr = curr->next.get()) {
printf(" %d", curr->value);
for (auto it = lst.begin(); it != lst.end(); it++) {
printf(" %d", *it);
}
printf(" ]\n");
}
Expand Down Expand Up @@ -112,8 +171,11 @@ int main() {
print(a); // [ 1 4 2 5 7 ]
print(b); // [ 1 4 2 8 5 7 ]

puts("------------");
b = {};
puts("------------");
a = {};
puts("------------");

return 0;
}