链队列java实现

来源:互联网 发布:p2p理财 知乎 编辑:程序博客网 时间:2024/05/16 13:05
public class LinkHeap<T>{    class Node<T>    {        T data;        Node<T> next;        Node(T data)        {               this.data = data;            this.next = null;        }    }    Node<T> font ;    Node<T> tail;    public LinkHeap()    {        this.font = null;        this.tail = null;    }    public boolean isEmpty()    {        return this.font==null && this.tail==null;    }       public void enQueue(T e)    {        if(this.isEmpty())         {            Node temp = new Node(e);            this.font = temp;            this.tail = this.font;        }        else        {            Node temp = new Node(e);            this.tail.next = temp;            this.tail = temp;        }    }    public Object deQueue()    {        if(this.isEmpty()) return null;        if(this.font==this.tail)         {            Node temp = this.font;            this.font = null;            this.tail = null;            return temp.data;        }        else        {            Node temp= this.font;            this.font  = temp.next;            return temp.data;        }    }    public static void main(String[] args)    {        LinkHeap<Integer> mLinkHeap = new LinkHeap<Integer>();        for(int i=0;i<10;i++)            mLinkHeap.enQueue(new Integer(i+1));        while(mLinkHeap.isEmpty()==false)        {            System.out.print(mLinkHeap.deQueue()+"\t");        }       }}
0 0
原创粉丝点击