双向链表

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

package com.ch5.doublyLinked;

public class Link {

	private long dData ;
	private Link next ;
	private Link previous ;
	
	public Link getPrevious() {
		return previous;
	}
	public void setPrevious(Link previous) {
		this.previous = previous;
	}
	
	
	public Link(){
		
	}
	public Link(long dData){
		this.dData = dData ;
	}
	
	public long getdData() {
		return dData;
	}
	public void setdData(long dData) {
		this.dData = dData;
	}

	
	
	public Link getNext() {
		return next;
	}
	public void setNext(Link next) {
		this.next = next;
	}
	public void displayLink(){
		System.out.print(dData+ " ");
	}
}


DoublyLinkedList .java

package com.ch5.doublyLinked;

public class DoublyLinkedList {

	private Link first ;
	private Link last ;
	
	public DoublyLinkedList(){
		first = null ;
		last  = null ;
	}
	
	public boolean isEmpty(){
		return (first == null) ;
	}
	
	public void insertFirst(long key){
		Link newLink = new Link(key) ;
		if (isEmpty()){
			last = newLink ;
		}else{
			first.setPrevious(newLink) ;
		}
		newLink.setNext(first) ;
		first = newLink ;
	}
	
	public void insertLast(long key){
		Link newLink = new Link(key) ;
		if (isEmpty()){
			first = newLink ;
		}else{
			last.setNext(newLink) ;
			newLink.setPrevious(last) ;
		}
		last = newLink ;
	}
	
	
	public Link deleteFirst(){
		Link temp = null ;
		
		if (! isEmpty()) {
			temp = first ;
			if (first.getNext() == null){
				last  = null ;
			}else{
				first.getNext().setPrevious(null) ;
			}
			first = first.getNext();
		}
		
		return temp ;
	}
	
	public Link deleteLast(){
		
		Link temp = null ;
		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 ;
		}
		Link current = first ;
		while(current.getdData() != key){
			
			current = current.getNext() ;
			if(null == current){
				return false ;
			}
		}
		Link newLink = new Link(value) ;
		
		if (current == last){
			newLink.setNext(null) ;
			last = newLink ;
		}else{
			newLink.setNext(current.getNext()) ;
			current.getNext().setPrevious(newLink) ;
		}
		
		current.setNext(newLink) ;
		newLink.setPrevious(current) ;
		return true ;
	}
	
	public Link deleteKey(long key){
		if (isEmpty()){
			return null ;
		}
		Link current = first ;
		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):") ;
		Link current = first ;
		while(null != current){
			current.displayLink() ;
			current = current.getNext() ;
		}
		System.out.println(" ");
	}
	
	public void displayBackward(){
		System.out.println("List (last --> first):") ;
		Link current = last ;
		while (null != current){
			current.displayLink() ;
			current = current.getPrevious() ;
		}
		
		System.out.println(" ");
	}
		
}


DoublyLinkedApp.java

package com.ch5.doublyLinked;

public class DoublyLinkedApp {

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

		DoublyLinkedList theList = new DoublyLinkedList() ;
		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 

你可能感兴趣的:(数据结构,算法,list,String,null,Class)