LinkedList的總結

 

//單鏈表LinkedList的原理

class Node

{

       String data; // 存放節點數據本身

       Node next; // 存放指向下一個節點的引用

       public Node(String data)

       {

              this.data = data;

       }

}

public class NodeTest

{

       public static void main(String[] args)

       {

              Node node1 = new Node("node1");

              Node node2 = new Node("node2");

              Node node3 = new Node("node3");

              //鏈接鏈表

              node1.next = node2;

              node2.next = node3;

              System.out.println(node1.next.next.data);

              System.out.println("---------------------");

              //在第一和第二中間,插入新鏈表

              Node node4 = new Node("node4");

              node1.next = node4;

              node4.next = node2;

              System.out.println(node1.next.next.next.data);

              System.out.println("--------------------");

              //刪除新鏈表

              node1.next = node2;

              node4.next = null;

              System.out.println(node1.next.next.data);

                    

       }

}

//雙向循環鏈表LinkedList的原理,還有單向循環鏈表沒有列出來,其實很簡單

class Node2

{

       Node2 previous;

       String data;

       Node2 next;

       public Node2(String data)

       {

              this.data = data;

       }

}

public class Node2Test

{    

       public static void main(String[] args)

       {

              Node2 node1 = new Node2("node1");

              Node2 node2 = new Node2("node2");

              Node2 node3 = new Node2("node3");

              //鏈接鏈表

              node1.next = node2;

              node2.previous = node1;

              node2.next = node3;

              node3.previous = node2;

              node3.next = node1;

              node1.previous = node3;

              System.out.println("--------------------");

              //插入新鏈表

              Node2 node4 = new Node2("node4");

              node1.next = node4;

              node4.previous = node1;

              node4.next = node2;

              node2.previous = node4;

              System.out.println("--------------------");

              //刪除新鏈表

              node1.next = node2;

              node2.previous = node1;

              node4.previous = null;

              node4.next = null; 

       }

}

 

//LinkedList鏈表的使用

import java.util.LinkedList;

public class LinkedListTest1

{    

       public static void main(String[] args)

       {

              LinkedList list = new LinkedList();

              list.add("F");

              list.add("B");

              list.add("D");

              list.add("E");

              list.add("C");

              list.addLast("Z");

              list.addFirst("A");

              list.add(1, "A2");

              System.out.println("最初的集合:" + list);

              list.remove("F");

              list.remove(2);

              System.out.println("變化之後的集合:" + list);

              Object value = list.get(2);

              list.set(2, (String)value + "changed");

              System.out.println("最後的集合:" + list);

       }

}

//用LinedList鏈表實現隊列原理:先進先出

import java.util.LinkedList;

public class MyQueue

{

       private LinkedList list = new LinkedList();

    //插入一個對象

       public void put(Object o)

       {

              list.addLast(o);

       }

    //得到一個對象

       public Object get()

       {

              return list.removeFirst();

       }

    //判斷隊列是否爲空

       public boolean isEmpty()

       {

              return list.isEmpty();

       }

       public static void main(String[] args)

       {

              MyQueue myQueue = new MyQueue();

              myQueue.put("one");

              myQueue.put("two");

              myQueue.put("three");

              System.out.println(myQueue.get());

              System.out.println(myQueue.get());

              System.out.println(myQueue.get());

              System.out.println(myQueue.isEmpty());

       }

 

輸出結果:

one

two

three

true

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章