Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree with @Stephen about not giving the answer (entirely), but I think I should give you hints.</p> <p>An important thing to understand is that Java does not explicitly specify pointers - rather, whenever a non-primitive (e.g. not <code>char</code>, <code>byte</code>, <code>int</code>, <code>double</code>, <code>float</code>, <code>long</code>, <code>boolean</code>, <code>short</code>) is passed to a function, it is passed as a reference. So, you can use temporary variables to swap the values. Try to code one yourself or look below:</p> <pre><code> public static void swapNodeNexts(final Node n1, final Node n2) { final Node n1Next = n1.next; final Node n2Next = n2.next; n2.next = n1Next; n1.next = n2Next; } </code></pre> <p>Then you'll need a data structure to hold the <code>Node</code>s. It's important that there be an even number of <code>Node</code>s only (odd numbers unnecessarily complicate things). It's also necessary to initialize the nodes. You should put this in your main method.</p> <pre><code> public static final int NUMPAIRS = 3; public static void main(final String[] args) { final Node[] nodeList = new Node[NUMPAIRS * 2]; for (int i = 0; i &lt; nodeList.length; i++) { nodeList[i] = new Node(); nodeList[i].n = (i + 1) * 10; // 10 20 30 40 } // ... } </code></pre> <p>The important part is to set the Node next values. You can't just loop through with a <code>for</code> loop for all of them, because then the last one's <code>next</code> would throw an <code>IndexOutOfBoundsException</code>. Try to make one yourself, or peek at mine.</p> <pre><code> for (int i = 0; i &lt; nodeList.length - 1; i++) { nodeList[i].next = nodeList[i + 1]; } nodeList[nodeList.length - 1].next = nodeList[0]; </code></pre> <p>Then run your swap function on them with a <code>for</code> loop. But remember, you don't want to run it on <em>every</em> node… think about it a bit.</p> <p>If you can't figure it out, here is my final code:</p> <pre><code> // Node class Node { public int n; // value public Node next; // pointer to next node @Override public String toString() { return "Node [n=" + n + ", nextValue=" + next.n + "]"; } } // NodeMain public class NodeMain { public static final int NUMPAIRS = 3; public static void main(final String[] args) { final Node[] nodeList = new Node[NUMPAIRS * 2]; for (int i = 0; i &lt; nodeList.length; i++) { nodeList[i] = new Node(); nodeList[i].n = (i + 1) * 10; // 10 20 30 40 } for (int i = 0; i &lt; nodeList.length - 1; i++) { nodeList[i].next = nodeList[i + 1]; } nodeList[nodeList.length - 1].next = nodeList[0]; // This makes 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 1 etc. printNodes(nodeList); for (int i = 0; i &lt; nodeList.length; i += 2) { swapNodeNexts(nodeList[i], nodeList[i + 1]); } // Now: 2 -&gt; 1 -&gt; 4 -&gt; 3 -&gt; 1 etc. printNodes(nodeList); } private static void printNodes(final Node[] nodeList) { for (int i = 0; i &lt; nodeList.length; i++) { System.out.println("Node " + (i + 1) + ": " + nodeList[i].n + "; next: " + nodeList[i].next.n); } System.out.println(); } private static void swapNodeNexts(final Node n1, final Node n2) { final Node n1Next = n1.next; final Node n2Next = n2.next; n2.next = n1Next; n1.next = n2Next; } } </code></pre> <p>I hope you were able to figure out at least some of this with guidance. More importantly, however, it's important that you understand the concepts here. If you have any questions, just leave a comment.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload