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 #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

hw02 #52

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
62 changes: 39 additions & 23 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,59 +1,71 @@
/* 基于智能指针实现双向链表 */
#include <cstdio>
#include <memory>
#include <cstring>

#include <iostream>

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

int value;
T value;

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

void insert(int val) {
auto node = std::make_shared<Node>(val);
void insert(const T &val) {
auto node = std::make_shared<Node<T>>(val);
node->next = next;
node->prev = prev;
if (prev)
prev->next = node;
if (prev.expired())
prev.lock()->next = node;
if (next)
next->prev = node;
}

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

~Node() {
// std::cout << prev.use_count() << std::endl;
// std::cout << next.use_count() << std::endl;
printf("~Node()\n"); // 应输出多少次?为什么少了?
/*
用use_count 测试了一下显示被循环引用了。
*/
}
};

template <class T>
struct List {
std::shared_ptr<Node> head;
std::shared_ptr<Node<T>> head;

List() = default;

List(List const &other) {
printf("List 被拷贝!\n");
head = other.head; // 这是浅拷贝!
// 请实现拷贝构造函数为 **深拷贝**
memcpy(head.get(), other.head.get(), sizeof(std::shared_ptr<Node<T>>));
}

List &operator=(List const &) = delete; // 为什么删除拷贝赋值函数也不出错?
/*
好像这里直接隐式调用了拷贝构造。当加入explicit就全错了
*/

List(List &&) = default;
List &operator=(List &&) = default;

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

Expand All @@ -64,14 +76,14 @@ struct List {
}

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

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

void print(List lst) { // 有什么值得改进的?
template <class T>
void print(List<T> lst) { // 有什么值得改进的?
/*
还不太懂数据结构不会改。
*/
printf("[");
for (auto curr = lst.front(); curr; curr = curr->next.get()) {
printf(" %d", curr->value);
Expand All @@ -89,7 +105,7 @@ void print(List lst) { // 有什么值得改进的?
}

int main() {
List a;
List<int> a;

a.push_front(7);
a.push_front(5);
Expand All @@ -99,18 +115,18 @@ int main() {
a.push_front(4);
a.push_front(1);

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

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

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

List b = a;
List<int> b = a;

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

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

b = {};
a = {};
Expand Down