# 双向链表

Java数据结构和算法中文第二版.pdf 代码
Link.java

private long dData ;

return previous;
}
this.previous = previous;
}

}
this.dData = dData ;
}

public long getdData() {
return dData;
}
public void setdData(long dData) {
this.dData = dData;
}

return next;
}
this.next = next;
}
System.out.print(dData+ " ");
}
}

first = null ;
last  = null ;
}

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

public void insertFirst(long key){
if (isEmpty()){
}else{
}
}

public void insertLast(long key){
if (isEmpty()){
}else{
}
}

if (! isEmpty()) {
temp = first ;
if (first.getNext() == null){
last  = null ;
}else{
first.getNext().setPrevious(null) ;
}
first = first.getNext();
}

return temp ;
}

if (!isEmpty()){
temp = last ;
if (first.getNext() == null){
first = null ;
}else{
last.getPrevious().setNext(null) ;
}
last = last.getPrevious() ;
}
return temp ;
}

public boolean insertAfter(long key , long value){

if (isEmpty()){
return false ;
}
while(current.getdData() != key){

current = current.getNext() ;
if(null == current){
return false ;
}
}

if (current == last){
}else{
}

return true ;
}

if (isEmpty()){
return null ;
}
while(current.getdData() != key){
current = current.getNext() ;
if (current == null){
return null ;
}
}

if (current == first){
first.getNext().setPrevious(null) ;
}else{
current.getPrevious().setNext(current.getNext()) ;
}
if (current == last){
last = current.getPrevious() ;
last.setNext(null) ;
}else{
current.getNext().setPrevious(current.getPrevious()) ;
}

return current  ;
}

public void displayForward(){
System.out.println("List (first --> last):") ;
while(null != current){
current = current.getNext() ;
}
System.out.println(" ");
}

public void displayBackward(){
System.out.println("List (last --> first):") ;
while (null != current){
current = current.getPrevious() ;
}

System.out.println(" ");
}

}

/**
* @param args
*/
public static void main(String[] args) {

theList.insertFirst(22) ;
theList.insertFirst(44) ;
theList.insertFirst(66) ;
theList.insertFirst(88) ;
theList.displayForward() ;
theList.displayBackward() ;

theList.insertLast(99);
theList.insertLast(100);
theList.insertLast(111);

theList.displayForward() ;
theList.displayBackward() ;

theList.insertAfter(22, 33) ;
theList.insertAfter(44, 55) ;
theList.displayForward() ;
theList.displayBackward() ;

theList.deleteFirst() ;
theList.deleteLast() ;
theList.deleteKey(22) ;
theList.displayForward() ;
theList.displayBackward() ;
}

}


List (first --> last):
88 66 44 22
List (last --> first):
22 44 66 88
List (first --> last):
88 66 44 22 99 100 111
List (last --> first):
111 100 99 22 44 66 88
List (first --> last):
88 66 44 55 22 33 99 100 111
List (last --> first):
111 100 99 33 22 55 44 66 88
List (first --> last):
66 44 55 33 99 100
List (last --> first):
100 99 33 55 44 66

• 本文已收录于以下专栏：

举报原因： 您举报文章：双向链表 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)