Node类
public class Node {
public T data;
Node next;
public Node (T d)
{
this.data=d;
}
}
Queue类
public class Queue {
private Node font;
private Node rear;
private int size=0;
public void push(T data)
{
Node node=new Node(data);
if (size==0)
{
this.rear=node;
this.font=node;
}
else
{
this.rear.next=node;
this.rear=node;
}
size++;
}
public Node pop()
{
if (size==0)
{
return null;
}
else
{
Node pre=this.font;
this.font=this.font.next;
size--;
return pre;
}
}
public int getSize()
{
return this.size;
}
public Node getFont()
{
return this.font;
}
public Node getRear()
{
return this.rear;
}
}
测试类
public class QueueTest {
public static void main(String[] args)
{
Queue qe=new Queue();
qe.push(1);
qe.push(2);
qe.push(3);
qe.push(4);
while(qe.getFont()!=null)
{
System.out.println("size: "+qe.getSize());
System.out.println("font: "+qe.getFont().data);
System.out.println(qe.pop().data);
}
}
}