Skip to content

Finish hw02 #82

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

Open
wants to merge 1 commit 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 @@
tags
build
GNUmakefile
83 changes: 61 additions & 22 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
/* 基于智能指针实现双向链表 */
#include <cstdio>
#include <memory>
#include <memory> // For three smart pointers
#include <cassert> // It's a invaluable thing

struct Node {
// Actually, we need public inherit
struct Node : std::enable_shared_from_this<Node> {
int value;
// 这两个指针会造成什么问题?请修复
// Obviously, it will cause cycle reference
std::shared_ptr<Node> next;
std::shared_ptr<Node> prev;
std::weak_ptr<Node> prev;
// 如果能改成 unique_ptr 就更好了!

int value;
// We can do this from another branch.

// 这个构造函数有什么可以改进的?
Node(int val) {
value = val;
// Explicit and Initializer list
explicit Node(int val) : value(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)
node->prev = shared_from_this();
if(next)
next->prev = node;
next = node;
}

void erase() {
if (prev)
prev->next = next;
if (prev.lock())
prev.lock()->next = next;
if (next)
next->prev = prev;
}
Expand All @@ -43,17 +48,44 @@ struct List {
List() = default;

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

auto current_head = std::make_shared<Node>(origin_head->value);
auto result = current_head;
origin_head = origin_head->next;
while(origin_head){
//while(origin_head.get()){
current_head->insert(origin_head->value);
origin_head = origin_head->next;
current_head = current_head->next;
}
// Inside constructor, there is no content before, so it's redundant.
// You can see the content of front()
assert(!front());
printf("front() == %p\n", front());
for (auto cur = front(); cur; ) {
auto tmp = cur;
cur = cur->next.get();
delete tmp;
}
head = result;
}

// From this source file context, there is no lvalue assignment copy action.
List &operator=(List const &) = delete; // 为什么删除拷贝赋值函数也不出错?

// Move sementics
List(List &&) = default;
List &operator=(List &&) = default;

Node *front() const {
Node* front() const {
return head.get();
}

Expand All @@ -72,6 +104,7 @@ struct List {
}

Node *at(size_t index) const {
// auto is Node*
auto curr = front();
for (size_t i = 0; i < index; i++) {
curr = curr->next.get();
Expand All @@ -80,10 +113,10 @@ struct List {
}
};

void print(List lst) { // 有什么值得改进的?
void print(const List& lst) { // 有什么值得改进的?
printf("[");
for (auto curr = lst.front(); curr; curr = curr->next.get()) {
printf(" %d", curr->value);
for (auto cur = lst.front(); cur; cur = cur->next.get()) {
printf(" %d", cur->value);
}
printf(" ]\n");
}
Expand All @@ -98,18 +131,24 @@ int main() {
a.push_front(9);
a.push_front(4);
a.push_front(1);
a.push_front(9);

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

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

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

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

List b = a;

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

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

a.at(3)->insert(10);
print(a); // [ 1 4 2 5 10 7 ]

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

b = {};
Expand Down