2010-11-17 180 views
1

我的一個朋友想讓我把他的代碼轉換成一個雙向鏈表,儘管我並不熟悉它。我查找了雙重鏈表,但我無法告訴他的代碼如何處理它。我不是主程序員。你有什麼建議嗎?單向鏈表到雙向鏈表

import java.util.Collection; 

import java.util.List; 


class SinglyLinkedList<E> implements List<E> { 



private class SinglyLinkedListNode<T> { 



    T data; 

    SinglyLinkedListNode<T> next; 



    public SinglyLinkedListNode() { 

     this(null, null); 

    } 



    public SinglyLinkedListNode(T data) { 

     this(data, null); 

    } 



    public SinglyLinkedListNode(T d, SinglyLinkedListNode<T> n) { 

     data = d; 

     next = n; 

    } 



    public boolean equals(Object o) { 

     if (data != null && o != null) { 

      return data.equals(((SinglyLinkedListNode) o).data); 

     } else { 

      return (data == null && o == null); 

     } 

    } 

} 

private SinglyLinkedListNode<E> list, last; 

private int size; 



public SinglyLinkedList() { 

    clear(); 

} 



public void clear() { 

    list = last = null; 

    size = 0; 

} 



public boolean contains(Object o) { 

    SinglyLinkedListNode<E> t = list; 

    while (t != null) { 

     if (t.data == null) { 

      if (o == null) { 

       return true; 

      } 

     } else if (t.data.equals(o)) { 

      return true; 

     } 

     t = t.next; 

    } 

    return false; 

} 



public boolean add(E e) { 

    SinglyLinkedListNode<E> n = new SinglyLinkedListNode<E>(e); 

    if (isEmpty()) { 

     list = last = n; 

    } else { 

     last = last.next = n; 

    } 

    size++; 

    return true; 

} 



public void add(int index, E e) { 

    int currSize = size(); 

    if (index < 0 || index > currSize) { 

     throw new IndexOutOfBoundsException(

       "Index: " + index + ", Size: " + size()); 

    } 

    if (isEmpty()) // index must == 0 

    { 

     list = last = new SinglyLinkedListNode<E>(e); 

    } else { 

     if (index == 0) { 

      list = new SinglyLinkedListNode<E>(e, list); 

     } else { 

      SinglyLinkedListNode<E> n = list; 

      for (int i = 0; i < index - 1; i++) { 

       n = n.next; 

      } 

      n.next = new SinglyLinkedListNode<E>(e, n.next); 

      if (index == currSize) { 

       last = n.next; 

      } 

     } 

    } 

    size++; 

} 



public boolean equals(SinglyLinkedList<E> e) { 


    SinglyLinkedListNode<E> e1 = list, e2 = e.list; 

    try { 

     for (int i = 1; i <= size(); i++) { 

      if (!e1.equals(e2)) { 

       return false; 

      } 

      e1 = e1.next; 

      e2 = e2.next; 

     } 

    } catch (NullPointerException ex) { 

     return false; 

    } 

    return true; 

} 



public E get(int index) { 

    if (index < 0 || index >= size()) { 

     throw new IndexOutOfBoundsException(

       "Index: " + index + ", Size: " + size()); 

    } 

    SinglyLinkedListNode<E> n = list; 

    int i = 0; 

    for (; i < index; i++) { 

     n = n.next; 

    } 

    return n.data; 

} 



@SuppressWarnings("unchecked") 

public int indexOf(Object o) { 

    SinglyLinkedListNode<E> n = list; 

    int i = 0; 

    while (n != null) { 

     if ((o == null 

       ? (n.data == null) 

       : (((E) o).equals(n.data)))) { 

      return i; 

     } 

     n = n.next; 

     i++; 

    } 

    return -1; 

} 



public boolean isEmpty() { 

    return list == null; 

} 



public E remove(int index) { 

    if (index < 0 || index >= size()) { 

     throw new IndexOutOfBoundsException(

       "Index: " + index + ", Size: " + size()); 

    } 

    SinglyLinkedListNode<E> n = list, prevNode = null; 

    int i = 0; 

    while (true) { 

     if (index == i) { 

      if (n == list) // removing first node 

      { 

       list = list.next; 

      } else { 

       prevNode.next = n.next; 

      } 

      if (n == last) { 

       last = prevNode; 

      } 

      size--; 

      return n.data; 

     } 

     prevNode = n; 

     n = n.next; 

     i++; 

    } 

} 



@SuppressWarnings("unchecked") 

public boolean remove(Object o) { 

    SinglyLinkedListNode<E> n = list, prevNode = null; 

    while (n != null) { 

     if ((o == null 

       ? (n.data == null) 

       : (((E) o).equals(n.data)))) { 

      if (n == list) //removing first node 

      { 

       list = list.next; 

      } else { 

       prevNode.next = n.next; 

      } 

      if (n == last) { 

       last = prevNode; 

      } 

      size--; 

      return true; 

     } 

     prevNode = n; 

     n = n.next; 

    } 

    return false; 

} 



public int size() { 

    return size; 

} 



public String toString() { 

    String s = "(("; 

    SinglyLinkedListNode<E> t = list; 

    if (t != null) { 

     while (t.next != null) { 

      s += t.data + ", "; 

      t = t.next; 

     } 

     s += last.data; 

    } 

    return s + "))"; 

} 
+14

你的朋友是你的CS老師嗎? – shoebox639 2010-11-17 19:59:58

+5

這功課嗎?爲什麼你的「朋友」不能自己發佈這個問題? – MAK 2010-11-17 20:01:15

+0

@ shoebox639我希望我可以再次讓你高興 – 2010-11-17 20:12:35

回答

1

我不明白這個問題。如果這是作業,你應該這麼說 - 社區規則!簡單解釋,無論:

鏈表是用下面的結構,...好,結構:

DATA      |--> DATA 
REFERENCE TO NEXT ITEM ---| REFERENCE TO NEXT ITEM ---... 

每一個「鏈接」中的「產業鏈」包含了一些數據和方法找到鏈中的下一個項目。正如你所說,這是一個單獨的鏈表。

雙向鏈表是一個非常相似的結構,只有鏈中的每個鏈接既包含查找下一個項目的方法,也包含查找前一個項目的方法。如果你需要能夠以兩種方式走列表,你需要這種結構。

|-> DATA      |--> DATA 
| REFERENCE TO NEXT ITEM ---| REFERENCE TO NEXT ITEM ---... 
---------------------------------- REFERENCE TO PREV ITEM 

Ooookay的「圖紙」是可怕的。您可以查看雙向鏈接列表與谷歌查詢是什麼,並獲得更好的信息,第二個想法,但哦。

+0

事實上,顯然功能標籤現在正式被阻止(至少這是某人告訴我的)。官方的建議是公開承認它是作業,但要避免'元'標籤(與問題內容無關)。 – Crisfole 2010-11-17 20:05:22

+0

哦,看看!編輯,謝謝。很高興知道。 – slezica 2010-11-17 20:09:36

0

每個節點需要一個next以及一個previous佔位符,它是一個雙向鏈接列表。在Java中是

0

LinkedList是一個雙向鏈表。你爲什麼想要自己創建一個?

0

聽起來像家庭作業。這會讓你開始。

http://en.wikipedia.org/wiki/Doubly_linked_list

基本上,在單鏈表每個節點都有一個指針到下一個節點。在雙向鏈表中,有指向下一個和前一個的指針。請小心指針如何在列表的開頭和結尾工作。