双向链表

原创 2012年03月29日 11:31:08
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 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

c语言实现的双向链表

  • 2016-07-31 15:45
  • 21KB
  • 下载

排序树 变成双向链表

  • 2014-09-14 20:58
  • 521B
  • 下载

剑指offer-面试题27.二叉搜索树与双向链表

题目:输入一颗二叉搜索树,将该二叉搜索树转换为一个排序的双向链表。要求不能创建任何新的结点,只能调整树种结点指针的指向。比如输入下图的二叉搜索树,则输出转换后的双向排序链表。 1 ...

双向链表——1

  • 2014-05-02 17:02
  • 524B
  • 下载

剑指offer刷题之c实现的二叉搜索树转换成双向链表

题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 参考文献:剑指offer 何海涛老师 P152 思路:将...

双向链表操作

  • 2014-10-28 11:22
  • 36KB
  • 下载

多重双向链表

  • 2015-06-18 09:13
  • 26KB
  • 下载

双向链表的简单实现

代码实现 #include #include using namespace std; typedef int DataType; struct ListNode { ListNode* _n...

双向链表的实现

  • 2015-05-16 10:40
  • 374KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)