1. 程式人生 > >數據結構(三)之單鏈表反向查找

數據結構(三)之單鏈表反向查找

hid 默認 splay del 下標 com 設置 display fbo

一、反向查找單鏈表

1、簡單查找

  先遍歷獲取單鏈表單長度n,然後通過計算得到倒數第k個元素的下標為n-k,然後查找下標為n-k的元素。

2、優化查找

先找到下標為k的元素為記錄點p1,然後設置新的記錄點p2的下標從0開始,同時遍歷兩個記錄點,直到p1的值為null,p2是倒數第k個元素。

單鏈表結點:

技術分享
package cn.edu.scau.mk;

/**
 *
 * @author MK
 * @param <T>
 */
public class Node<T> {

    private T data;
    private Node<T> next = null
; public Node(T data) { this.data = data; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) {
this.next = next; } }
View Code

鏈表:

技術分享
package cn.edu.scau.mk;

import java.util.Comparator;

/**
 *
 * @author MK
 * @param <T>
 */
public class LinkedList<T> {

    protected Node<T> head = null;

    /**
     * 添加
     *
     * @param data
     */
    public void add(T data) {
        
//頭結點為null if (head == null) { head = new Node<>(data); return; } //尋找末結點 Node<T> curNode = head; while (curNode.getNext() != null) { curNode = curNode.getNext(); } curNode.setNext(new Node<>(data));//添加結點 } /** * 刪除 * * @param index 下標,從0開始 * @return */ public boolean delete(int index) { //沒有數據 if (head == null) { return false; } //刪除頭結點 if (index == 0) { head = head.getNext(); } Node<T> curNode = head; int i = 1; while (curNode.getNext() != null) { if (i == index) { curNode.setNext(curNode.getNext().getNext()); return true; } i++; curNode = curNode.getNext(); } throw new IndexOutOfBoundsException("Index: "+index+", Size: "+i); } /** * 長度 * * @return */ public int length() { int len = 0; Node<T> curNode = head; while (curNode != null) { len++; curNode = curNode.getNext(); } return len; } /** * 查找 * @param index 位置 * @return */ public T get(int index) { Node<T> curNode = head; int i = 0; while (curNode != null) { if (i == index) { return curNode.getData(); } i++; curNode = curNode.getNext(); } throw new IndexOutOfBoundsException("Index: "+index+", Size: "+i); } /** * 排序 * @param comparator 比較器 */ public void sort(Comparator<T> comparator) { //沒有數據 if (head == null) { return; } Node<T> curNode = head; Node<T> nextNode; Node<T> minNode; while (curNode.getNext() != null) { minNode = curNode; //默認最小結點為當前結點 nextNode = curNode.getNext(); //下一個結點 while (nextNode != null) { //比當前結點小,記錄最小結點 if(comparator.compare(curNode.getData(), nextNode.getData())>0){ minNode=nextNode; } nextNode=nextNode.getNext(); //繼續與下一個結點比較 } //最小結點不是當前結點,交換數據 if(minNode!=curNode){ T data=curNode.getData(); curNode.setData(minNode.getData()); minNode.setData(data); } curNode=curNode.getNext(); //移至下一個結點 } } /** * 打印輸出 */ public void print() { Node<T> curNode = head; while (curNode!=null) { System.out.print(curNode.getData()+" "); curNode=curNode.getNext(); } System.out.println(); } }
View Code

二、簡單查找

package cn.edu.scau.mk;

import java.util.HashMap;

/**
 *
 * @author MK
 * @param <T>
 */
public class OpLinkedList<T> extends LinkedList<T> {

    /**
     * 簡單反向查找
     * @param index 倒數個數,從1開始
     * @return 
     */
    public T getByLastIndex(int index) {
        //倒數個數小於1
        if (index < 1) {
            throw new IndexOutOfBoundsException("Last Index : " + index);
        }
        
        int len=this.length();//鏈表長度
        //倒數個數越界
        if (head == null||len<index) {
            throw new IndexOutOfBoundsException("Last Index: " + index + ", Size: " + 0);
        }
        
        len=len-index;//第n-k個
        Node<T> curNode = head;  //默認第0個
        for (int i = 0; i < len; i++) {          
             curNode = curNode.getNext();//第i+1個
        }
        
        return curNode.getData();
    }
   
}

三、優化查找

package cn.edu.scau.mk;

import java.util.HashMap;

/**
 *
 * @author MK
 * @param <T>
 */
public class OpLinkedList<T> extends LinkedList<T> {

    /**
     *優化反向查找
     * @param index 倒數個數,從1開始
     * @return
     */
    public T getByLastIndex(int index) {

        if (index < 1) {
            throw new IndexOutOfBoundsException("Last Index : " + index);
        }
        if (head == null) {
            throw new IndexOutOfBoundsException("Last Index: " + index + ", Size: " + 0);
        }
        
        //查找第index個結點
        Node<T> curNode = head;
        for (int i = 0; i < index; i++) {
             if (curNode==null) {
                 throw new IndexOutOfBoundsException("Last Index: " + index + ", Size: " + i);         
            }
             curNode = curNode.getNext();
        }
        //循環直到curNode為null,indexNode剛好為倒數index個   
        Node<T> indexNode = head;
        while (curNode != null) {            
            curNode =curNode.getNext();
            indexNode=indexNode.getNext();
        }
            
        return indexNode.getData();
    }
}

數據結構(三)之單鏈表反向查找