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、push、enqueue 和 dequeue 或 top(在堆栈的情况下).
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
接口(并检查 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.
在列表末尾添加和删除总是模拟一个堆栈,这是一个 LIFO(后进先出)数据结构.
Appending and removing always from the end of the list simulates a stack, which is a LIFO (Last in first out) data structure.
这篇关于Java:LinkedList 类作为堆栈和队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!