diff --git a/.vs/CMake Overview b/.vs/CMake Overview new file mode 100644 index 0000000..e69de29 diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..8f0d733 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "x64-Debug" +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..39e7bf4 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,12 @@ +{ + "OutputFoldersPerTargetSystem": { + "\u672C\u5730\u8BA1\u7B97\u673A": [ + "out\\build\\x64-Debug", + "out\\install\\x64-Debug" + ] + }, + "ExpandedNodes": [ + "" + ], + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/hw02/FileContentIndex/d1a15635-b0fa-4c65-a2f5-aa69f91583a9.vsidx b/.vs/hw02/FileContentIndex/d1a15635-b0fa-4c65-a2f5-aa69f91583a9.vsidx new file mode 100644 index 0000000..3835424 Binary files /dev/null and b/.vs/hw02/FileContentIndex/d1a15635-b0fa-4c65-a2f5-aa69f91583a9.vsidx differ diff --git a/.vs/hw02/v17/.wsuo b/.vs/hw02/v17/.wsuo new file mode 100644 index 0000000..148dd15 Binary files /dev/null and b/.vs/hw02/v17/.wsuo differ diff --git a/.vs/hw02/v17/Browse.VC.db b/.vs/hw02/v17/Browse.VC.db new file mode 100644 index 0000000..7df9af3 Binary files /dev/null and b/.vs/hw02/v17/Browse.VC.db differ diff --git a/.vs/hw02/v17/DocumentLayout.json b/.vs/hw02/v17/DocumentLayout.json new file mode 100644 index 0000000..60340b9 --- /dev/null +++ b/.vs/hw02/v17/DocumentLayout.json @@ -0,0 +1,12 @@ +{ + "Version": 1, + "WorkspaceRootPath": "D:\\Github_clone\\hw02\\", + "Documents": [], + "DocumentGroupContainers": [ + { + "Orientation": 0, + "VerticalTabListWidth": 256, + "DocumentGroups": [] + } + ] +} \ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..91232e7 Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..fc10398 --- /dev/null +++ b/.vscode/tasks.json @@ -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" +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index dc25c6b..227c451 100644 --- a/main.cpp +++ b/main.cpp @@ -2,93 +2,131 @@ #include #include -struct Node { +struct Node +{ // 这两个指针会造成什么问题?请修复 - std::shared_ptr next; - std::shared_ptr prev; + std::unique_ptr 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(val); - node->next = next; + void insert(int val) + { + auto node = std::make_unique(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 head; +struct List +{ + std::unique_ptr head; List() = default; - List(List const &other) { - printf("List 被拷贝!\n"); - head = other.head; // 这是浅拷贝! + List(List const &other) + { + auto head = std::make_unique(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(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(value); - node->next = head; + void push_front(int value) + { + auto node = std::make_unique(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); @@ -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 = {}; diff --git a/main.exe b/main.exe new file mode 100644 index 0000000..779333e Binary files /dev/null and b/main.exe differ