栈,队列是一种特殊的线性表,
栈 是 新增,删除/查找都在线性表(数组,链表)的一端,先进后出,实现类有LinkedList
队列是新增跟查找/删除在线性表(数组,链表)的两端,先进先出,实现类有LinkedList
双向队列,在线性表(数组,链表)的两端都可以删除,新增 ,java 双向队列的接口是Deque,实现类有LinkedList
因为链表对删除新增有良好的性能,所以一般底层数据结构一般是链表
LinkedList 底层的数据结构是双向链表 ,其实现了,栈,队列,双向队列
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
看LinkedList属性结构:
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
双向链表节点类型:
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
private void linkFirst(E e) {
}
void linkLast(E e) {
}
void linkBefore(E e, Node<E> succ) {
}
private E unlinkFirst(Node<E> f) {
}
/**
* Unlinks non-null last node l.
*/
private E unlinkLast(Node<E> l) {
}
E unlink(Node<E> x) {
}
public E getFirst() {
}
public E getLast() {
}
public E removeFirst() {
}
public E removeLast() {
}
public void addFirst(E e) {
linkFirst(e);
}
public void addLast(E e) {
linkLast(e);
}
public boolean contains(Object o) {
return indexOf(o) != -1;
}
public int size() {
return size;
}
public boolean add(E e) {
}
public boolean remove(Object o) {
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
}
public void clear() {
}
// Positional Access Operations
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
public E set(int index, E element) {
}
public void add(int index, E element) {
}
public E remove(int index) {
}
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
private boolean isPositionIndex(int index) {
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
private void checkElementIndex(int index) {
}
private void checkPositionIndex(int index) {
}
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
}
// Search Operations
public int indexOf(Object o) {
}
public int lastIndexOf(Object o) {
}
// Queue operations.
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E element() {
return getFirst();
}
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E remove() {
return removeFirst();
}
public boolean offer(E e) {
return add(e);
}
// Deque operations
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
public boolean offerLast(E e) {
addLast(e);
return true;
}
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
public void push(E e) {
addFirst(e);
}
public E pop() {
return removeFirst();
}
}
看其操作方法可以分为三部分:
1、实现集合Collection list接口操作,List list= new LinkedList<>();来实现一个集合,在队尾插入,插入有序
2、实现Collection queue操作,Queue queue = new LinkedList<>();来实现队列,在队尾插入,并且在队头删除/查询
3、实现Collection deque操作 实现双向队列,Deque deque = new LinkedList<>();在队头队尾都可以插入/删除/查询
也可以用Deque stack= new LinkedList<>();来实现栈,栈是在队头插入/删除/查询