关于CTCI中Java队列实现的问题
我正在研究第5版代码访谈中队列的实现 排队说明如下:关于CTCI中Java队列实现的问题,java,queue,Java,Queue,我正在研究第5版代码访谈中队列的实现 排队说明如下: class Queue { Node first, last; void enqueue(Object item) { // This indicates the Queue is empty // so we set both first and last as // the same item if(first == null) {
class Queue {
Node first, last;
void enqueue(Object item) {
// This indicates the Queue is empty
// so we set both first and last as
// the same item
if(first == null) {
last = new Node(item);
first = last;
} else {
last.next = new Node(item);
last = last.next;
}
}
}
我理解if声明中的内容。此条件处理空队列。因此,如果队列为空,并且我们试图向队列中添加一个项目,则添加的项目既是第一个项目也是最后一个项目
然而,我不理解的是else语句中发生了什么。本书首先将项目指定为last.next
,然后将last
指定为last.next
。这难道不会使最后一个元素和最后一个元素的下一个元素都是一样的吗?此外,我还演示了队列。这是否准确说明了last
和last.next
的位置
[ | | | ]
^ ^
last last.next
else部分中的代码:
1: last.next = new Node(item);
2: last = last.next;
以下是我们将从以下内容开始:
(a) -> (b) -> (c)
^first ^last
我们称之为排队(d)
1号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
我们看到第1行在列表的末尾创建了一个新元素,但是last
仍然指向(c)
2号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
现在,last
指向(d)
,该节点适当地没有next
节点
你看,(c)
永远不会消失,移动的只是最后一个引用
至于为什么它是这样工作的,这一切都归结于对象是Java中的引用这一事实
考虑以下代码:
Object x = 300;
Object y = 500;
Object z = x;
//here, x = 300, y = 500, z = 300
x = y;
//now, x = 500, y = 500, z = 300
//because the actual object that x initially pointed to was not changed, only x as a reference was changed.
在列表的大小分别为n=0
和n=1
的情况下:
NULL
^first,last
NULL (a)
^first ^last
(a)
^first,last
然后是另一个电话
(a)
^first,last
(a) -> (b)
^first,last
(a) -> (b)
^first ^last
希望对else部分的代码有所帮助:
1: last.next = new Node(item);
2: last = last.next;
以下是我们将从以下内容开始:
(a) -> (b) -> (c)
^first ^last
我们称之为排队(d)
1号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
我们看到第1行在列表的末尾创建了一个新元素,但是last
仍然指向(c)
2号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
现在,last
指向(d)
,该节点适当地没有next
节点
你看,(c)
永远不会消失,移动的只是最后一个引用
至于为什么它是这样工作的,这一切都归结于对象是Java中的引用这一事实
考虑以下代码:
Object x = 300;
Object y = 500;
Object z = x;
//here, x = 300, y = 500, z = 300
x = y;
//now, x = 500, y = 500, z = 300
//because the actual object that x initially pointed to was not changed, only x as a reference was changed.
在列表的大小分别为n=0
和n=1
的情况下:
NULL
^first,last
NULL (a)
^first ^last
(a)
^first,last
然后是另一个电话
(a)
^first,last
(a) -> (b)
^first,last
(a) -> (b)
^first ^last
希望对else部分的代码有所帮助:
1: last.next = new Node(item);
2: last = last.next;
以下是我们将从以下内容开始:
(a) -> (b) -> (c)
^first ^last
我们称之为排队(d)
1号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
我们看到第1行在列表的末尾创建了一个新元素,但是last
仍然指向(c)
2号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
现在,last
指向(d)
,该节点适当地没有next
节点
你看,(c)
永远不会消失,移动的只是最后一个引用
至于为什么它是这样工作的,这一切都归结于对象是Java中的引用这一事实
考虑以下代码:
Object x = 300;
Object y = 500;
Object z = x;
//here, x = 300, y = 500, z = 300
x = y;
//now, x = 500, y = 500, z = 300
//because the actual object that x initially pointed to was not changed, only x as a reference was changed.
在列表的大小分别为n=0
和n=1
的情况下:
NULL
^first,last
NULL (a)
^first ^last
(a)
^first,last
然后是另一个电话
(a)
^first,last
(a) -> (b)
^first,last
(a) -> (b)
^first ^last
希望对else部分的代码有所帮助:
1: last.next = new Node(item);
2: last = last.next;
以下是我们将从以下内容开始:
(a) -> (b) -> (c)
^first ^last
我们称之为排队(d)
1号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
我们看到第1行在列表的末尾创建了一个新元素,但是last
仍然指向(c)
2号线运行后:
(a) -> (b) -> (c) -> (d)
^first ^last
现在,last
指向(d)
,该节点适当地没有next
节点
你看,(c)
永远不会消失,移动的只是最后一个引用
至于为什么它是这样工作的,这一切都归结于对象是Java中的引用这一事实
考虑以下代码:
Object x = 300;
Object y = 500;
Object z = x;
//here, x = 300, y = 500, z = 300
x = y;
//now, x = 500, y = 500, z = 300
//because the actual object that x initially pointed to was not changed, only x as a reference was changed.
在列表的大小分别为n=0
和n=1
的情况下:
NULL
^first,last
NULL (a)
^first ^last
(a)
^first,last
然后是另一个电话
(a)
^first,last
(a) -> (b)
^first,last
(a) -> (b)
^first ^last
希望这能有所帮助嗯,是的。它正在创建一个新节点,将其添加到列表的末尾,并将最后一个引用设置为指向该新的、最后一个节点。它正在创建一个新节点,将其添加到列表的末尾,并将最后一个引用设置为指向该新的、最后一个节点。它正在创建一个新节点,将其添加到列表的末尾,并将最后一个引用设置为指向该新的、最后一个节点。它正在创建一个新节点,将其添加到列表的末尾,并将最后一个引用设置为指向该新的最后一个节点。在您的示例中,是first。next
位于first
的左侧或右侧,并且是last.next
在last
的左侧或右侧?箭头指示节点的next
元素。这是一个很好的解释。非常感谢。与本主题相关的是,您是否可以用您在这里使用的相同方式来表示:(a)->(b)->(c)->(d)
,其中(d)
是顶部?(c)
是顶部。下一步
?我个人喜欢在这些类型的图表中显示箭头,从包含引用的对象到它引用的对象,但这只是我个人的偏好。对于堆栈,我可能会编写(a)在您的示例中,isfirst
左侧或右侧的isnext
,islast
左侧或右侧的isnext
?箭头指示节点的next
元素。这是一个很好的解释。非常感谢。与本主题相关的是,您是否可以用您在这里使用的相同方式来表示:(a)->(b)->(c)->(d)
,其中(d)
是顶部?(c)
是顶部。下一步
?我个人喜欢在这些类型的图表中显示箭头,从包含引用的对象到它引用的对象,但这只是我个人的偏好。对于堆栈,我可能会编写(a)在您的示例中,是first.next
在first
的左侧或右侧,是last.next