Java数据结构——双向链表

 

class DoublyLinkedList{				//双向链表
	private Link_long first;
	private Link_long last;
	
	public DoublyLinkedList(){		//构造函数
		this.first = null;
		this.last = null;
	}
	
	public boolean isEmpty(){
		return first==null;
	}
	
	public void insertFirst(long dd){		//从链表的头开始插入
		Link_long newLink = new Link_long(dd);
		if(isEmpty()){
			last = newLink;				//不用改变first
		}else{
			first.previous = newLink;		//插入新的元素
		}
		newLink.next = first;					//插入新的元素
		first = newLink;							//修改first的位置
	}
	
	public void insertLast(long dd){		//从链表的尾开始插入
		Link_long newLink = new Link_long(dd);
		if(isEmpty()){
			first = newLink;			//不用改变last
		}else{
			last.next = newLink;		//在last后面添加新元素,并修改last的位置
			newLink.previous = last;
		}
		last = newLink;				//注意:只有一个元素的时候,插入要把last也赋为newLink
	}
	
	public Link_long deleteFirst(){				//从链表的头删除一个元素
		Link_long temp = first;		//暂存first
		if(first.next == null){			//如果只有一个元素,把last也赋为null
			last = null;
		}else{
			first.next.previous = null;
		}
		first = first.next;			//把next设为first
		return temp;				//返回原来的first
	}
	
	public Link_long deleteLast(){				//从链表的头删除一个元素
		Link_long temp = last;		//暂存last
		if(first.next == null){			//如果只有一个元素,把first也赋为null
			first = null;
		}else{
			last.previous.next = null;
		}
		last = last.previous;			//把previous设为last
		return temp;						//返回原来的last
	}
	
	public boolean insertAfter(long key,long dd){		//在特定元素后面插入
		Link_long current = first;
		while(current.dData != key){
			current = current.next;
			if(current == null){
				return false;
			}
		}
		Link_long newLink = new Link_long(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_long deleteKey(long key){		//删除指定元素
		Link_long current = first;
		while(current.dData != key){
			current = current.next;
			if(current == null){
				return null;
			}
		}
		if(current == first){		//如果第一个元素匹配,则删除,并把first向后移动
			first = current.next;
		}else{
			current.previous.next = current.next;	//改变current上一个元素的next的指向
		}
		if(current == last){
			last = current.previous;	//如果最后一个元素匹配,则删除,并把previous向前移动
		}else{
			current.next.previous = current.previous;	//改变current后一个元素的previous的指向
		}
		return current;
	}
	
	public void displayForward(){
		System.out.println("List(first-->last):");
		Link_long current = first;			//用于不断改变位置实现遍历
		while(current != null){
			current.displayLink();
			current = current.next;
		}
	}
	
	public void displayBackward(){
		System.out.println("List(last-->first):");
		Link_long current = last;			//用于不断改变位置实现遍历
		while(current != null){
			current.displayLink();
			current = current.previous;
		}
	}
	
}

public class DoublyLinked_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		DoublyLinkedList theList = new DoublyLinkedList();
		
		theList.insertFirst(40);
		theList.insertFirst(30);
		theList.insertFirst(20);
		theList.insertFirst(10);
		theList.displayForward();
		theList.displayBackward();
		
		theList.insertAfter(20, 25);
		theList.displayForward();
		theList.displayBackward();
		
		theList.deleteFirst();
		theList.displayForward();
		
		theList.deleteKey(20);
		theList.displayForward();
	}

}

 

posted @ 2016-03-31 11:02  tonglin0325  阅读(293)  评论(0编辑  收藏  举报