一、简单的链表结点类
1、单链表的两个属性:
1)value:值
2)next:指向下一个结点的指针
对于双链表则多了一个属性:pre:表示指向上一个结点的指针
2、链表结点类的代码:
package com.hnust.List;
public class ListNode<T> {
public T value;
public ListNode<T> next;
public ListNode(T value, ListNode<T> next) {
super();
this.value = value;
this.next = next;
}
public ListNode() {
super();
}
public ListNode<T> pre;
}
二、链表的增删改查、逆序打印链表(递归和非递归)以及求链表最大元素等操作
package com.hnust.List;
import java.util.Comparator;
import java.util.Stack;
public class MiniList<T> {
private ListNode<T> head = new ListNode<T>(null, null);
public Comparator<T> comp;
public int compare(T a , T b){
if(comp!=null){
return comp.compare( a, b);
}else{
Comparable<T> c = (Comparable<T>) a;
return c.compareTo(b);
}
}
public T getMax(){
if(head.next==null){
return null;
}
ListNode<T> p = head.next;
T max = p.value;
p = p.next;
while(p!=null){
if(compare(p.value,max)>0){
max = p.value;
}
p = p.next;
}
return max;
}
public void arrayToList(T[] array) {
ListNode<T> p = head;
for (T t : array) {
ListNode<T> node = new ListNode<T>(t, null);
p.next = node;
p = node;
}
}
public void insert(int index, T value) {
ListNode<T> p = head;
for (int i = 0; i <= index; i++) {
p = p.next;
}
ListNode<T> node = new ListNode<T>(value, null);
node.next = p.next;
p.next = node;
}
public void remove(int index) {
ListNode<T> p = head;
for (int i = 0; i <= index - 1; i++) {
p = p.next;
}
p.next = p.next.next;
}
public T get(int index) {
ListNode<T> p = head;
for (int i = 0; i <= index; i++) {
p = p.next;
}
System.out.println(p.value);
return p.value;
}
public void set(int index, T value) {
ListNode<T> p = head;
for (int i = 0; i <= index; i++) {
p = p.next;
}
p.value = value;
}
public void printInverse() {
if (head.next == null) {
return;
}
Stack<T> stack = new Stack();
ListNode<T> p = head.next;
while (p != null) {
stack.push(p.value);
p = p.next;
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
System.out.println();
}
private void recursive(ListNode<T> p) {
if (p != null) {
recursive(p.next);
System.out.print(p.value+" ");
}
}
public void printRecursive(){
if (head.next == null) {
return;
}
recursive(head.next);
}
public void printList() {
ListNode<T> p = head.next;
while (p != null) {
System.out.print(p.value + " ");
p = p.next;
}
System.out.println();
}
}
三、链表测试类
package com.hnust.List;
/**
* 链表测试类
* */
import java.util.Comparator;
import org.junit.Test;
public class TestList {
@SuppressWarnings("unused")
@Test
public void testNode01() {
ListNode<Integer> p4 = new ListNode<Integer>(4, null);
ListNode<Integer> p3 = new ListNode<Integer>(3, p4);
ListNode<Integer> p2 = new ListNode<Integer>(2, p3);
ListNode<Integer> p1 = new ListNode<Integer>(1, p2);
}
@SuppressWarnings("unused")
@Test
public void testNode02() {
ListNode<Integer> p1 = new ListNode<Integer>(1,
new ListNode<Integer>(2, new ListNode<Integer>(3, new ListNode<Integer>(4, null))));
}
@Test
public void testMiniList() {
MiniList<Integer> list = new MiniList<Integer>();
Integer[] array = new Integer[] { 0, 1, 2, 3, 4, 5 };
list.arrayToList(array);
list.printList();
list.insert(3, 6);
list.printList();
list.remove(3);
list.printList();
list.get(2);
list.printList();
list.set(5, 10);
list.printList();
}
@Test
public void testPrintInverse() {
MiniList<Integer> list = new MiniList<Integer>();
Integer[] array = new Integer[] { 0, 1, 2, 3, 4, 5 };
list.arrayToList(array);
list.printList();
list.printInverse();
list.printRecursive();
}
@Test
public void testMaxInteger() {
MiniList<Integer> list = new MiniList<Integer>();
Integer[] array = { 1, 43, 53, 4, 73, 10, 8, 9, 7, 22, 35 };
list.arrayToList(array);
System.out.println(list.getMax());
}
@Test
public void testMaxPerson() {
MiniList<Person> list = new MiniList<Person>();
list.comp = new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getId() - o2.getId();
}
};
Person[] per = { new Person(1, "Wupeng"), new Person(2, "Kobe"), new Person(3, "James") };
list.arrayToList(per);
System.out.println(list.getMax());
}
}