java中的LinkedList

來源:互聯網
上載者:User

package com.csmzxy.rjxy.j2se.array_collection_04;

public class LinkedListTest {

    private static int size;
    private Node header = null;// 表示為頭結點

    public LinkedListTest() {
        header = new Node(null, null);
        size = 0;
    }

    public boolean addhead(Object o) {
        if (header != null) {
            Node node = new Node(o, null);
            node.setNext(header.getNext());
            header.setNext(node);
            size++;
            return true;
        }
        return false;
    }

    public void add(Object o) {
        Node node = new Node(o, null);
        Node temp = header;
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        temp.setNext(node);
        size++;
    }

    // 列印鏈錶裡面所有的節點
    public void print() {
        Node temp = header;
        while (temp.getNext() != null) {
            System.out.println("元素:" + temp.getNext().value);
            temp = temp.getNext();
        }

    }

    // 1. 在此列表中指定的位置插入指定的元素
    public boolean add(int index, Object o) {
        Node newNode = new Node(o, null);
        if (index > 0 && index <= size) {
            Node temp = header;
            for (int i = 1; i < index; i++) {
                temp = temp.getNext();
            }
            newNode.setNext(temp.getNext());
            temp.setNext(newNode);
            size++;
            return true;

        } else {
            return false;
        }
    }

    // 2. 將指定元素插入此列表的開頭。
    public void addFirst(Object o) {
        // 首Crowdsourced Security Testing道的是header節點為1,指定元素插入到0位置
        Node newNode = new Node(o, null);
        Node temp = header;
        newNode.setNext(temp.getNext());
        temp.setNext(newNode);
        size++;
    }

    // 3. 將指定元素添加到此列表的結尾
    public void addLast(Object o) {
        Node newNode = new Node(o, null);
        Node temp = header;
        for (int i = 1; i <= size; i++) {
            temp = temp.getNext();
            if (temp.getNext() == null) {
                temp.setNext(newNode);

            }
        }
        size++;

    }

    // 4. 從此列表中移除所有元素
    public void clear() {
        int j = size;
        Node temp = header;
        header = null;
        for (int i = 1; i <= j; i++) {
            temp = temp.getNext();
            temp.setValue(null);
            System.out.println(temp.getValue());
            size--;
        }
    }

    // 5. 返回此列表中指定位置處的元素
    private Node get(int index) {
        Node temp = header;
        if (index <= 0 || index > size) {
            throw new IndexOutOfBoundsException("Index:" + index + ",Size:"
                    + size);
        } else {
            // 遍歷所有的節點元素把最後一個節點返回
            for (int i = 1; i <= index; i++) {
                temp = temp.getNext();
            }

            return temp;
        }
    }

    // 6. 返回此列表的第一個元素
    public Object getFirst(){
        Node temp=header;
        temp=temp.getNext();
        return temp.getValue();
    }
    // 7. 返回此列表的最後一個元素
    public Object getLast() {
        Node temp=header;
        while (size>0) {
            temp=temp.getNext();
            size--;
        }
        return temp.getValue();
    }
    // 8. 擷取並移除此列表的頭(第一個元素)
    public Object remove() {
        Node temp=header;
        temp=temp.getNext();
        temp.setValue(null);
        size--;
        return temp.getValue();
    }
    // 9. 移除此列表中指定位置處的元素
    public void remove(int index) {
        get(index).setValue(null);
        size--;
       
    }
   
    // 10. 移除並返回此列表的最後一個元素

    // 11. 返回此列表的元素數
    public int size() {
        return size;
    }

    public static void main(String[] args) {
        LinkedListTest linkedListTest = new LinkedListTest();

        linkedListTest.addhead("指定到header位置添加節點");
        linkedListTest.add("444");
        linkedListTest.add("222");
        linkedListTest.add(2, "指定到2位置添加節點");
        linkedListTest.add(3, "指定到3位置添加節點");
        linkedListTest.add(5, "指定到5位置添加節點");

       
        // 1. 在此列表中指定的位置插入指定的元素
        // 2. 將指定元素插入此列表的開頭
        linkedListTest.addFirst("addFirst");
        linkedListTest.remove(3);
        //System.out.println("返回此列表的第一個元素:"+linkedListTest.getFirst());
        //System.out.println("返回此列表的最後一個元素:"+linkedListTest.getLast());
        //linkedListTest.addLast("addLast");
        linkedListTest.print();

         System.out.println("鏈表長度:" + linkedListTest.size());
        //linkedListTest.clear();
        //System.out.println("鏈表長度:" + linkedListTest.size());
        // System.out.println("索引5的位置得到:" + linkedListTest.get(5).getValue());
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.