How do you create a swap function for doubly linked lists?
<p>I'm having issues creating a swap function for a doubly linked list. I want to simply "rewire" the list not change any values (I know this would be easy). I tried to create this temporary item to hold <code>back&lt;-p-&gt;front</code> so that I could set q = to this front and back but the temp item changes along with p. How can I swap these items without a temporary item or how do I get my temporary item to behave.</p>

<pre><code>void DLinkedList::swap(Item *p, Item *q)
{
    Item* temp = p;
    p-&gt;next = q-&gt;next;
    p-&gt;pre = q-&gt;pre;
    if (p-&gt;next != NULL)
        p-&gt;next-&gt;pre = p;
    if (q-&gt;next != NULL)
        q-&gt;next-&gt;pre = q;
    q-&gt;next = temp-&gt;next;
    q-&gt;pre = temp-&gt;pre;
    if (p-&gt;pre != NULL)
        p-&gt;pre-&gt;next = p;
    if (!q-&gt;pre == NULL) {
        q-&gt;pre-&gt;next = q;
    }
    cout &lt;&lt; "- The items " &lt;&lt; p-&gt;val &lt;&lt; " &amp; " &lt;&lt; q-&gt;val &lt;&lt; " were swapped -" &lt;&lt; endl;
}
</code></pre>