此处包括一个泛型链表抽象类,一个Integer型实现类,一个测试类。
实现了链表的以下功能:
1.节点添加(第一个或最后一个)
2.节点删除(第一个或最后一个)
3.遍历(返回一个数组)
4.排序
5.删除指定元素
6.取指定元素
7.扩充长度
8.判空
9.取长度
10.清空链表
泛型链表抽象类
LinkedList.java:
package linkedlist;
public abstract class LinkedList<T> {
public Node<T> head;
public LinkedList() {
this.head = new Node<T>();
}
public Node<T> getHead() {
return head;
}
public void setHead(Node<T> head) {
this.head = head;
}
public Node<T> fetchLast() {
Node<T> p = this.head;
while (p.next != null) {
p = p.next;
}
return p;
}
public Node<T> fetchBeforeLast() {
Node<T> p = this.head;
while (p.next.next != null) {
p = p.next;
}
return p;
}
public void insert(T data) {
insertFront(data);
}
public void add(T data) {
insertRear(data);
}
public void insertFront(T data) {
Node<T> q = new Node<T>(data);
Node<T> p = this.head.next;
q.next = p;
this.head.next = q;
}
public void insertRear(T data) {
Node<T> q = new Node<T>(data);
Node<T> p = this.fetchLast();
p.next = q;
}
public void clear() {
this.head.next = null;
}
public void remove() {
if (this.getSize() == 0) {
throw new IllegalArgumentException("this linked list is empty.");
}
Node<T> p = this.head.next;
this.head.next = p.next;
p = null;
}
public void removeLast() {
if (this.getSize() == 0) {
throw new IllegalArgumentException("this linked list is empty.");
}
Node<T> preP = this.fetchBeforeLast();
Node<T> p = this.fetchLast();
preP.next = p.next;
p = null;
}
public Node<T> fetch(T data) {
if (this.getSize() == 0) {
throw new IllegalArgumentException("this linked list is empty.");
}
Node<T> p = this.head.next;
while (p.next != null && p.data.equals(data) != true) {
p = p.next;
}
return p;
}
public void delete(T data) {
if (this.getSize() == 0) {
throw new IllegalArgumentException("this linked list is empty.");
}
Node<T> p = fetch(data);
Node<T> q = this.head;
while (q.next.data.equals(data) != true) {
q = q.next;
}
q.next = p.next;
p = null;
}
public int getSize() {
int size = 0;
Node<T> p = this.head.next;
while (p != null) {
p = p.next;
size++;
}
return size;
}
public boolean isEmpty() {
return this.getSize() == 0 ? true : false;
}
public abstract T[] travel();
public abstract void sort();
}
Integer型实现类
IntegerLinkedList.java:
package linkedlist;
public class IntegerLinkedList extends LinkedList<Integer> {
public IntegerLinkedList() {
super();
}
public void print() {
Node<Integer> p = this.head.next;
System.out.print("Head->");
while(p != null) {
System.out.print(p.data + "->");
p = p.next;
}
System.out.println("null");
}
@Override
public Integer[] travel() {
Integer arr[] = new Integer[this.getSize()];
Node<Integer> p = this.head.next;
int index = 0;
while(p != null) {
arr[index++] = p.data;
p = p.next;
}
return arr;
}
@Override
public void sort() {
Integer arr[] = this.travel();
int size = this.getSize();
for(int i = 0; i < this.getSize(); i++) {
for(int j = 1; j < this.getSize(); j++) {
if(arr[j - 1] > arr[j]) {
Integer temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
this.head.next = null;
for(int i = 0; i < size; i++) {
this.add(arr[i]);
}
}
}
测试类
IntegerLinkedListDemo.java:
package linkedlist;
public class IntegerLinkedListDemo {
public static void main(String[] args) {
IntegerLinkedList test = new IntegerLinkedList();
test.print();
System.out.println("================");
test.insert(1);
test.insert(2);
test.insert(3);
test.print();
System.out.println("size is: " + test.getSize());
System.out.println("================");
test.add(1);
test.add(2);
test.add(3);
test.print();
System.out.println("size is: " + test.getSize());
System.out.println("================");
test.remove();
test.print();
System.out.println("size is: " + test.getSize());
System.out.println("================");
test.removeLast();
test.print();
System.out.println("size is: " + test.getSize());
System.out.println("================");
test.delete(1);
test.delete(1);
test.print();
System.out.println("size is: " + test.getSize());
System.out.println("================");
test.delete(2);
test.delete(2);
test.add(5);
test.add(99);
test.add(-1);
test.add(0);
test.add(43);
test.add(26);
test.print();
System.out.println("size is: " + test.getSize());
System.out.println("================");
test.sort();
test.print();
System.out.println("================");
test.clear();
test.print();
System.out.println("================");
}
}
输出结果:
Head->null
================
Head->3->2->1->null
size is: 3
================
Head->3->2->1->1->2->3->null
size is: 6
================
Head->2->1->1->2->3->null
size is: 5
================
Head->2->1->1->2->null
size is: 4
================
Head->2->2->null
size is: 2
================
Head->5->99->-1->0->43->26->null
size is: 6
================
Head->-1->0->5->26->43->99->null
================
Head->null
================