Java经典算法大全 下载本文

内容发布更新时间 : 2024/4/27 20:22:13星期一 下面是文章的全部内容请认真阅读。

public T next() {

if(nextNode == header)

throw new NoSuchElementException(\

lastReturned = nextNode; nextNode = nextNode.next; return lastReturned.nodeValue; }

public void remove() {

if(lastReturned == header)

throw new IllegalStateException(\

MyLinkedList.this.remove(lastReturned); lastReturned = header; listSize--;

} }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

private class MyListIterator extends MyIterator implements ListIterator {

private int nextIndex;

MyListIterator(int index) {

if(index < 0 || index > listSize)

throw new IndexOutOfBoundsException(\

//如果index小于listSize/2,就从表头开始查找定位,否则就从表尾开始查找定位

if(index < (listSize >> 1)) { nextNode = header.next;

for(nextIndex = 0; nextIndex < index; nextIndex++) nextNode = nextNode.next; }else {

nextNode = header;

for(nextIndex = listSize; nextIndex > index; nextIndex--) nextNode = nextNode.prev; } }

public boolean hasPrevious() {

return nextIndex != 0;

//return nextNode.prev != header; }

36

public T previous() { if (nextIndex == 0)

throw new NoSuchElementException(\

lastReturned = nextNode = nextNode.prev; nextIndex--;

return lastReturned.nodeValue; }

public void remove() {

if(lastReturned == header)

throw new IllegalStateException(\

MyLinkedList.this.remove(lastReturned); nextIndex--;

listSize--;

if(lastReturned == nextNode) nextNode = nextNode.next; lastReturned = header;

}

public void add(T item) {

MyLinkedList.this.addBefore(nextNode, item); nextIndex++; listSize++;

lastReturned = header; }

public void set(T item) {

if (lastReturned == header)

throw new IllegalStateException();

lastReturned.nodeValue = item; }

public int nextIndex() { return nextIndex; }

public int previousIndex() { return nextIndex - 1; } }

37

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public Iterator iterator() { return new MyIterator(); }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public ListIterator listIterator(int index) {

return new MyListIterator(index); }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public static void main(String[] args) {

MyLinkedList t = new MyLinkedList(); t.add(\ t.add(\ t.add(\ t.add(\ //t.remove(\ //t.addFirst(\ //t.addLast(\ //t.printList();

ListIterator it = t.listIterator(t.size());

while(it.hasPrevious()) {

System.out.println(it.previous()); // D C B A }

}

}// MyLinkedList end~

ArrayList 底层数组实现的,当实例化一个ArrayList是也相当实例化了一个数组 所以对元素的随即访问较快,而增加删除操作慢

LinkedList 底层实现是一个双向链表,没一个结点都包含了前一个元素的引用和后一个元素的引用和结点值

所以对元素的随即访问很慢,而增删较快

java 实现链表和c实现一样。 就是指针变成了引用。

【参考资料】JAVA的链表(2009-05-11 01:35:49)标签:java 链表 分类:学习资料 又是个不错的地方:http://blog.sina.com.cn/s/articlelist_1282789430_0_1.html

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

38

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()

39

{

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) {

40