基于双向链表的双端队列

public class Link {
    public long dData;
    public Link next;
    public Link previous;

    public Link(long dData){
        this.dData=dData;
    }

    public void displayLink(){
        System.out.print(dData+" ");
    }
}
public class DoubleLinkedList {
    private Link first;
    private Link last;

    public DoubleLinkedList(){
        first=null;
        last=null;
    }

    public boolean isEmpty(){
        return (first==null);
    }

    public void insertFirst(long dd){
        Link newLink = new Link(dd);
        if(isEmpty()){
            last=newLink;
        }else{
            first.previous=newLink;
        }
        newLink.next=first;
        first=newLink;
    }

    public void insertLast(long dd){
        Link newLink=new Link(dd);
        if(isEmpty()){
            first=newLink;
        }
        else{
            last.next=newLink;
            newLink.previous=last;
        }
        last=newLink;
    }

    public Link deleteFirst(){//delete first link
        Link temp =first;
        if(first.next==null){//if only one items
            last=null;
        }else{
            first.next.previous=null;
        }
        first=first.next;
        return temp;
    }

    public Link deleteLast(){//delete last link
        Link temp =last;
        if(first.next==null){//if only one items
            first=null;
        }else{
            last.previous.next=null;
        }
        last=last.previous;
        return temp;
    }

    public boolean insertAfter(long key,long dd){
        Link current =first;
        while(current.dData!=key){
            current=current.next;
            if(current==null){
                return false;//didn't find it 
            }
        }
        Link newLink = new Link(dd);
        if(current==last){
            newLink.next=null;
            last=newLink;
        }
        else{
            newLink.next=current.next;
            current.next.previous=newLink;
        }
        newLink.previous=current;
        current.next=newLink;
        return true;
    }

    public Link deleteKey(long key){
        Link current =first;
        while(current.dData!=key){
            current=current.next;
            if(current==null)
                return null;
        }
        if(current==first){
            first=current.next;
        }
        else{
            current.previous.next=current.next;
        }
        if(current==last){
            last=current.previous;
        }
        else{
            current.next.previous=current.previous;
        }
        return current;
    }

    public void displayForward(){
        Link current=first;
        while(current!=null){
            current.displayLink();
            current=current.next;
        }
        System.out.println("");
    }
    //遍历
    public void displayBackward(){
        Link current = last;//start at end
        while(current!=null){
            current.displayLink();
            current=current.previous;
        }
        System.out.println("");
    }
    //insertAfter在某一特定元素后面插入
}
public class DuQueue {
    private DoubleLinkedList doubleLinkedList;

    public DuQueue(){
        doubleLinkedList=new DoubleLinkedList();
    }

    public void insertLeft(long value){
        doubleLinkedList.insertFirst(value);
    }

    public void insertRight(long value){
        doubleLinkedList.insertLast(value);
    }

    public long removeLeft(){
        return doubleLinkedList.deleteFirst().dData;
    }

    public long removeRight(){
        return doubleLinkedList.deleteLast().dData;
    }
    public boolean isEmpty(){
        return doubleLinkedList.isEmpty();
    }

    public void display(){
        doubleLinkedList.displayForward();
    }
}
public class DuQueueApp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DuQueue theQueue = new DuQueue();
        theQueue.insertRight(10);
        theQueue.insertRight(20);
        theQueue.insertRight(30);
        theQueue.insertRight(40);
        theQueue.display();
        System.out.println("移除"+theQueue.removeLeft());
        System.out.println("移除"+theQueue.removeLeft());
        System.out.println("移除"+theQueue.removeLeft());
        theQueue.display();
        theQueue.insertLeft(50);
        theQueue.insertLeft(60);
        theQueue.insertLeft(70);
        theQueue.insertLeft(80);
        theQueue.display();
        theQueue.removeRight();
        theQueue.removeRight();
        theQueue.removeRight();
        theQueue.display();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值