- 1、本文档共8页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
(精品课件)第四次chapter4作业(10.18评讲)
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
您可能关注的文档
- (教育学原理)第九章裴斯泰洛齐的教育思想.ppt
- (教育学原理)第5讲 教育与社会发展 未经整理.doc
- (论文)哈尔滨市民生尚都安置小区环境景观规划设计12.17.doc
- (教育学原理)思考题答案 学长给的 仅供参考.doc
- (教育学原理)第3讲 教育的历史发展.doc
- (教育学原理)康永久 第四讲 人的发展与教育 没讲的康老师说不考.ppt
- (教育学原理)第1讲 教育学的演变轨迹 录音.doc
- (教育学原理)教育史.doc
- (教育学原理)简明中国教育史课件11.ppt
- (教育心理学)第五讲格式塔心理学.ppt
- 基层群众自治制度高中政治统编版必修三政治与法治.pptx
- 坚持“两个毫不动摇”高中政治统编版必修二.pptx
- 民族区域自治制度 高中政治统编版必修3.pptx
- 坚持党的领导高中政治统编版必修三.pptx
- 2026届新高考政治热点精准复习人民代表大会:我国的国家权力机关.pptx
- 中国共产党领导人民站起来+富起来+强起来+高中政治统编版必修三.pptx
- 联合国+高中政治统编版选择性必修一.pptx
- 2026届新高考政治热点精准复习优化内外制度环境+增强经济发展活力.pptx
- 2026届新高考政治热点精准复习 推进中国式现代化实现跨越式大发展.pptx
- 法律保护下的婚姻+高中政治统编版选择性必修二法律与生活.pptx
文档评论(0)