- 主要类:
package dcircularlinked;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class dcircularlink<E> implements linearList<E>{
private class Node<E>{
public E value; //节点值
public Node<E> prev; //前一个节点
public Node<E> next; //后一个节点
public Node(E value, Node<E> prev, Node<E> next) {
this.value = value;
this.prev = prev;
this.next = next;
}
}
private int size;//链表长度
public Node<E> head;//头节点
public dcircularlink() {
head = new Node<E>(null, null, null);
head.prev = head.next = head;
size = 0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void validateIndex(int index){
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return;
}
public int indexOf( E theElement) {
Node<E> currentNode = head.next;
int index = 0;
while (currentNode.value != theElement&¤tNode!=head)
{
currentNode = currentNode.next;
index++;
}
if (currentNode == head)
return -1;
else
return index;
}
private Node<E> getNode(int index){
validateIndex(index);
if (index <= size/2) {
Node<E> cur = head.next;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
return cur;
}
Node<E> cur = head.prev;
int newIndex = size - index - 1;
for (int i = 0; i < newIndex; i++) {
cur = cur.prev;
}
return cur;
}
public E get(int index){
return getNode(index).value;
}
public void insert(int index, E value){
if (index != size)
validateIndex(index);
Node<E> p = head;
for (int i = 0; i < index; i++)
p = p.next;
p.next = new Node<E>(value,p ,p.next);
p.next.next.prev=p.next;
size++;
return;
}
public void output() {
Node<E> p = head.next;
while(p != head){
System.out.println(p.value);
p = p.next;
}
}
public void erase(int index){
validateIndex(index);
Node<E> p = head;
for(int i = 0;i < index;i++)
p = p.next;
p.next.next.prev=p;
p.next = p.next.next;
size--;
return;
}
public Iterator<E> iterator() {
return new myListIterator();
}
public class myListIterator implements Iterator<E> {
Node<E> current = head.next;
public boolean hasNext() {
return (current != head);
}
public E next() {
if (current==head)
throw new IndexOutOfBoundsException();
E item = current.value;
current = current.next;
return item;
}
public void remove() {
if (current==head)
throw new NoSuchElementException();
current.prev.next = current.next;
current.next.prev = current.prev;
current = current.next;
size--;
}
}
}
- 接口:
package dcircularlinked; public interface linearList<T> { public T get(int theIndex); public int indexOf( T theElement); void erase(int theIndex) ; void insert(int theIndex, T value); void output() ; }
测试:
package dcircularlinked; import java.util.Iterator; public class test { public static void main(String[] args) { dcircularlink<Integer> y=new dcircularlink<Integer>(), z=new dcircularlink<Integer>(); // test size System.out.println("Initial size of y and z = "); System.out.println(y.size()); System.out.println(","); System.out.println(z.size()); // test insert y.insert(0, 2); y.insert(1, 6); y.insert(0, 1); y.insert(2, 4); y.insert(3, 5); y.insert(2, 3); System.out.println("Inserted 6 integers, list y should be 1 2 3 4 5 6"); y.output(); System.out.println("Size of y = "); System.out.println( y.size()); y.erase(5); y.output(); int index = y.indexOf(4); if (index < 0) System.out.println("4 not found "); else System.out.println("The index of 4 is "); System.out.println( index); index = y.indexOf(7); if (index < 0) System.out.println(" 7 not found"); else System.out.println(index); System.out.println("Hello World"); Iterator<Integer> it = y.iterator(); while (it.hasNext()) System.out.println(it.next()); } }