Skip to content

Long_hw_02 #77

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
Empty file added .vs/CMake Overview
Empty file.
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": "x64-Debug"
}
12 changes: 12 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"OutputFoldersPerTargetSystem": {
"\u672C\u5730\u8BA1\u7B97\u673A": [
"out\\build\\x64-Debug",
"out\\install\\x64-Debug"
]
},
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
Binary file not shown.
Binary file added .vs/hw02/v17/.wsuo
Binary file not shown.
Binary file added .vs/hw02/v17/Browse.VC.db
Binary file not shown.
12 changes: 12 additions & 0 deletions .vs/hw02/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Version": 1,
"WorkspaceRootPath": "D:\\Github_clone\\hw02\\",
"Documents": [],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": []
}
]
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
119 changes: 79 additions & 40 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,131 @@
#include <cstdio>
#include <memory>

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

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;
void insert(int val)
{
auto node = std::make_unique<Node>(val);
if (next)
{
next->prev = node.get();
}
node->next = std::move(next);
node->prev = prev;
if (prev)
prev->next = node;
if (next)
next->prev = node;
prev->next = std::move(node);
}

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

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

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

List() = default;

List(List const &other) {
printf("List 被拷贝!\n");
head = other.head; // 这是浅拷贝!
List(List const &other)
{
auto head = std::make_unique<Node>(other.head->value);

if (!other.head)
{
head = nullptr;
}
else
{
auto ptr_this = head.get();
auto ptr_other = other.head.get();
while (ptr_other->next)
{
ptr_this->next = std::make_unique<Node>(ptr_other->next->value);
ptr_this->next->prev = ptr_this;
ptr_other = ptr_other->next.get();
ptr_this = ptr_this->next.get();
}
}
// 这是浅拷贝!
// 请实现拷贝构造函数为 **深拷贝**
}

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

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

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

int pop_front() {
int pop_front()
{
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;
void push_front(int value)
{
auto node = std::make_unique<Node>(value);
if (head)
head->prev = node;
head = node;
{
head->prev = node.get();
}
node->next = std::move(head);

head = std::move(node);
}

Node *at(size_t index) const {
Node *at(size_t index) const
{
auto curr = front();
for (size_t i = 0; i < index; i++) {
for (size_t i = 0; i < index; i++)
{
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()) {
for (auto curr = lst.front(); curr; curr = curr->next.get())
{
printf(" %d", curr->value);
}
printf(" ]\n");
}

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

a.push_front(7);
Expand All @@ -99,18 +137,19 @@ int main() {
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)->insert(9);

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

List b = a;

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 = {};
Expand Down
Binary file added main.exe
Binary file not shown.