链表


  在第一章的数组中,我们看到数组作为数据存储结构有一定的缺陷。在无序数组中,搜索时低效的;而在有序数组中,插入效率又很低;不管在哪一种数组中删除效率都很低。况且一个数组创建后,它的大小是无法改变的。

        在本章中,我们将讨论下链表这个数据结构,它可以解决上面的一些问题。链表可能是继数组之后第二种使用得最广泛的通用数据结构了。本章主要讨论单链表和双向链表。

        顾名思义,单链表只能从表头到表尾的顺序,每个节点中保存了指向下一个节点的指针;双向链表则可以反向遍历,因为节点中既保存了向后节点的指针,又保存了向前节点的指针。由于链表结构相对简单,这里不再赘述,直接通过程序来查看它们常见的操作:

    单链表:

  1. public class LinkedList {  
  2.     private Link first;  
  3.     private int nElem; //链表中节点数量  
  4.     public LinkedList() {  
  5.         first = null;  
  6.         nElem = 0;  
  7.     }  
  8.       
  9.     public void insertFirst(int value) {//添加头结点  
  10.         Link newLink = new Link(value);  
  11.         newLink.next = first; //newLink-->old first  
  12.         first = newLink; //first-->newLink  
  13.         nElem ++;  
  14.     }  
  15.       
  16.     public Link deleteFirst() { //删除头结点  
  17.         if(isEmpty()) {  
  18.             System.out.println("链表为空!");  
  19.             return null;  
  20.         }  
  21.         Link temp = first;  
  22.         first = first.next;  
  23.         nElem --;  
  24.         return temp;  
  25.     }  
  26.   
  27.     /************************************************************ 
  28.      ***下面是有序链表的插入*** 
  29.      ***这样简单排序就可以用链表来实现,复杂度为O(N) 
  30.      ***定义一个方法,传入一个数组, 
  31.      ***在方法内,遍历数组并调用insert方法就可以将数组中的数据排好序 
  32.      ***********************************************************/  
  33.     public void insert(int value) {  
  34.         Link newLink = new Link(value);  
  35.         Link previous = null;  
  36.         Link current = first;  
  37.         while(current != null && value > current.data) {  
  38.             previous = current;  
  39.             current = current.next;  
  40.         }  
  41.         if(previous == null) {  
  42.             first = newLink;          
  43.         }  
  44.         else {  
  45.             previous.next = newLink;  
  46.         }  
  47.         newLink.next = current;  
  48.         nElem ++;  
  49.     }  
  50.       
  51.     public Link find(int value) { //查找特定的节点  
  52.         Link current = first;  
  53.         while(current.data != value) {  
  54.             if(current.next == null)   
  55.                 return null;  
  56.             else  
  57.                 current = current.next;  
  58.         }     
  59.         return current;  
  60.     }  
  61.       
  62.     public Link delete(int value) { //删除特定的节点  
  63.         Link current = first;  
  64.         Link previous = first;  
  65.         while(current.data != value) {  
  66.             if(current.next == null) {  
  67.                 return null;  
  68.             }  
  69.             previous = current;  
  70.             current = current.next;  
  71.         }  
  72.         if(current == first) {  
  73.             first = first.next;  
  74.         }  
  75.         else {  
  76.             previous.next = current.next;  
  77.         }  
  78.         nElem --;  
  79.         return current;  
  80.     }  
  81.       
  82.     public void displayList() {  
  83.         if(isEmpty()){  
  84.             System.out.println("链表为空!");  
  85.             return;  
  86.         }  
  87.         Link current = first;  
  88.         while(current != null) {  
  89.             current.displayLink();  
  90.             current = current.next;  
  91.         }  
  92.         System.out.println(" ");  
  93.     }  
  94.       
  95.     public boolean isEmpty() {  
  96.         return (first == null);  
  97.     }  
  98.       
  99.     public int size() {  
  100.         return nElem;  
  101.     }  
  102. }  
  103.   
  104. class Link {  
  105.     public int data;  
  106.     public Link next;  
  107.       
  108.     public Link(int data) {  
  109.         this.data = data;  
  110.         this.next = null;  
  111.     }  
  112.       
  113.     public void displayLink() {  
  114.         System.out.print("{" + data + "} ");  
  115.     }  
  116. }  
public class LinkedList {
	private Link first;
	private int nElem; //链表中节点数量
	public LinkedList() {
		first = null;
		nElem = 0;
	}
	
	public void insertFirst(int value) {//添加头结点
		Link newLink = new Link(value);
		newLink.next = first; //newLink-->old first
		first = newLink; //first-->newLink
		nElem ++;
	}
	
	public Link deleteFirst() { //删除头结点
		if(isEmpty()) {
			System.out.println("链表为空!");
			return null;
		}
		Link temp = first;
		first = first.next;
		nElem --;
		return temp;
	}

	/************************************************************
	 ***下面是有序链表的插入***
	 ***这样简单排序就可以用链表来实现,复杂度为O(N)
	 ***定义一个方法,传入一个数组,
	 ***在方法内,遍历数组并调用insert方法就可以将数组中的数据排好序
	 ***********************************************************/
	public void insert(int value) {
		Link newLink = new Link(value);
		Link previous = null;
		Link current = first;
		while(current != null && value > current.data) {
			previous = current;
			current = current.next;
		}
		if(previous == null) {
			first = newLink;		
		}
		else {
			previous.next = newLink;
		}
		newLink.next = current;
		nElem ++;
	}
	
	public Link find(int value) { //查找特定的节点
		Link current = first;
		while(current.data != value) {
			if(current.next == null) 
				return null;
			else
				current = current.next;
		}	
		return current;
	}
	
	public Link delete(int value) { //删除特定的节点
		Link current = first;
		Link previous = first;
		while(current.data != value) {
			if(current.next == null) {
				return null;
			}
			previous = current;
			current = current.next;
		}
		if(current == first) {
			first = first.next;
		}
		else {
			previous.next = current.next;
		}
		nElem --;
		return current;
	}
	
	public void displayList() {
		if(isEmpty()){
			System.out.println("链表为空!");
			return;
		}
		Link current = first;
		while(current != null) {
			current.displayLink();
			current = current.next;
		}
		System.out.println(" ");
	}
	
	public boolean isEmpty() {
		return (first == null);
	}
	
	public int size() {
		return nElem;
	}
}

class Link {
	public int data;
	public Link next;
	
	public Link(int data) {
		this.data = data;
		this.next = null;
	}
	
	public void displayLink() {
		System.out.print("{" + data + "} ");
	}
}

    双向链表:

  1. public class DoublyLinkedList {  
  2.     private Node first; //头节点  
  3.     private Node last; //尾节点  
  4.     private int size;  
  5.       
  6.     public DoublyLinkedList() {  
  7.         first = null;  
  8.         last = null;  
  9.         size = 0;  
  10.     }  
  11.       
  12.     public int size() {  
  13.         return size;  
  14.     }  
  15.       
  16.     public boolean isEmpty() {  
  17.         return (first == null);  
  18.     }  
  19.       
  20.     public void insertFirst(long value) { //插入头节点  
  21.         Node newLink = new Node(value);  
  22.         if(isEmpty()) {  
  23.             last = newLink;  
  24.         }  
  25.         else{  
  26.             first.previous = newLink; //newLink <-- oldFirst.previous  
  27.             newLink.next = first; //newLink.next --> first  
  28.         }   
  29.         first = newLink; //first --> newLink  
  30.         size ++;  
  31.     }  
  32.       
  33.     public void insertLast(long value) {//插入尾节点  
  34.         Node newLink = new Node(value);  
  35.         if(isEmpty()){  
  36.             first = newLink;  
  37.         }  
  38.         else {  
  39.             last.next = newLink; //newLink <-- oldLast.next  
  40.             newLink.previous = last; //newLink.previous --> last  
  41.         }  
  42.         last = newLink;  
  43.         size ++;  
  44.     }  
  45.       
  46.     public Node deleteFirst() {//删除头结点  
  47.         if(first == null) {  
  48.             System.out.println("链表为空!");  
  49.             return null;  
  50.         }  
  51.         Node temp = first;  
  52.         if(first.next == null) { //只有一个节点  
  53.             last = null;  
  54.         }  
  55.         else {  
  56.             first.next.previous = null;  
  57.         }  
  58.         first = first.next;  
  59.         size --;  
  60.         return temp;  
  61.     }  
  62.       
  63.     public Node deleteLast() {//删除尾节点  
  64.         if(last == null) {  
  65.             System.out.println("链表为空");  
  66.             return null;  
  67.         }  
  68.         Node temp = last;  
  69.         if(last.previous == null) { //只有一个节点  
  70.             first = null;  
  71.         }  
  72.         else {  
  73.             last.previous.next = null;  
  74.         }  
  75.         last = last.previous;  
  76.         size --;  
  77.         return temp;  
  78.     }  
  79.       
  80.     public boolean insertAfter(long key, long value) { //在key后面插入值为value的新节点  
  81.         Node current = first;  
  82.         while(current.data != key) {  
  83.             current = current.next;  
  84.             if(current == null) {  
  85.                 System.out.println("不存在值为" + value + "的节点!");  
  86.                 return false;  
  87.             }  
  88.         }  
  89.         if(current == last) {  
  90.             insertLast(value);  
  91.         }  
  92.         else {  
  93.             Node newLink = new Node(value);  
  94.             newLink.next = current.next;  
  95.             current.next.previous = newLink;  
  96.             newLink.previous = current;  
  97.             current.next = newLink;  
  98.             size ++;  
  99.         }  
  100.         return true;  
  101.     }  
  102.       
  103.     public Node deleteNode(long key) {//删除指定位置的节点  
  104.         Node current = first;  
  105.         while(current.data != key) {  
  106.             current = current.next;  
  107.             if(current == null) {  
  108.                 System.out.println("不存在该节点!");  
  109.                 return null;  
  110.             }  
  111.         }  
  112.         if(current == first) {  
  113.             deleteFirst();  
  114.         }  
  115.         else if(current == last){  
  116.             deleteLast();  
  117.         }  
  118.         else {  
  119.             current.previous.next = current.next;  
  120.             current.next.previous = current.previous;  
  121.             size --;  
  122.         }  
  123.         return current;  
  124.     }  
  125.       
  126.     public void displayForward() { //从头到尾遍历链表  
  127.         System.out.println("List(first --> last): ");  
  128.         Node current = first;  
  129.         while(current != null) {  
  130.             current.displayLink();  
  131.             current = current.next;  
  132.         }  
  133.         System.out.println(" ");  
  134.     }  
  135.       
  136.     public void displayBackward() { //从尾到头遍历链表  
  137.         System.out.println("List(last --> first): ");  
  138.         Node current = last;  
  139.         while(current != null) {  
  140.             current.displayLink();  
  141.             current = current.previous;  
  142.         }  
  143.         System.out.println(" ");  
  144.     }  
  145. }  
  146.   
  147. class Node {  
  148.     public long data;  
  149.     public Node next;  
  150.     public Node previous;  
  151.       
  152.     public Node(long value) {  
  153.         data = value;  
  154.         next = null;  
  155.         previous = null;  
  156.     }  
  157.       
  158.     public void displayLink() {  
  159.         System.out.print(data + " ");  
  160.     }  
  161. }  
public class DoublyLinkedList {
	private Node first; //头节点
	private Node last; //尾节点
	private int size;
	
	public DoublyLinkedList() {
		first = null;
		last = null;
		size = 0;
	}
	
	public int size() {
		return size;
	}
	
	public boolean isEmpty() {
		return (first == null);
	}
	
	public void insertFirst(long value) { //插入头节点
		Node newLink = new Node(value);
		if(isEmpty()) {
			last = newLink;
		}
		else{
			first.previous = newLink; //newLink <-- oldFirst.previous
			newLink.next = first; //newLink.next --> first
		} 
		first = newLink; //first --> newLink
		size ++;
	}
	
	public void insertLast(long value) {//插入尾节点
		Node newLink = new Node(value);
		if(isEmpty()){
			first = newLink;
		}
		else {
			last.next = newLink; //newLink <-- oldLast.next
			newLink.previous = last; //newLink.previous --> last
		}
		last = newLink;
		size ++;
	}
	
	public Node deleteFirst() {//删除头结点
		if(first == null) {
			System.out.println("链表为空!");
			return null;
		}
		Node temp = first;
		if(first.next == null) { //只有一个节点
			last = null;
		}
		else {
			first.next.previous = null;
		}
		first = first.next;
		size --;
		return temp;
	}
	
	public Node deleteLast() {//删除尾节点
		if(last == null) {
			System.out.println("链表为空");
			return null;
		}
		Node temp = last;
		if(last.previous == null) { //只有一个节点
			first = null;
		}
		else {
			last.previous.next = null;
		}
		last = last.previous;
		size --;
		return temp;
	}
	
	public boolean insertAfter(long key, long value) { //在key后面插入值为value的新节点
		Node current = first;
		while(current.data != key) {
			current = current.next;
			if(current == null) {
				System.out.println("不存在值为" + value + "的节点!");
				return false;
			}
		}
		if(current == last) {
			insertLast(value);
		}
		else {
			Node newLink = new Node(value);
			newLink.next = current.next;
			current.next.previous = newLink;
			newLink.previous = current;
			current.next = newLink;
			size ++;
		}
		return true;
	}
	
	public Node deleteNode(long key) {//删除指定位置的节点
		Node current = first;
		while(current.data != key) {
			current = current.next;
			if(current == null) {
				System.out.println("不存在该节点!");
				return null;
			}
		}
		if(current == first) {
			deleteFirst();
		}
		else if(current == last){
			deleteLast();
		}
		else {
			current.previous.next = current.next;
			current.next.previous = current.previous;
			size --;
		}
		return current;
	}
	
	public void displayForward() { //从头到尾遍历链表
		System.out.println("List(first --> last): ");
		Node current = first;
		while(current != null) {
			current.displayLink();
			current = current.next;
		}
		System.out.println(" ");
	}
	
	public void displayBackward() { //从尾到头遍历链表
		System.out.println("List(last --> first): ");
		Node current = last;
		while(current != null) {
			current.displayLink();
			current = current.previous;
		}
		System.out.println(" ");
	}
}

class Node {
	public long data;
	public Node next;
	public Node previous;
	
	public Node(long value) {
		data = value;
		next = null;
		previous = null;
	}
	
	public void displayLink() {
		System.out.print(data + " ");
	}
}

        在表头插入和删除速度很快,仅需改变一两个引用值,所以话费O(1)的时间。平均起来,查找、删除和在指定节点后面插入都需要搜索链表中的一半节点,需要O(N)次比较。在数组中执行这些操作也需要O(N)次比较,但是链表仍然要比数组快一些,因为当插入和删除节点时,链表不需要移动任何东西,增加的效率是很显著的,特别是当复制时间远远大于比较时间的时候。

        当然,链表比数组优越的另一个重要方面是链表需要多少内存就可以用多少内存,并且可以扩展到所有可用内存。数组的大小在它创建的时候就固定了,所以经常由于数组太大导致效率低下,或者数组太小导致空间溢出。可变数组可以解决这个问题,但是它经常只允许以固定大小的增量扩展,这个解决方案在内存使用率上来说还是比链表要低。

         链表比较简单,就讨论这么多,如有问题,欢迎留言指正~

_____________________________________________________________________________________________________________________________________________________

-----乐于分享,共同进步!

-----更多文章请看:http://blog.csdn.net/eson_15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值