Ano*_*ous 4 java queue stack class linked-list
我是LinkedList类的新手,并且在如何使用它以实现或实例化堆栈和队列对象方面面临困难.我不是在寻找一段自行实现的代码.
我想知道我们如何将这个类用作堆栈和队列,并且可以使用已经定义的方法:pop,push,enqueue和dequeue或top(在堆栈的情况下).
A LinkedList已经是一个队列,因为它实现了Queue接口(并自己检查Javadoc).因此它具有以下队列操作:
排队:
add() - Appends the specified element to the end of this list.
Run Code Online (Sandbox Code Playgroud)
出队:
remove() - Retrieves and removes the head (first element) of this list.
Run Code Online (Sandbox Code Playgroud)
使用a LinkedList作为堆栈也很容易,因为它有一个方法removeLast()可以从列表的末尾删除一个项目(而不是开头,这样remove()做:
推:
add() - Appends the specified element to the end of this list.
Run Code Online (Sandbox Code Playgroud)
流行:
removeLast() - Removes and returns the last element from this list.
Run Code Online (Sandbox Code Playgroud)
始终从列表末尾追加和删除模拟堆栈,该堆栈是FIFO(先进先出)数据结构.