class Node<E>{
Node <E> next=null;
E data;
public Node(E data) {
this.data=data;
}
}
public class myQueue<E> {
private Node<E> head=null;
private Node<E> tail=null;
public boolean isEmpty() {
return head==null&&tail==null;
}
//put() 队尾入队
public void put(E data) {
Node <E> newNode=new Node<E>(data);
if(head==null&&tail==null)
head=tail=newNode;
else {
tail.next=newNode;
tail=newNode;
}
}
//pop() 队头出队
public E pop() {
if(this.isEmpty()) {
return null;
}else {
E tmp=head.data;
head=head.next;
return tmp;
}
}
//size()
public int size() {
Node<E> tmp=head;
int n=0;
while(tmp!=null) {
n++;
tmp=tmp.next;
}
return n;
}
}
Java:如何用链表实现队列
最新推荐文章于 2024-05-16 19:23:34 发布