Java:LinkedList类作为堆栈和队列 [英] Java: LinkedList class as stack and queues
问题描述
我是LinkedList类的新手,并且在如何使用它来实现或实例化堆栈和队列对象方面遇到困难。我不是在寻找一段自我实现的代码。
I am new to LinkedList class, and facing difficulties as to how to use it in order to implement or instantiate stack and queues object. I am not looking for piece of self-implemented codes.
我想知道我们如何将此类用作堆栈和队列,并可以使用已经定义的方法:pop
I wanted to know how do we use this class as stack and queues and can use the already defined methods: pop,push,enqueue and dequeue or top (in case of stacks).
推荐答案
队列
A LinkedList
已经是一个队列,因为它实现了 Queue
接口(并检查< a href = https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html rel = noreferrer> Javadoc 自己)。因此,它具有以下队列操作:
Queue
A LinkedList
is already a queue, since it implements the Queue
interface (and check the Javadoc yourself). Hence it has the following queue operations:
排队:
add() - Appends the specified element to the end of this list.
出队:
remove() - Retrieves and removes the head (first element) of this list.
堆栈
这也很容易使用 LinkedList
作为堆栈,因为它具有方法 removeLast()
,该方法可以从结尾删除项目列表中的(而不是开始, remove()
会这样做:
Stack
It is also very easy to use a LinkedList
as a stack, since it has a method removeLast()
which can remove an item from the end of the list (rather than the start, which remove()
does:
推送:
add() - Appends the specified element to the end of this list.
流行音乐:
removeLast() - Removes and returns the last element from this list.
始终附加并删除从列表末尾开始模拟一个堆栈,该堆栈是一个FIFO(先进先出)数据结构。
Appending and removing always from the end of the list simulates a stack, which is a FIFO (first in first out) data structure.
这篇关于Java:LinkedList类作为堆栈和队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!