2012-08-02 77 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将会有一个奇数索引,所以如果我们反过来一举一动,就会得到“正确”的元素)

相关问题