Java实现:
static class Node{ Object item; Node next; public Node(Object item, Node next) { this.item = item; this.next = next; } }
其实就创建一个静态内部类即可,类中item是用来存储数据的,next是指向下一个node节点,最后一个节点的next为空。
二、属性
head:链表表头,拉取或遍历数据都是从这里开始的。
tail:链表表尾,每次添加数据都添加到tail的后面,变成新的tail
size:队列长度
//表头 private Node head; //表尾 private Node tail; //队列长度 private int size;
三、添加数据
public void offer(Object item){ Node n = new Node(item,null); if (tail == null){ head = tail = n; }else { tail.next = n; tail = n; } size++; }
用代码实现队列的添加操作看起是很简单的,无非就是将新节点添加到表尾,然后把新节点设置成新的表尾。
四、拉取数据
public Object poll(){ if (head == null) return null; Node h = head; //将拉取的节点的下一个节点变成新的表头 head = head.next; //把旧的表头的下一个节点指向设置为null,让gc回收 h.next = null

