LinkedQueue的底层实现

LinkedQueue的底层实现

package zy813ture;

public class MyLinkedQueue1 {
private Node head;

private Node last = head = new Node();//初始化可以设定默认值,形式固定

// private int size;

private class Node {
private Object data;

private Node next;
}

public MyLinkedQueue1() {
// last = head = new Node();
// last=head;
}

public boolean offer(Object element) {// 将指定的元素插入此队列
if (element == null) {
throw new NullPointerException();
}

// 1
Node node = new Node();
node.data = element;

// 2
last.next = node;

// 3
last = node;
// size++;
return true;

}

public Object peek() {// 获取不移除此队列的头,如果此队列为空,则返回 null。
if (head.next == null) {
return null;
}

return head.next.data;
}

public Object poll() {// 获取并移除此队列的头,如果此队列为空,则返回 null。
if (head.next == null) {
return null;
}

//1
Node node = head;

//2
head= head.next;

//3
//node= null;
node =null;
return head.data;
}
public static void main(String []args){
MyLinkedQueue1 me= new MyLinkedQueue1();
me.offer("ab1");
me.offer("ab2");
me.offer("ab3");
me.offer("ab4");
me.offer("ab5");
//System.out.println(me.peek());
System.out.println(me.poll());//ab1 null
System.out.println(me.poll());//
//System.out.println(me.peek());

//System.out.println(me.peek());

}

}

上一篇:Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值


下一篇:ZPush--基于netty4实现的苹果通知推送服务(APNs)Javaclient