算法第四版习题讲解

算法练习(47): 队列、栈的复制(1.3.41-1.3.42)

2017-11-18  本文已影响191人  kyson老师

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(算法交流),想要加入的,请添加我的微信号:zhujinhui207407 谢谢。另外,本人的个人博客 http://www.kyson.cn 也在不停的更新中,欢迎一起讨论

算法(第4版)

知识点

题目

1.3.41 复制队列。编写一个新的构造函数,使以下代码:
Queue r = new Queue(q);
得到的 r 指向队列 q 的一个新的独立的副本。可以对 q 或 r 进行任意入列或出列操作但它们不会相互影响。


1.3.41 Copy a queue. Create a new constructor so that
Queue<Item> r = new Queue<Item>(q);
makes r a reference to a new and independent copy of the queue q. You should be able to push and pop from either q or r without influencing the other. Hint : Delete all of the elements from q and add these elements to both q and r.

答案

题目

复制栈。为基于链表实现的栈编写一个新的构造函数,使以下代码

Stack t = new Stack(s);

得到的 t 指向栈 s 的一个新的独立的副本。


1.3.42 Copy a stack. Create a new constructor for the linked-list implementation of Stack so that
Stack<Item> t = new Stack<Item>(s);
makes t a reference to a new and independent copy of the stack s.

广告

我的首款个人开发的APP壁纸宝贝上线了,欢迎大家下载。

上一篇 下一篇

猜你喜欢

热点阅读