队列_双端链表实现

//链结点
public class Link {
    public long dData;
    public Link next;
    public Link(long dd) {
        dData=dd;    
    }
    public void displayLink() {
        System.out.print(dData+" ");
    }

}
public class FirstLastList {
        public Link first;
        public Link last;
        public FirstLastList() {
            first=null;
            last=null;
        }
        //是否为空
        public boolean isEmpty() {
            return first==null;//last也行。可以选其中一个
        }
        //从last节点位置处 插入
        public void insertLast(long dd) {
            Link newLink=new Link(dd);
            if(isEmpty()) {
                //如果是空的,first节点需要指向当前插入的节点
                first=newLink;
            }else
                last.next=newLink;
            last=newLink;
                
        }
        //从first节点处 删除
        public long deleteFirst() {
            long temp=first.dData;
            if(first.next==null) {
                //如果只有一个节点,需要将last为null
                last=null;
            }
            first=first.next;//改变头(如果只有一个节点,该操作也是null)
            return temp;
            
        }
        //显示所有数据
        public void displayList() {
            Link current=first;
            while(current!=null) {
                current.displayLink();
                current=current.next;
            }
            System.out.println();
        }




}
public class LinkQueue {
    private FirstLastList theList;
    public LinkQueue() {
        theList=new FirstLastList();
    }
    public boolean isEmpty() {
        return theList.isEmpty();
    }
    public void insert(long j) {
        theList.insertLast(j);
    }
    public long remove() {
        return theList.deleteFirst();
    }
    public void displayQueue() {
        System.out.print("QUeue(front-->rear):");
        theList.displayList();
    }

}
public class Test {

    public static void main(String[] args) {
        LinkQueue theQueue =new LinkQueue();
        theQueue.insert(20);
        theQueue.insert(40);
        theQueue.displayQueue();
        theQueue.insert(60);
        theQueue.insert(80);
        theQueue.displayQueue();
        theQueue.remove();
        theQueue.remove();
        theQueue.displayQueue();

    }

}
原文地址:https://www.cnblogs.com/S-Mustard/p/8084892.html