(69條消息) 單鏈表(ListNode)及操作其基礎方法

(69條消息) 單鏈表(ListNode)及操作其基礎方法,第1張

在做算法題的過程中,縂會遇見單鏈表相關的題,在Java中用需要自己定義一個ListNode類來生成鏈表對象,閑來無事就寫了一個ListNode,對鏈表的操作直接在ListNode裡進行了,實際用処不大,僅供蓡考。如有問題煩請大家指正。下邊上代碼:

public class ListNode T {  private T val;  private ListNode T next;  private ListNode T head; public ListNode() { } public ListNode(T a) { this(); head = new ListNode T val = a; head.next = this; }  public void add(T a) { ListNode T node = new ListNode T if (head == null) { head = new ListNode T head.next = node.head.next; return; } else { ListNode T temp = head; while (temp.next != null) { temp = temp.next; } temp.next = node.head.next; } }  public void add(int index, T a) { if (!isPositionIndex(index)) { throw new IndexOutOfBoundsException(String.valueOf(index)); } ListNode T node = new ListNode T ListNode T temp = head; while (index 0) { temp = temp.next; index--; } ListNode T t = new ListNode (); t = temp.next; temp.next = node; node.next = t; }  public void add(ListNode T node) { if (node.head.next != null) { if (head == null) { head = new ListNode T head.next = node.head.next; } else { ListNode T temp = head; while (temp.next != null) { temp = temp.next; } temp.next = node.head.next; } } }  public T remove(int index) { if (!isPositionIndex(index)) { throw new IndexOutOfBoundsException(String.valueOf(index)); } ListNode T temp = head; while (index = 0) { temp = temp.next; index--; } temp.next = temp.next.next; return temp.val; }  public boolean remove(T o) { if (head == null) { return false; } ListNode T temp = head; while (temp.next != null) { if (o == null) { if (o == temp.next.val) { temp.next = temp.next.next; return true; } } else { if (o.equals(temp.next.val)) { temp.next = temp.next.next; return true; } } temp = temp.next; } return false; }  public T get(int index) { return this.getListNode(index).val; }  public T set(int index, T o) { ListNode T node = this.getListNode(index); T oldVal = node.val; node.val = o; return oldVal; }  private ListNode T getListNode(int index) { if (!isPositionIndex(index)) { throw new IndexOutOfBoundsException(String.valueOf(index)); } ListNode T temp = head; while (index = 0) { temp = temp.next; index--; } return temp; }  public int get(T o) { if (head == null) { return -1; } int index = 0; ListNode T temp = head; while (temp.next != null) { if (o == null) { if (o == temp.next.val) { return index; } } else { if (o.equals(temp.next.val)) { return index; } } temp = temp.next; index  ; } return -1; }  public void printListNode() { if (head == null) { return; } ListNode T temp = head; System.out.print("打印鏈表:"); while (temp.next != null) { System.out.print(temp.next.val  ""); temp = temp.next; } }  public int length() { int count = 0; if (head == null) { return count; } ListNode T temp = head; while (temp.next != null) { temp = temp.next; count  ; } return count; }  public void clear() { if (head == null) { return; } ListNode T temp = head; while (temp.next != null) { ListNode T node = temp.next; temp.head = null; temp.val = null; temp.next = null; temp = node; } }  private boolean isPositionIndex(int index) { if (index 0 || index this.length()) { return false; } return true; }  public static void printListNode(ListNode ? node) { if (node.head == null) { return; } ListNode ? temp = node.head; System.out.print("打印鏈表:"); while (temp.next != null) { System.out.print(temp.next.val  ""); temp = temp.next; } } public ListNode T getNext() { return next; } public void setNext(ListNode T next) { this.next = next; } public T val() { return val; }}

 


本站是提供個人知識琯理的網絡存儲空間,所有內容均由用戶發佈,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵擧報。

生活常識_百科知識_各類知識大全»(69條消息) 單鏈表(ListNode)及操作其基礎方法

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情