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

xiao-ming-hub 完成了作业 #68

Open
wants to merge 4 commits 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
129 changes: 54 additions & 75 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,72 @@
/* 基于智能指针实现双向链表 */
#include <cstdio>
#include <iostream>
#include <memory>
#include <initializer_list>

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

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;
}
Node(Tp val): value(val) {}
~Node() { std::clog << "~Node(" << value << ")\n"; }

void insert(Tp val) { value = val; }
void erase() {
if (prev)
prev->next = next;
if (next)
next->prev = prev;
}

~Node() {
printf("~Node()\n"); // 应输出多少次?为什么少了?
if (next) next->prev = prev;
if (prev) prev->next = std::move(next);
}
};

struct List {
std::shared_ptr<Node> head;
template <typename Tp, typename Init = std::initializer_list<Tp>> struct List {
std::unique_ptr<Node<Tp>> head;

List() = default;

List(List const &other) {
printf("List 被拷贝!\n");
head = other.head; // 这是浅拷贝!
// 请实现拷贝构造函数为 **深拷贝**
List(Init const &range) {
if (range.begin() == range.end()) return;
Node<Tp> *tail;
auto it = range.begin();
head = std::make_unique<Node<Tp>>(*it);
for (++it, tail = head.get(); it != range.end(); ++it) {
tail->next = std::make_unique<Node<Tp>>(*it);
tail->next->prev = tail;
tail = tail->next.get();
}
}

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

List &operator=(List const &) = delete; // 为什么删除拷贝赋值函数也不出错?
// 因为编译器会用“解构函数+拷贝构造”代替拷贝赋值。
List(List &&) = default;
List &operator=(List &&) = default;

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

struct iterator {
Node<Tp> *node;
iterator(Node<Tp> *p): node(p) {}

Tp operator*() { return node->value; }
iterator operator++() { return node = node->next.get(); }
iterator operator--() { return node = node->prev; }
bool operator==(iterator const &other) { return node == other.node; }
bool operator!=(iterator const &other) { return node != other.node; }
};
iterator begin() const { return iterator(head.get()); }
iterator end() const { return iterator(nullptr); }

Node<Tp> *front() const { return head.get(); }
bool empty() const { return !(head); }
int pop_front() {
if (head == nullptr) return 0x114154;
int ret = head->value;
head = head->next;
head = std::move(head->next);
return ret;
}

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

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

void print(List lst) { // 有什么值得改进的?
printf("[");
for (auto curr = lst.front(); curr; curr = curr->next.get()) {
printf(" %d", curr->value);
}
printf(" ]\n");
template <typename Tp, typename Init>
std::ostream &operator<<(std::ostream &os, List<Tp, Init> const &l) {
for (os << '['; Tp x : l) os << x << ", ";
os << (l.empty() ? "]" : "\b\b]");
return os;
}

int main() {
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);

#define print(l) std::cout << l << '\n';
List<int> a = {1, 4, 9, 2, 8, 5, 7};
print(a); // [ 1 4 9 2 8 5 7 ]

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

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

List b = a;

List<int, List<int>> b = a;
a.at(3)->erase();

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

b = {};
a = {};

return 0;
}