【Java】队列和栈

可以使用双向链表进行设计一个队列,也可以使用单向链表设计一个队列

双向链表

可以实现成为双端队列,即一个队列的队头也可以成为这个队列的队尾。

单向链表

如果使用头插,尾删的方法(头删,尾插)可以实现

方法中的异同:

add()

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add, which can fail to insert an element only by throwing an exception.

立即插入元素,如果容量满,抛出异常,

offer()

Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

返回队头元素并将其删除,如果队列为空将抛出异常。

remove()

Retrieves and removes the head of this queue, or returns null if this queue is empty.

返回队头元素并将其删除,如果队列为空则返回null。

poll()

Retrieves, but does not remove, the head of this queue. This method differs from peek only in that it throws an exception if this queue is empty.

返回队头元素,如果队列为空将抛出异常。

element()

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

返回队头元素,如果队列为空将抛出异常。

peek()

返回队头元素

你可能感兴趣的:(java,开发语言)