java常用作栈结构的类:
oracle官网文档:
下列是java常用作栈结构的类与接口
ArrayDeque (Java Platform SE 8 ) (oracle.com)
Queue (Java Platform SE 8 ) (oracle.com)
Deque (Java Platform SE 8 ) (oracle.com)
LinkedList (Java Platform SE 8 ) (oracle.com)
Stack (Java Platform SE 8 ) (oracle.com)
相关方法介绍:
(15条消息) java集合超详解_phial03的博客-优快云博客_java集合
(15条消息) JAVA队列( Queue ) 详解_java叶新东老师的博客-优快云博客_java queue
Interface Queue<E>
-
Type Parameters:
E
- the type of elements held in this collectionAll Superinterfaces:
Collection<E>, Iterable<E>
All Known Subinterfaces:
BlockingDeque<E>, BlockingQueue<E>, Deque<E>, TransferQueue<E>
All Known Implementing Classes:
AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue, SynchronousQueue
Modifier and Type | Method and Description |
---|---|
boolean | add(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning upon success and throwing an if no space is currently available. true IllegalStateException
|
E | element()
Retrieves, but does not remove, the head of this queue.
|
boolean | offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
|
E | peek()
Retrieves, but does not remove, the head of this queue, or returns if this queue is empty. null
|
E | poll()
Retrieves and removes the head of this queue, or returns if this queue is empty. null
|
E | remove()
Retrieves and removes the head of this queue.
|
Interface Deque<E>
-
Type Parameters:
E
- the type of elements held in this collectionAll Superinterfaces:
Collection<E>, Iterable<E>, Queue<E>
All Known Subinterfaces:
All Known Implementing Classes:
ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque, LinkedList
Modifier and Type | Method and Description |
---|---|
boolean | add(E e) Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning |
void | addFirst(E e) Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an |
void | addLast(E e) Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an |
boolean | contains(Object o) Returns |
E | getFirst() Retrieves, but does not remove, the first element of this deque. |
E | getLast() Retrieves, but does not remove, the last element of this deque. |
E | peek() Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns |
E | peekFirst() Retrieves, but does not remove, the first element of this deque, or returns |
E | peekLast() Retrieves, but does not remove, the last element of this deque, or returns |
E | poll() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns |
E | pollFirst() Retrieves and removes the first element of this deque, or returns |
E | pollLast() Retrieves and removes the last element of this deque, or returns |
E | remove() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). |
Class LinkedList<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.AbstractSequentialList<E>
-
- java.util.LinkedList<E>
-
Type Parameters:
E
- the type of elements held in this collectionAll Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>
Modifier and Type | Method and Description |
---|---|
boolean | add(E e) Appends the specified element to the end of this list. |
void | add(int index, E element) Inserts the specified element at the specified position in this list. |
boolean | addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. |
boolean | addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list, starting at the specified position. |
void | addFirst(E e) Inserts the specified element at the beginning of this list. |
void | addLast(E e) Appends the specified element to the end of this list. |
void | clear() Removes all of the elements from this list. |
E | get(int index) Returns the element at the specified position in this list. |
E | getFirst() Returns the first element in this list. |
E | getLast() Returns the last element in this list. |
E | peek() Retrieves, but does not remove, the head (first element) of this list. |
E | peekFirst() Retrieves, but does not remove, the first element of this list, or returns |
E | peekLast() Retrieves, but does not remove, the last element of this list, or returns |
E | poll() Retrieves and removes the head (first element) of this list. |
E | pollFirst() Retrieves and removes the first element of this list, or returns |
E | pollLast() Retrieves and removes the last element of this list, or returns |
E | pop() Pops an element from the stack represented by this list. |
void | push(E e) Pushes an element onto the stack represented by this list. |
E | remove() Retrieves and removes the head (first element) of this list. |
上述所列方法只提供了集中比较常用的方法,详细方法请前往oracle进行查看。
栈与递归leetcode算法题:
栈与计算器
逆波兰表达式介绍:
逆波兰表达式是一种后缀表达式,所谓后缀就是指算符写在后面。
逆波兰表达式计算求值:
如果遇到操作数,则将操作数入栈;
如果遇到运算符,则将两个操作数出栈,其中先出栈的是右操作数,后出栈的是左操作数,使用运算符对两个操作数进行运算,将运算得到的新操作数入栈。
整个逆波兰表达式遍历完毕之后,栈内只有一个元素,该元素即为逆波兰表达式的值。
递归与分治
将整个问题分解成若干小问题后再分而治之。如果分解得到的子问题相对来说还是太大,则可反复使用分治策略将这些子问题分成更小的同类型子问题,直至产生方便求解的子问题,必要时逐步合并这些子问题的解,从而得到问题的解。
分治算法可以由递归过程来表示,因为分治法就是一种找大规模问题与小规模问题关系的方法,是递归设计的一种具体策略。