码迷,mamicode.com
首页 > 其他好文 > 详细

实现一个基于双向链表的双端队列

时间:2015-01-30 17:13:10      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

package day1_30_5_2;

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指针直接指向新创建的节点
			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(){
		
		Link temp = first;
		if(first.next == null){//如果只有一个元素
			last = null;
		}
		else
			first.next.previous = null;
		    first = first.next;
		    return temp;
		
	}
	
	//删除最后一个节点的实现
	public Link deleteLast(){
		Link temp = last;
		if (first.next == null) // if only one item  
            first = null; // first --> null  
        else  
            last.previous.next = null; // old previous --> null  
        last = last.previous; // old previous <-- last  
        return temp;
	}
	
	//在某个元素的后面添加一个元素,insert dd after key
	public boolean insertAfter(long key,long dd){
		Link current = first;
		
		while(current.dData != key){
			current = current.next;
			if(current == null){
				return false;
			}
		}
		//创建一个新的节点
		Link newLink = new Link(dd);
		//current  在last
		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; // start at beginning  
	        while (current.dData != key) // until match is found,  
	        {  
	            current = current.next; // move to next link  
	            if (current == null)  
	                return null; // didn‘t find it  
	        }  
	        if (current == first) // found it; first item?  
	            first = current.next; // first --> old next  
	        else  
	            // not first  
	            // old previous --> old next  
	            current.previous.next = current.next;  
	  
	        if (current == last) // last item?  
	            last = current.previous; // old previous <-- last  
	        else  
	            // not last  
	            // old previous <-- old next  
	            current.next.previous = current.previous;  
	        return current; // return value  
		
	}
	
	
	public void displayForward() {  
        System.out.print("List (first-->last): ");  
        Link current = first; // start at beginning  
        while (current != null) // until end of list,  
        {  
            current.displayLink(); // display data  
            current = current.next; // move to next link  
        }  
        System.out.println("");  
    }  
  
    // -------------------------------------------------------------  
    public void displayBackward() {  
        System.out.print("List (last-->first): ");  
        Link current = last; // start at end  
        while (current != null) // until start of list,  
        {  
            current.displayLink(); // display data  
            current = current.previous; // move to previous link  
        }  
        System.out.println("");  
    }  
	
	
	
	
	
}

  

实现一个基于双向链表的双端队列

标签:

原文地址:http://www.cnblogs.com/aicpcode/p/4262635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!