- 1、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
(精品课件)第四次chapter4作业1(10.24评讲)
P130 E2 E2. Consider a linked stack that includes a method size. This method size requires a loop that moves through the entire stack to count the entries, since the number of entries in the stack is not kept as a separate member in the stack record. Write a method size for a linked stack by using a loop that moves a pointer variable from node to node through the stack. Answer int Stack :: size() const /* Post: Return the number of entries in the Stack. */ { Node *temp = top_node; int count = 0; while (temp != NULL) { temp = temp -next; count++; } return count; } Consider modifying the declaration of a linked stack to make a stack into a structure with two members, the top of the stack and a counter giving its size. What changes will need to be made to the other methods for linked stacks? Discuss the advantages and disadvantages of this modi?cation compared to the original implementation of linked stacks. Answer 构造函数需添加将count初始化为0的语句。 empty函数和size函数只要根据count返回相应结果。 成功的push操作应使count增1 成功的pop操作应使count减1 优点:可使链栈类与顺序栈类一致。 减少size方法的运行时间。 缺点:增加额外的变量和额外的代码。 是否添加count,取决于size方法的调用频率。 P137 4.3 E2 E2. What is wrong with the following attempt to use the copy constructor to implement the overloaded assignment operator for a linked Stack? void Stack :: operator = (const Stack original) { Stack new_copy(original); top_node = new_copy.top_node; } How can we modify this code to give a correct implementation? Answer The assignment top_node = new_copy.top_node; leaves the former nodes of the left hand side of the assignment operator as garbage.应该回收的空间没回收,结果丢了变成垃圾! Moreover, when the function ends, the statically allocated Stack new_copy will be destructed, this will destroy the newly assigned left hand side of the assignment operator. 把需要赋值不该回收的空间回收了。 We can avoid both problems by swapping the pointers top_node and new_copy.top_node as in the following. //用书上的方法也可以 void Stack :: operator = (const Stack original) /* Post: The Stack is reset as a copy of Stack original. */ { Stack n
您可能关注的文档
最近下载
- 义务教育版(2024)四年级全一册第7课《数字编码随处见》.pptx VIP
- 《工程勘察定向钻探技术规程》.pdf VIP
- 多联机空调施工组织设计方案.docx VIP
- 计算机应用基础(Windows10+WPSOffice 2019)中职全套教学课件.pptx
- DELL EMC 存储设备健康检查报告(模板).doc VIP
- 金融知识竞赛题库第四部分中国人民银行法试题及答案.doc VIP
- 2025-2030年中国猪苓中药材行业现状调查及投资前景预测研究报告.docx
- 大疆无人机公司介绍.ppt VIP
- 防酸碱劳保培训课件.pptx VIP
- GBZT 181-2024 建设项目放射性职业病危害评价报告编制标准.pdf VIP
文档评论(0)