您的位置:首页 > 理论基础 > 数据结构算法

【数据结构】链表

2016-04-06 13:33 381 查看
昨天学了链表的一些基本操作,今天记录下来进行复习巩固首先是链表结构的声明,链表包含表元素以及指向该元素后继元的指针
<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指针指向nullListNode<T> node = new ListNode<T>(t,null);//让p指针的next指向nodep.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指向nodenode.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.nextListNode<T> p = pre.next;//把前驱元素指针的next指向第index位置的p的nextpre.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!=4000null){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指针指向pnext.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;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: