2012-08-02 71 views
0

我正在使用java中的鏈接列表,我需要獲取x個對象的列表並將奇數位置的對象移動到列表的末尾。java list通過重新排列鏈接來移動項目

我必須通過使用鏈接,沒有新的節點,沒有list.data交換。

當我從一個列表移動到另一個列表時,我覺得我有一個體面的句柄,但遍歷和追加只有一個列表的引用是非常艱難的。

這裏的實際問題---

編寫通過移動到列表中的爲奇數位置否則保留列表順序的所有值結束重新排列整數列表中的元素的方法轉移。例如,假設變量列表存儲以下值:

[0,1,2,3,4,5,6,7] list.shift();應重新排列列表爲:

[0,2,4,6,1,3,5,7] 您必須通過重新排列列表的鏈接來解決此問題。


下面

是,我需要編寫前的方法(與上述限制類。

我真的不能拿出一個行動的計劃。

// A LinkedIntList object can be used to store a list of integers. 
public class LinkedIntList { 
    private ListNode front; // node holding first value in list (null if empty) 
    private String name = "front"; // string to print for front of list 

    // Constructs an empty list. 
    public LinkedIntList() { 
     front = null; 
    } 

    // Constructs a list containing the given elements. 
    // For quick initialization via Practice-It test cases. 
    public LinkedIntList(int... elements) { 
     this("front", elements); 
    } 

    public LinkedIntList(String name, int... elements) { 
     this.name = name; 
     if (elements.length > 0) { 
      front = new ListNode(elements[0]); 
      ListNode current = front; 
      for (int i = 1; i < elements.length; i++) { 
       current.next = new ListNode(elements[i]); 
       current = current.next; 
      } 
     } 
    } 

    // Constructs a list containing the given front node. 
    // For quick initialization via Practice-It ListNode test cases. 
    private LinkedIntList(String name, ListNode front) { 
     this.name = name; 
     this.front = front; 
    } 

    // Appends the given value to the end of the list. 
    public void add(int value) { 
     if (front == null) { 
      front = new ListNode(value, front); 
     } else { 
      ListNode current = front; 
      while (current.next != null) { 
       current = current.next; 
      } 
      current.next = new ListNode(value); 
     } 
    } 

    // Inserts the given value at the given index in the list. 
    // Precondition: 0 <= index <= size 
    public void add(int index, int value) { 
     if (index == 0) { 
      front = new ListNode(value, front); 
     } else { 
      ListNode current = front; 
      for (int i = 0; i < index - 1; i++) { 
       current = current.next; 
      } 
      current.next = new ListNode(value, current.next); 
     } 
    } 

    public boolean equals(Object o) { 
     if (o instanceof LinkedIntList) { 
      LinkedIntList other = (LinkedIntList) o; 
      return toString().equals(other.toString()); // hackish 
     } else { 
      return false; 
     } 
    } 

    // Returns the integer at the given index in the list. 
    // Precondition: 0 <= index < size 
    public int get(int index) { 
     ListNode current = front; 
     for (int i = 0; i < index; i++) { 
      current = current.next; 
     } 
     return current.data; 
    } 

    // Removes the value at the given index from the list. 
    // Precondition: 0 <= index < size 
    public void remove(int index) { 
     if (index == 0) { 
      front = front.next; 
     } else { 
      ListNode current = front; 
      for (int i = 0; i < index - 1; i++) { 
       current = current.next; 
      } 
      current.next = current.next.next; 
     } 
    } 

    // Returns the number of elements in the list. 
    public int size() { 
     int count = 0; 
     ListNode current = front; 
     while (current != null) { 
      count++; 
      current = current.next; 
     } 
     return count; 
    } 

    // Returns a text representation of the list, giving 
    // indications as to the nodes and link structure of the list. 
    // Detects student bugs where the student has inserted a cycle 
    // into the list. 
    public String toFormattedString() { 
     ListNode.clearCycleData(); 

     String result = this.name; 

     ListNode current = front; 
     boolean cycle = false; 
     while (current != null) { 
      result += " -> [" + current.data + "]"; 
      if (current.cycle) { 
       result += " (cycle!)"; 
       cycle = true; 
       break; 
      } 
      current = current.__gotoNext(); 
     } 

     if (!cycle) { 
      result += " /"; 
     } 

     return result; 
    } 

    // Returns a text representation of the list. 
    public String toString() { 
     return toFormattedString(); 
    } 

    // Returns a shorter, more "java.util.LinkedList"-like text representation of the list. 
    public String toStringShort() { 
     ListNode.clearCycleData(); 

     String result = "["; 

     ListNode current = front; 
     boolean cycle = false; 
     while (current != null) { 
      if (result.length() > 1) { 
       result += ", "; 
      } 
      result += current.data; 
      if (current.cycle) { 
       result += " (cycle!)"; 
       cycle = true; 
       break; 
      } 
      current = current.__gotoNext(); 
     } 

     if (!cycle) { 
      result += "]"; 
     } 

     return result; 
    } 


    // ListNode is a class for storing a single node of a linked list. This 
    // node class is for a list of integer values. 
    // Most of the icky code is related to the task of figuring out 
    // if the student has accidentally created a cycle by pointing a later part of the list back to an earlier part. 

    public static class ListNode { 
     private static final List<ListNode> ALL_NODES = new ArrayList<ListNode>(); 

     public static void clearCycleData() { 
      for (ListNode node : ALL_NODES) { 
       node.visited = false; 
       node.cycle = false; 
      } 
     } 

     public int data;   // data stored in this node 
     public ListNode next;  // link to next node in the list 
     public boolean visited; // has this node been seen yet? 
     public boolean cycle;  // is there a cycle at this node? 

     // post: constructs a node with data 0 and null link 
     public ListNode() { 
      this(0, null); 
     } 

     // post: constructs a node with given data and null link 
     public ListNode(int data) { 
      this(data, null); 
     } 

     // post: constructs a node with given data and given link 
     public ListNode(int data, ListNode next) { 
      ALL_NODES.add(this); 
      this.data = data; 
      this.next = next; 
      this.visited = false; 
      this.cycle = false; 
     } 

     public ListNode __gotoNext() { 
      return __gotoNext(true); 
     } 

     public ListNode __gotoNext(boolean checkForCycle) { 
      if (checkForCycle) { 
       visited = true; 

       if (next != null) { 
        if (next.visited) { 
         // throw new IllegalStateException("cycle detected in list"); 
         next.cycle = true; 
        } 
        next.visited = true; 
       } 
      } 
      return next; 
     } 
    } 

// YOUR CODE GOES HERE 

} 
+3

在紙上繪製這樣的結構通常是一個很好的準備步驟來開始攻擊... – 2012-08-02 15:21:48

+1

也許從你的源列表中首先創建兩個列表'0 2 4 6'和'1 3 5 7',然後將它們鏈接起來最後。 – 2012-08-02 15:23:47

+1

我認爲這是'[作業]'? – 2012-08-02 15:24:01

回答

1

看它是這樣的:

首先我們需要某種光標,它會經過列表並指向我們的「當前」節點

第二我們需要一些布爾變量(我將它稱爲INV)初始化爲FALSE ...每當我們移動列表中的一個節點時,我們反轉INV

如果您通過左側列表,第二個元素將被rearanged第一,所以這將是我們光標初始位置

允許採取元件/節點上的參考,並保持該引用作爲中斷標準

開始循環:

現在從列表中刪除當前節點並將其插入列表的末尾(移至en d ...不是光標可能無法與節點移動...)

將光標移動到該節點是正確的,我們只是移動節點的前位置(如果存在)

的如果當前元素是我們的放棄標準(我們移動的第一個元素),我們可以假設列表按照所需的順序排序 - >我們完成 - >退出循環...如果它不是我們的放棄標準...繼續

評估「光標的指數甚至是」 TRUE或FALSE ... XOR與INV

如果結果是TRUE將光標移動到下一個EL ement ...如果是錯誤刪除的節點,並在結束(它移動到結束)

插入它做循環

-

這種做法將不保留訂單,而我們在列表中移動,但是當它完成時將按照期望的順序列出...

INV var用於補償索引在移除節點時移動...(0,1,2,3 ...如果刪除了1並將其放在最後,2將會有一個奇數索引,所以如果我們反過來一舉一動,就會得到「正確」的元素)

相關問題