鏈表的java實現與時間和空間複雜度分析

今天主要編寫了鏈表,其具體代碼如下:

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class LinkList {
    private Node head;

    public LinkList() {
        // TODO Auto-generated constructor stub
        head = new Node("");
    }
    public void Insert() {
        String string;
        Node node = head;
        while (!(string = StdIn.readString()).equals("#")) {
            Insert(node, new Node(string));
            node = node.next;
        }
    }

    /**
     * insert node n2 after node n1
     * @param n1
     * @param n2
     */
    public void Insert(Node n1, Node n2) {
        if (n1 == null || n2 == null);
        else {
            Node n3 = n1.next;
            n1.next = n2;
            n2.next = n3;
        }
    }

    /**
     * delete the k-th node
     * @author Bjy_PC
     *
     */
    public String delete(int k) {
        if (head.next == null) return null;
        Node node = head;
        int count = 0;
        String string = new String();
        while (node.next != null) {
            if (k - 1 == count) {
                string = node.next.value;
                node.next = node.next.next;
                break;
            }
            node = node.next;
            count++;
        }
        return string;

    }

    /**
     * find the max element using recursion
     * @author Bjy_PC
     *
     */
    public Node max() {
        return max(head.next, head.next.next);      
    }
    public Node max(Node n1, Node n2) {
        if (n2.next == null)  return n1;
        else                  return max(n1.value.compareTo(n2.value) >= 0? n1:n2, n2.next);     
    }

    /**
     * show the list
     * @author Bjy_PC
     *
     */
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        Node node = head;
        String string = new String("");
        while (node.next != null) {
            node = node.next;
            string += node.value + "--->";
        }
        return string;
    }

    /**
     * reverse the linklist
     * @author Bjy_PC
     *
     */
    public Node reverse() {
        Node first = head.next;
        Node second = first.next;
        Node reverse = null;

        if (first == null || second == null) return this.head.next;
        else {
            while (first != null) {
                first.next = reverse;
                reverse = first;
                first = second;
                if (second != null) 
                    second = second.next;
            }
        }
        head.next = reverse;
        return reverse;     
    }

     class Node {
        Node next;
        String value;

        public Node(String item) {
            // TODO Auto-generated constructor stub
            this.value = item;
            this.next = null;
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkList list = new LinkList();
        list.Insert();
        StdOut.println(list);
        list.delete(1);
        list.Insert(list.head.next.next, list.new Node("perfect"));
        StdOut.println(list);
        StdOut.println("max value :" + list.max().value);
        list.reverse();
        StdOut.println(list);
    }
}

其中每個操作的時間複雜度最多爲線性的,對空間複雜度來說,Node節點佔用72字節,其中Node對象開銷16個字節,內部類佔用8字節的額外開銷,指向Node的引用佔用8個字節,String對象佔用40個字節。則長度爲N的鏈表至少需要24 + 72*N個字節(不包括字符串數組的字節數),其中LinkList對象有16個字節的對象開銷,head節點的引用佔用8個字節。

發佈了39 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章