在Java中如何实现双向链表?

Python019

在Java中如何实现双向链表?,第1张

双向链表:就是有双向指针,即双向的链域。\x0d\x0a链结点的结构:\x0d\x0a┌────┬────┬────────┐\x0d\x0a│ data │ next │ previous │\x0d\x0a└────┴────┴────────┘\x0d\x0a双向链表不必是双端链表(持有对最后一个链结点的引用),双端链表插入时是双向的。\x0d\x0a有两条链:一条从头到尾,一条从尾到头,删除遍历时也是双向的。\x0d\x0a/**\x0d\x0a * 双向链表\x0d\x0a */\x0d\x0apublic class DoublyLinkedList {\x0d\x0aprivate Link head //首结点\x0d\x0aprivate Link rear //尾部指针\x0d\x0apublic DoublyLinkedList() { }\x0d\x0apublic T peekHead() {\x0d\x0aif (head != null) {\x0d\x0areturn head.data\x0d\x0a}\x0d\x0areturn null\x0d\x0a}\x0d\x0apublic boolean isEmpty() {\x0d\x0areturn head == null\x0d\x0a}\x0d\x0apublic void insertFirst(T data) {// 插入 到 链头\x0d\x0aLink newLink = new Link(data)\x0d\x0aif (isEmpty()) {//为空时,第1次插入的新结点为尾结点\x0d\x0arear = newLink\x0d\x0a} else {\x0d\x0ahead.previous = newLink//旧头结点的上结点等于新结点\x0d\x0a}\x0d\x0anewLink.next = head//新结点的下结点旧头结点\x0d\x0ahead = newLink//赋值后,头结点的下结点是旧头结点,上结点null\x0d\x0a}\x0d\x0apublic void insertLast(T data) {//在链尾 插入\x0d\x0aLink newLink = new Link(data)\x0d\x0aif (isEmpty()) {\x0d\x0ahead = newLink\x0d\x0a} else {\x0d\x0arear.next = newLink\x0d\x0a}\x0d\x0anewLink.previous = rear\x0d\x0arear = newLink//赋值后,尾结点的上结点是旧尾结点,下结点null\x0d\x0a}\x0d\x0apublic T deleteHead() {//删除 链头\x0d\x0aif (isEmpty()) return null\x0d\x0aLink temp = head\x0d\x0ahead = head.next//变更首结点,为下一结点\x0d\x0aif (head != null) {\x0d\x0ahead.previous = null\x0d\x0a} else {\x0d\x0arear = null\x0d\x0a}\x0d\x0areturn temp.data\x0d\x0a}\x0d\x0apublic T deleteRear() {//删除 链尾\x0d\x0aif (isEmpty()) return null\x0d\x0aLink temp = rear\x0d\x0arear = rear.previous//变更尾结点,为上一结点\x0d\x0aif (rear != null) {\x0d\x0arear.next = null\x0d\x0a} else {\x0d\x0ahead = null\x0d\x0a}\x0d\x0areturn temp.data\x0d\x0a}\x0d\x0apublic T find(T t) {//从头到尾find\x0d\x0aif (isEmpty()) {\x0d\x0areturn null\x0d\x0a}\x0d\x0aLink find = head\x0d\x0awhile (find != null) {\x0d\x0aif (!find.data.equals(t)) {\x0d\x0afind = find.next\x0d\x0a} else {\x0d\x0abreak\x0d\x0a}\x0d\x0a}\x0d\x0aif (find == null) {\x0d\x0areturn null\x0d\x0a}\x0d\x0areturn find.data\x0d\x0a}\x0d\x0apublic T delete(T t) {\x0d\x0aif (isEmpty()) {\x0d\x0areturn null\x0d\x0a}\x0d\x0aLink current = head\x0d\x0awhile (!current.data.equals(t)) {\x0d\x0acurrent = current.next\x0d\x0aif (current == null) {\x0d\x0areturn null\x0d\x0a}\x0d\x0a}\x0d\x0aif (current == head) {\x0d\x0ahead = head.next\x0d\x0aif (head != null) {\x0d\x0ahead.previous = null\x0d\x0a}\x0d\x0a} else if (current == rear) {\x0d\x0arear = rear.previous\x0d\x0aif (rear != null) {\x0d\x0arear.next = null\x0d\x0a}\x0d\x0a} else {\x0d\x0a//中间的非两端的结点,要移除current\x0d\x0acurrent.next.previous = current.previous\x0d\x0acurrent.previous.next = current.next\x0d\x0a}\x0d\x0areturn current.data\x0d\x0a}\x0d\x0apublic boolean insertAfter(T key, T data) {//插入在key之后, key不存在return false\x0d\x0aif (isEmpty()) {\x0d\x0areturn false\x0d\x0a}\x0d\x0aLink current = head\x0d\x0awhile (!current.data.equals(key)) {\x0d\x0acurrent = current.next\x0d\x0aif (current == null) {\x0d\x0areturn false\x0d\x0a}\x0d\x0a}\x0d\x0aLink newLink = new Link(data)\x0d\x0aif (current == rear) {\x0d\x0arear = newLink\x0d\x0a} else {\x0d\x0anewLink.next = current.next\x0d\x0acurrent.next.previous = newLink\x0d\x0a}\x0d\x0acurrent.next = newLink\x0d\x0anewLink.previous = current\x0d\x0areturn true\x0d\x0a}\x0d\x0apublic void displayList4Head() {//从头开始遍历\x0d\x0aSystem.out.println("List (first-->last):")\x0d\x0aLink current = head\x0d\x0awhile (current != null) {\x0d\x0acurrent.displayLink()\x0d\x0acurrent = current.next\x0d\x0a}\x0d\x0a}\x0d\x0apublic void displayList4Rear() {//从尾开始遍历\x0d\x0aSystem.out.println("List (last-->first):")\x0d\x0aLink current = rear\x0d\x0awhile (current != null) {\x0d\x0acurrent.displayLink()\x0d\x0acurrent = current.previous\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aclass Link {//链结点\x0d\x0aT data//数据域\x0d\x0aLink next//后继指针,结点 链域\x0d\x0aLink previous//前驱指针,结点 链域\x0d\x0aLink(T data) {\x0d\x0athis.data = data\x0d\x0a}\x0d\x0avoid displayLink() {\x0d\x0aSystem.out.println("the data is " + data.toString())\x0d\x0a}\x0d\x0a}\x0d\x0apublic static void main(String[] args) {\x0d\x0aDoublyLinkedList list = new DoublyLinkedList()\x0d\x0alist.insertLast(1)\x0d\x0alist.insertFirst(2)\x0d\x0alist.insertLast(3)\x0d\x0alist.insertFirst(4)\x0d\x0alist.insertLast(5)\x0d\x0alist.displayList4Head()\x0d\x0aInteger deleteHead = list.deleteHead()\x0d\x0aSystem.out.println("deleteHead:" + deleteHead)\x0d\x0alist.displayList4Head()\x0d\x0aInteger deleteRear = list.deleteRear()\x0d\x0aSystem.out.println("deleteRear:" + deleteRear)\x0d\x0alist.displayList4Rear()\x0d\x0aSystem.out.println("find:" + list.find(6))\x0d\x0aSystem.out.println("find:" + list.find(3))\x0d\x0aSystem.out.println("delete find:" + list.delete(6))\x0d\x0aSystem.out.println("delete find:" + list.delete(1))\x0d\x0alist.displayList4Head()\x0d\x0aSystem.out.println("----在指定key后插入----")\x0d\x0alist.insertAfter(2, 8)\x0d\x0alist.insertAfter(2, 9)\x0d\x0alist.insertAfter(9, 10)\x0d\x0alist.displayList4Head()\x0d\x0a}\x0d\x0a}

链表是一种重要的数据结构,在程序设计中占有很重要的地位。C语言和C++语言中是用指针来实现链表结构的,由于Java语言不提供指针,所以有人认为在Java语言中不能实现链表,其实不然,Java语言比C和C++更容易实现链表结构。Java语言中的对象引用实际上是一个指针(本文中的指针均为概念上的意义,而非语言提供的数据类型),所以我们可以编写这样的类来实现链表中的结点。

class Node

{

Object data

Node next//指向下一个结点

}

将数据域定义成Object类是因为Object类是广义超类,任何类对象都可以给其赋值,增加了代码的通用性。为了使链表可以被访问还需要定义一个表头,表头必须包含指向第一个结点的指针和指向当前结点的指针。为了便于在链表尾部增加结点,还可以增加一指向链表尾部的指针,另外还可以用一个域来表示链表的大小,当调用者想得到链表的大小时,不必遍历整个链表。下图是这种链表的示意图:

链表的数据结构

我们可以用类List来实现链表结构,用变量Head、Tail、Length、Pointer来实现表头。存储当前结点的指针时有一定的技巧,Pointer并非存储指向当前结点的指针,而是存储指向它的前趋结点的指针,当其值为null时表示当前结点是第一个结点。那么为什么要这样做呢?这是因为当删除当前结点后仍需保证剩下的结点构成链表,如果Pointer指向当前结点,则会给操作带来很大困难。那么如何得到当前结点呢,我们定义了一个方法cursor(),返回值是指向当前结点的指针。类List还定义了一些方法来实现对链表的基本操作,通过运用这些基本操作我们可以对链表进行各种操作。例如reset()方法使第一个结点成为当前结点。insert(Object d)方法在当前结点前插入一个结点,并使其成为当前结点。remove()方法删除当前结点同时返回其内容,并使其后继结点成为当前结点,如果删除的是最后一个结点,则第一个结点变为当前结点。

链表类List的源代码如下:

import java.io.*

public class List

{

/*用变量来实现表头*/

private Node Head=null

private Node Tail=null

private Node Pointer=null

private int Length=0

public void deleteAll()

/*清空整个链表*/

{

Head=null

Tail=null

Pointer=null

Length=0

}

public void reset()

/*链表复位,使第一个结点成为当前结点*/

{

Pointer=null

}

public boolean isEmpty()

/*判断链表是否为空*/

{

return(Length==0)

}

public boolean isEnd()

/*判断当前结点是否为最后一个结点*/

{

if(Length==0)

 throw new java.lang.NullPointerException()

else if(Length==1)

 return true

else

 return(cursor()==Tail)

}

public Object nextNode()

/*返回当前结点的下一个结点的值,并使其成为当前结点*/

{

if(Length==1)

 throw new java.util.NoSuchElementException()

else if(Length==0)

 throw new java.lang.NullPointerException()

else

{

 Node temp=cursor()

 Pointer=temp

 if(temp!=Tail)

return(temp.next.data)

 else

throw new java.util.NoSuchElementException()

}

}

public Object currentNode()

/*返回当前结点的值*/

{

Node temp=cursor()

return temp.data

}

public void insert(Object d)

/*在当前结点前插入一个结点,并使其成为当前结点*/

{

Node e=new Node(d)

if(Length==0)

{

 Tail=e

 Head=e

}

else

{

 Node temp=cursor()

 e.next=temp

 if(Pointer==null)

Head=e

 else

Pointer.next=e

}

Length++

}

public int size()

/*返回链表的大小*/

{

return (Length)

}

public Object remove()

/*将当前结点移出链表,下一个结点成为当前结点,如果移出的结点是最后一个结点,则第一个结点成为当前结点*/

{

Object temp

if(Length==0)

 throw new java.util.NoSuchElementException()

else if(Length==1)

{

 temp=Head.data

 deleteAll()

}

else

{

 Node cur=cursor()

 temp=cur.data

 if(cur==Head)

Head=cur.next

 else if(cur==Tail)

 {

Pointer.next=null

Tail=Pointer

reset()

 }

 else

Pointer.next=cur.next

Length--

}

return temp

}

private Node cursor()

/*返回当前结点的指针*/

{

if(Head==null)

 throw new java.lang.NullPointerException()

else if(Pointer==null)

 return Head

else

 return Pointer.next

}

public static void main(String[] args)

/*链表的简单应用举例*/

{

List a=new List ()

for(int i=1i<=10i++)

 a.insert(new Integer(i))

 System.out.println(a.currentNode())

 while(!a.isEnd())

System.out.println(a.nextNode())

a.reset()

while(!a.isEnd())

{

 a.remove()

}

a.remove()

a.reset()

if(a.isEmpty())

 System.out.println("There is no Node in List \n")

 System.in.println("You can press return to quit\n")

try

{

 System.in.read()

 //确保用户看清程序运行结果

}

catch(IOException e)

{}

 }

}

class Node

/*构成链表的结点定义*/

{

 Object data

 Node next

 Node(Object d)

 {

data=d

next=null

 }

}

读者还可以根据实际需要定义新的方法来对链表进行操作。双向链表可以用类似的方法实现只是结点的类增加了一个指向前趋结点的指针。

可以用这样的代码来实现:

class Node

{

Object data

Node next

Node previous

Node(Object d)

{

data=d

next=null

previous=null

}

}

当然,双向链表基本操作的实现略有不同。链表和双向链表的实现方法,也可以用在堆栈和队列的实现中,这里就不再多写了,有兴趣的读者可以将List类的代码稍加改动即可。

希望对你有帮助。

定义接口:

//Deque.java

package dsa//根据自己的程序位置不同

public interface Deque {

public int getSize()//返回队列中元素数目

public boolean isEmpty()//判断队列是否为空

public Object first() throws ExceptionQueueEmpty//取首元素(但不删除)

public Object last() throws ExceptionQueueEmpty//取末元素(但不删除)

public void insertFirst(Object obj)//将新元素作为首元素插入

public void insertLast(Object obj)//将新元素作为末元素插入

public Object removeFirst() throws ExceptionQueueEmpty//删除首元素

public Object removeLast() throws ExceptionQueueEmpty//删除末元素

public void Traversal()//遍历

}

双向链表实现:

//Deque_DLNode.java

/*

* 基于双向链表实现双端队列结构

*/

package dsa

public class Deque_DLNode implements Deque {

protected DLNode header//指向头节点(哨兵)

protected DLNode trailer//指向尾节点(哨兵)

protected int size//队列中元素的数目

//构造函数

public Deque_DLNode() {

header = new DLNode()

trailer = new DLNode()

header.setNext(trailer)

trailer.setPrev(header)

size = 0

}

//返回队列中元素数目

public int getSize()

{ return size}

//判断队列是否为空

public boolean isEmpty()

{ return (0 == size) ? true : false}

//取首元素(但不删除)

public Object first() throws ExceptionQueueEmpty {

if (isEmpty())

throw new ExceptionQueueEmpty("意外:双端队列为空")

return header.getNext().getElem()

}

//取末元素(但不删除)

public Object last() throws ExceptionQueueEmpty {

if (isEmpty())

throw new ExceptionQueueEmpty("意外:双端队列为空")

return trailer.getPrev().getElem()

}

//在队列前端插入新节点

public void insertFirst(Object obj) {

DLNode second = header.getNext()

DLNode first = new DLNode(obj, header, second)

second.setPrev(first)

header.setNext(first)

size++

}

//在队列后端插入新节点

public void insertLast(Object obj) {

DLNode second = trailer.getPrev()

DLNode first = new DLNode(obj, second, trailer)

second.setNext(first)

trailer.setPrev(first)

size++

}

//删除首节点

public Object removeFirst() throws ExceptionQueueEmpty {

if (isEmpty())

throw new ExceptionQueueEmpty("意外:双端队列为空")

DLNode first = header.getNext()

DLNode second = first.getNext()

Object obj = first.getElem()

header.setNext(second)

second.setPrev(header)

size--

return(obj)

}

//删除末节点

public Object removeLast() throws ExceptionQueueEmpty {

if (isEmpty())

throw new ExceptionQueueEmpty("意外:双端队列为空")

DLNode first = trailer.getPrev()

DLNode second = first.getPrev()

Object obj = first.getElem()

trailer.setPrev(second)

second.setNext(trailer)

size--

return(obj)

}

//遍历

public void Traversal() {

DLNode p = header.getNext()

while (p != trailer) {

System.out.print(p.getElem()+" ")

p = p.getNext()

}

System.out.println()

}

}