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

hw02 done #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
74 changes: 51 additions & 23 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,40 @@

struct Node {
// 这两个指针会造成什么问题?请修复
std::shared_ptr<Node> next;
std::shared_ptr<Node> prev;
// std::shared_ptr<Node> next;
// std::shared_ptr<Node> prev;
// 如果能改成 unique_ptr 就更好了!
std::unique_ptr<Node> next;
Node* prev;
// 类似于课上所讲的情况,两个共享指针互相指向,会造成循环引用,引用计数无法置零,无法被回收
// 前一个指针“拥有”后一个指针

int value;

// 这个构造函数有什么可以改进的?
Node(int val) {
value = val;
// 使用引用增加效率
Node(const 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;
// 插入当前结点之后,但是如果当前节点是独立的节点,似乎没有办法实现
// 但是双向链表有头结点,而且insert函数没有被使用过
auto node = std::make_unique<Node>(val);
if(next)
{
next->prev = node.get();
node->next = std::move(next);
}
node->prev = prev->next.get();
next = std::move(node);
}

void erase() {
if (prev)
prev->next = next;
if (next)
if(next.get())
next->prev = prev;
if(prev)
prev->next = std::move(next);
// 因为调用std::move的原因,调整顺序
}

~Node() {
Expand All @@ -38,14 +46,28 @@ struct Node {
};

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

List() = default;

List(List const &other) {
printf("List 被拷贝!\n");
head = other.head; // 这是浅拷贝!
// head = other.head; // 这是浅拷贝!
// 请实现拷贝构造函数为 **深拷贝**
auto cur = other.head.get();
if(cur) {
while(cur->next.get()) {
cur = cur->next.get();
}
while(cur->prev) {
this->push_front(cur->value);
cur = cur->prev;
}
this->push_front(cur->value);
}
else {
head = nullptr;
}
}

List &operator=(List const &) = delete; // 为什么删除拷贝赋值函数也不出错?
Expand All @@ -58,29 +80,35 @@ struct List {
}

int pop_front() {
// 未作非合法判断,不知道返回什么值合适,exception也还没有学过
int ret = head->value;
head = head->next;
if(head->next.get())
head = std::move(head->next);
else
head = nullptr;
return ret;
}

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

Node *at(size_t index) const {
auto curr = front();
for (size_t i = 0; i < index; i++) {
if(curr == nullptr) // 越界处理
return nullptr;
curr = curr->next.get();
}
return curr;
}
};

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