Skip to content

finish hw2 #78

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

finish hw2 #78

wants to merge 1 commit into from

Conversation

gamdwk
Copy link

@gamdwk gamdwk commented Aug 13, 2024

``修改 main.cpp,改良其中的双链表类 List:

  • 避免函数参数不必要的拷贝 5 分
    修改print函数参数为常量引用。
  • 修复智能指针造成的问题 10 分
    使用shared_ptr会导致循环引用,因此在List释放head以后,Node无法被释放
    修改next和prev为
std::unique_ptr<Node> next;
Node *prev;

减少了shared_ptr的增加的atomic变量。解决了循环引用问题

  • 改用 unique_ptr<Node> 10 分
    改用unique_ptr,把赋值操作改成移动操作。
    push_front的时候要注意不能提前把head move掉,否则会导致后面判断head为空

  • 实现拷贝构造函数为深拷贝 15 分
    遍历列表并复制

  • 说明为什么可以删除拷贝赋值函数 5 分
    拷贝赋值函数可以由拷贝构造+移动赋值函数实现。

  • 改进 Node 的构造函数 5 分
    使用explicit和列表表达式
    并通过 main() 函数中的基本测试。

[ 1 4 9 2 8 5 7 ]
~Node(9)
[ 1 4 2 8 5 7 ]
List 被拷贝!
~Node(8)
[ 1 4 2 5 7 ]
[ 1 4 2 8 5 7 ]
~Node(1)
~Node(4)
~Node(2)
~Node(8)
~Node(5)
~Node(7)
~Node(1)
~Node(4)
~Node(2)
~Node(5)
~Node(7)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant