java实现队列(链表方式)

来源:互联网 发布:java 多重继承 编辑:程序博客网 时间:2024/05/17 02:56

Node类

public class Node<T> {public T data;Node<T> next;public Node (T d){this.data=d;}}


Queue类

public class Queue<T> {private Node<T> font;private Node<T> rear;private int size=0;public void push(T data){Node<T> node=new Node<T>(data);if (size==0){this.rear=node;this.font=node;}else {this.rear.next=node;this.rear=node;}size++;} public Node<T> pop() { if (size==0) { return null; } else  { Node<T> pre=this.font; this.font=this.font.next; size--; return pre; } } public int getSize() { return this.size; }  public Node<T> getFont() { return this.font; } public Node<T> getRear() { return this.rear; } }


测试类

public class QueueTest { public static void main(String[] args) { Queue<Integer> qe=new Queue<Integer>(); 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); } }}


 

原创粉丝点击