链表是一种十分重要的数据结构结构,在程序设计中有很重要的地位。在C和C++中链表都是用指针实现的,但是为了内存安全,JAVA不再提供指针,因而有的读者认为JAVA无法实现链表,其实相反,我们只需简单地定义一个节点类“Node”即可轻松实现,接下来看代码(一共有三个类:MyList接口类、MyLinkedList功能类、MyAbstractList抽象类): MyList接口类: package list; public interface MyList { public void add(Object o); public void add(int index,Object o); public void clear(); public boolean contains(Object o); public Object get(int index); public int indexOf(Object o); public boolean isEmpty(); public int lastIndexOf(Object o); public boolean remove(Object o); public boolean remove(int index); public Object set(int index,Object o); public int size(); } MyLinkedList功能类: package list; public class MyLinkedList extends MyAbstractList { private Node first, last; private static class Node { Object element; Node next; public Node(Object element) { this.element = element; } } public MyLinkedList(Object[] objects) { super(objects); } public MyLinkedList() { } public Object getFirst() { if (size == 0) { return null; } else { return first.element; } } public Object getLast() { if (size == 0) { return null; } else { return last.element; } } public void addFirst(Object o) { Node newNode = new Node(o); newNode.next = first; first = newNode; size++; if (last == null) { last = first; } } public void addLast(Object o) { if (last == null) { last = first = new Node(o); } else { Node newNode = new Node(o); last.next = newNode; last = last.next; } size++; } public void add(int index, Object o) { if (index == 0) { addFirst(o); } else { if (index >= size) { addLast(o); } else { Node current = first; for (int i = 1; i < index; i++) { current = current.next; } Node temp1 = current.next; Node temp2 = new Node(o); current.next = temp2; temp2.next = temp1; size++; } } } public Object set(int index, Object o) { Node temp = new Node(o); if (index == 0) { first = temp; } else { Node current = first; for (int i = 1; i < index; i++) { current = current.next; } Node temp1 = current.next; current.next = temp; temp.next = temp1; } return o; } public void clear() { first = last = null; } public boolean contains(Object o) { Node current = first; while (current != null) { if (current.element.equals(o)) { return true; } current = current.next; } return false; } public Object get(int index) { Node current = first; for (int i = 0; i < index; i++) { current = current.next; } return current.element; } public int indexOf(Object o) { Node current = first; int index=-1; int i=0; while (current != null) { if (current.element.equals(o)) { return i; } current = current.next; i++; } return index; } public int lastIndexOf(Object o) { Node current = first; int index=-1; int i=0; while (current != null) { if (current.element.equals(o)) { index=i; } current = current.next; i++; } return index; } public boolean remove(Object o) { return remove(indexOf(o)); } public boolean remove(int index) { if (index == 0) { first.next=first; return true; } else { if (index >= size) { return false; } else { Node current = first; for (int i = 1; i < index; i++) { current = current.next; } Node temp = current.next; current.next = temp.next; size--; return true; } } } public String toString() { StringBuffer result=new StringBuffer("["); Node current = first; for(int i=0;i<size;i++){ result.append(current.element); current=current.next; if(current !=null)result.append(", "); else result.append("]"); } return result.toString(); } } MyAbstractList抽象类: package list; public abstract class MyAbstractList implements MyList{ protected int size; protected MyAbstractList(){ } protected MyAbstractList(Object[] objects){ for(int i=0;i<objects.length;i++){</objects.length;i++){ this.add(objects[i]); } } public void add(Object o){ add(size,o); } public boolean isEmpty(){ return size==0; } public int size(){ return size; } } 好了,接下来我们写一个测试类,测试一下: package list; public class TestList { public static void main(String[] args) { MyList list=new MyLinkedList(); list.add("tom"); System.out.println("(1)"+list.toString()); list.add(0,"Chalse"); System.out.println("(2)"+list.toString()); list.add("John"); System.out.println("(3)"+list.toString()); } } 结果如下: 我们可以看到刚才添加的值都依次打印出来了,测试成功! 其实上面的代码很简单,而且如果你只想实现单链表的话,MyLinkedList功能类就基本上能够满足。我在这里加上了接口和抽象类,是为了以后能更方便、更有结构性地添加新方法,使之实现双链表。笔者只不过抛砖引玉,至于怎么实现,读者自己琢磨(~_~)