双向链表
2016-03-10 15:43
253 查看
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 DoubleLinkedApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DoubleLinkedList theList = new DoubleLinkedList(); theList.insertFirst(22); theList.insertFirst(44); theList.insertFirst(66); theList.insertLast(11); theList.insertLast(33); theList.insertLast(55); theList.displayForward(); theList.displayBackward(); theList.deleteFirst(); theList.deleteLast(); theList.displayForward(); theList.insertAfter(22, 77); theList.insertAfter(33, 88); theList.displayForward(); } }
相关文章推荐
- java数据结构之实现双向链表的示例
- C++将二叉树转为双向链表及判断两个链表是否相交
- C#双向链表LinkedList排序实现方法
- C语言实现双向链表
- PHP小教程之实现双向链表
- Java实现双向链表(两个版本)
- java中使用双向链表实现贪吃蛇程序源码分享
- 数据结构(2)-双向链表的实现
- 通用双向链表(一)——接口设计
- 通用双向链表(二)———接口实现
- 循环链表与双向链表
- 双向链表
- 数据结构与算法之链表4---双向链表
- 双向链表实现约瑟夫双向生死游戏
- Java数据结构-线性表之双向链表
- SICILY 1934 移动小球
- 链表模块
- LinuxC双向链表的各种操作
- 数据结构之双向循环链表操作4-(插入,删除,建立等)
- bzoj3658 树状数组+双向链表