• 双向链表(Double-Linked List)




    public
    class doubleLinkedList <Item>{ private Node first; private Node last; private int itemcount; class Node{ Node prev; Node next; Item item; } public void addFirst(Item item){ Node oldfirst=first; first=new Node(); first.item=item; first.next=oldfirst; if(oldfirst!=null){ oldfirst.prev=first; } if(itemcount==0){ last=first; } itemcount++; } public void addlast(Item item){ Node oldlast=last; last=new Node(); last.item=item; last.prev=oldlast; if(oldlast!=null){ oldlast.next=last; } if(itemcount==0){ last=first; } itemcount++; } // public Item delfirst(){ if(first==null){ throw new NullPointerException("no node linked list"); } Item item=first.item; first=first.next; if(first!=null){ first.prev=null; } if(itemcount==1){ last=null; } itemcount--; return item; } public Item dellast(){ if(last==null){ throw new NullPointerException("no node this linked"); } Item item =last.item; last=last.prev; if(last!=null){ last.next=null; } if(itemcount==1){ first=null; } itemcount--; return item; } // public void addbefore(Item targetItem,Item item){ Node target=first; if(target==null){ throw new NullPointerException("no node this linked"); } if(target!=null && target.item!=targetItem){ target=target.next; } if(target.prev==null){ addFirst(item); } else{ Node targetprev=target.prev; Node newtarget=new Node(); newtarget.item=item; target.prev=newtarget; newtarget.next=target; newtarget.prev=targetprev; targetprev.next=newtarget; itemcount++; } } // public void addafter(Item targetItem,Item item){ Node target =first; if(target==null){ throw new NullPointerException("no node this linked!"); } if(target!=null && target.item!=targetItem){ target=target.next; } if(target.next==null){ addlast(item); } else{ Node oldtarget=target.next; Node newnode=new Node(); newnode.item=item; newnode.next=oldtarget; oldtarget.prev=newnode; target.next=newnode; newnode.prev=target; itemcount++; } } }
  • 相关阅读:
    2020.12.15
    2020.12.14
    2020.12.13
    2020.12.11
    2020.12.10
    语音合成标记语言(SSML)
    Skyline查询
    win10 VMware 安装 Linux 虚拟机
    图像梯度计算
    Harris Corner Detection
  • 原文地址:https://www.cnblogs.com/luo-mao/p/6044473.html
Copyright © 2020-2023  润新知