Skip to content
Open

HW02 #86

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
184 changes: 100 additions & 84 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,118 +2,134 @@
#include <cstdio>
#include <memory>

struct Node {
// 这两个指针会造成什么问题?请修复
std::shared_ptr<Node> next;
std::shared_ptr<Node> prev;
// 如果能改成 unique_ptr 就更好了!

int value;

// 这个构造函数有什么可以改进的?
Node(int val) {
value = val;
}

void insert(int val) {
auto node = std::make_shared<Node>(val);
node->next = next;
node->prev = prev;
if (prev)
prev->next = node;
if (next)
next->prev = node;
struct Node : std::enable_shared_from_this<Node> {
// 这两个指针会造成什么问题?请修复
std::shared_ptr<Node> next;
std::weak_ptr<Node> prev; // 解决循环引用
// 如果能改成 unique_ptr 就更好了!

int value;

// 改进构造函数,初始化所有成员
explicit Node(int val) : next(nullptr), value(val) {}

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

void erase() {
auto prev_node = prev.lock();
if (prev_node) {
prev_node->next = next;
}

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

~Node() {
printf("~Node()\n"); // 应输出多少次?为什么少了?
}
~Node() {
printf("~Node()\n"); // 应输出多少次?为什么少了?
}
};

struct List {
std::shared_ptr<Node> head;
std::shared_ptr<Node> head;

List() = default;
List() = default;

List(List const &other) {
printf("List 被拷贝!\n");
head = other.head; // 这是浅拷贝!
// 请实现拷贝构造函数为 **深拷贝**
}
List(List const &other) {
printf("List 被拷贝!\n");
// 这是浅拷贝!
// 请实现拷贝构造函数为 **深拷贝**

List &operator=(List const &) = delete; // 为什么删除拷贝赋值函数也不出错?
if (!other.head)
return;

List(List &&) = default;
List &operator=(List &&) = default;
head = std::make_shared<Node>(other.head->value);
auto cur = head;
auto ne = other.head->next;

Node *front() const {
return head.get();
while (ne != nullptr) {
auto new_node = std::make_shared<Node>(ne->value);
cur->next = new_node;
new_node->prev = cur;
ne = ne->next;
cur = new_node;
}
}

int pop_front() {
int ret = head->value;
head = head->next;
return ret;
}
List &operator=(List const &) = delete; // 为什么删除拷贝赋值函数也不出错?

void push_front(int value) {
auto node = std::make_shared<Node>(value);
node->next = head;
if (head)
head->prev = node;
head = node;
}
List(List &&) = default;
List &operator=(List &&) = default;

Node *at(size_t index) const {
auto curr = front();
for (size_t i = 0; i < index; i++) {
curr = curr->next.get();
}
return curr;
Node *front() const { return head.get(); }

int pop_front() {
int ret = head->value;
head = head->next;
if (head) {
head->prev.reset();
}
return ret;
}

void push_front(int value) {
auto node = std::make_shared<Node>(value);
node->next = head;
if (head)
head->prev = node;
head = node;
}

Node *at(size_t index) const {
auto curr = front();
for (size_t i = 0; i < index; i++) {
curr = curr->next.get();
}
return curr;
}
};

void print(List lst) { // 有什么值得改进的?
printf("[");
for (auto curr = lst.front(); curr; curr = curr->next.get()) {
printf(" %d", curr->value);
}
printf(" ]\n");
void print(const List &lst) { // 有什么值得改进的?
printf("[");
for (auto curr = lst.front(); curr; curr = curr->next.get()) {
printf(" %d", curr->value);
}
printf(" ]\n");
}

int main() {
List a;
List a;

a.push_front(7);
a.push_front(5);
a.push_front(8);
a.push_front(2);
a.push_front(9);
a.push_front(4);
a.push_front(1);
a.push_front(7);
a.push_front(5);
a.push_front(8);
a.push_front(2);
a.push_front(9);
a.push_front(4);
a.push_front(1);

print(a); // [ 1 4 9 2 8 5 7 ]
print(a); // [ 1 4 9 2 8 5 7 ]

a.at(2)->erase();
a.at(2)->erase();

print(a); // [ 1 4 2 8 5 7 ]
print(a); // [ 1 4 2 8 5 7 ]

List b = a;
List b = a;

a.at(3)->erase();
a.at(3)->erase();

print(a); // [ 1 4 2 5 7 ]
print(b); // [ 1 4 2 8 5 7 ]
print(a); // [ 1 4 2 5 7 ]
print(b); // [ 1 4 2 8 5 7 ]

b = {};
a = {};
b = {};
a = {};

return 0;
return 0;
}