昨天学了链表的一些基本操作,今天记录下来进行复习巩固
首先是链表结构的声明,链表包含表元素以及指向该元素后继元的指针
<pre name="code" class="java">/**
* Created by novax_000 on 2016/4/5.
*/
public class ListNode<T> {
//单链表结构,元素值value,指向后继元的指针next
public T value;
public ListNode<T> next;
//有参构造函数
public ListNode(T value, ListNode<T> next) {
super();
this.value = value;
this.next = next;
}
//无参构造函数
public ListNode() {
super();
}
}
将一个数组转为链表
//声明一个头节点
private ListNode<T> head = new ListNode<T>(null,null);
//将数组转为链表
public void arrayToList(T[] array)
{
//声明链表的指针,指向头节点
ListNode<T> p = head;
//遍历数组,将数组的每一个值放到链表里
for(T t:array)
{
//节点值为数组的每一个值t,默认next指针指向null
ListNode<T> node = new ListNode<T>(t,null);
//让p指针的next指向node
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
node.next = p.next;
p.next = node;
}
链表的删除操作
//把链表的第index位置的元素删除
public T remove(int index)
{
ListNode<T> pre = head;
//获得第index之前一个位置的指针
for(int i=0;i<index;i++)
{
pre= pre.next;
}
//第index位置的指针指向的是pre.next
ListNode<T> p = pre.next;
//把前驱元素指针的next指向第index位置的p的next
pre.next = p.next;
return p.value;
}
链表的查询操作
//查询链表index位置的value public T get(int index) { ListNode<T> p = head; for(int i=0;i<=index;i++) { p = p.next; } return p.value; }
链表的修改操作
//将链表的第index位置的值改为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 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 printInverse() { if(head.next==null) { return; } //遍历链表,入栈,再挨个弹出 Stack<T> stack = new Stack<T>(); 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(); }
逆序打印链表(递归)
//递归实现逆序打印链表 public void printInverseRecursive() { if(head.next==null) { return; } recursiveInverse(head.next); System.out.println(); } public void recursiveInverse(ListNode<T> p) { if(p!=null) { recursiveInverse(p.next); System.out.print(p.value+" "); } }
链表反转(非递归)
//非递归实现链表反转 public ListNode<T> reverseList(ListNode<T> head) { if(head==null||head.next==null) { return head; }else{ ListNode<T> pre = head; ListNode<T> p = head.next; ListNode<T> next = null; while(p!=null) { //先用next存放下一个节点 next = p.next; //再把当前结点指向前一个节点 p.next = pre; //再把pre和p往后移 pre = p; p = next; } //最后清除头节点的环 head.next=null; return pre; } }
链表反转(递归)
//递归实现链表反转 public ListNode<T> recursive(ListNode<T> p) { if(p.next==null) { return p; } else { //建一个next储存p的后继元 ListNode<T> next = p.next; //一直递归到最后一个元素 ListNode<T> tail = recursive(next); //让后继元的next指针指向p next.next = p; return tail; } } public ListNode<T> reverseListRecursive(ListNode<T> head) { if(head==null||head.next==null) { return head; }else { ListNode<T> tail = recursive(head); //去掉头节点的环 head.next=null; return tail; }