Java单链表实现

/**
 * 
 * 单链表基本操作
 * 
 * @author John
 *
 */
class LinkList {

    private Node first;
    private int pos = 0;

    public LinkList() {
        this.first = null;
    }

    /**
     * 插入头结点
     * 
     * @param data
     */
    public void insertFirstNode(int data) {
        Node node = new Node(data);
        node.next = first;
        first = node;
    }

    /**
     * 删除头结点
     * 
     * @return
     */
    public Node deleteFirstNode() {
        Node tempNode = first;
        first = first.next;
        return tempNode;
    }

    /**
     * 在index之后的位置插入date数据
     * 
     * @param index
     * @param data
     */
    public void insertNode(int index, int data) {
        Node node = new Node(data);
        Node current = first;
        Node previous = first;
        while (pos != index) {
            previous = current;
            current = current.next;
            pos++;
        }
        node.next = current;
        previous.next = node;
        pos = 0;
    }

    /**
     * 根据位置删除节点
     * 
     * @param index
     * @return
     */
    public Node deleteBypos(int index) {
        Node current = first;
        Node previous = first;
        while (pos != index) {
            pos++;
            previous = current;
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            pos = 0;
            previous.next = current.next;
        }
        return current;
    }

    /**
     * 根据节点数据删除指定节点
     * 
     * @param data
     * @return
     */
    public Node deleteByData(int data) {
        Node current = first;
        Node previous = first;
        Node temp = null;
        while (current != null) {
            while (current.data != data) {
                if (current.next == null) {
                    if (temp != null)
                        return temp;
                    return null;
                }
                previous = current;
                current = current.next;
            }
            if (current == first) {
                temp = current;
                first = first.next;
                current = first;
                previous = first;
            } else {
                temp = current;
                previous.next = current.next;
                current = current.next;
            }
        }
        return temp;
    }

    /**
     * 根据某一位置查找节点信息
     * 
     * @param index
     * @return
     */
    public Node findByPos(int index) {
        Node current = first;
        if (pos != index) {
            current = current.next;
            pos++;
        }
        pos = 0;
        return current;
    }

    /**
     * 输出所有节点信息
     */
    public void displayAllNodes() {
        Node current = first;
        String next = "";
        while (current != null) {
            System.out.print(next);
            current.display();
            current = current.next;
            next = " --> ";
        }
        System.out.println();
    }

    /**
     * 得到链表的长度
     * 
     * @return
     */
    public int length() {
        Node current = first;
        int lenght = 0;
        while (current != null) {
            lenght++;
            current = current.next;
        }
        return lenght;
    }

    /**
     * 对链表元素数据进行排序
     */
    public void sort() {
        int n = this.length();
        int temp = 0;
        Node p = first;
        if (first == null || first.next == null) {
            return;
        }
        for (int i = 1; i < n; i++) {
            p = first;
            for (int j = i; j < n; j++) {
                if (p.data > p.next.data) {
                    temp = p.data;
                    p.data = p.next.data;
                    p.next.data = temp;
                }
                p = p.next;
            }
        }
    }

    /**
     * 反转链表
     */
    public void reverse() {
        if (first == null || first.next == null) {
            return;
        }
        Node p1 = first;
        Node p2 = p1.next;
        Node p3 = null;
        while (p2 != null) {
            p3 = p2.next;
            p2.next = p1;
            p1 = p2;
            p2 = p3;
        }
        first.next = null;
        first = p1;
    }

}

class Node {
    Node next;
    int data;

    public Node(int data) {
        this.data = data;
    }

    public void display() {
        System.out.print(data);
    }
}

 

posted @ 2016-08-22 09:12  SaraMorning  阅读(309)  评论(0编辑  收藏  举报