Jekyll2018-06-02T12:23:18+00:00http://vparticles.com/VParticlesCoding, Design and moreNick Garlisnick@vparticles.comSwap nodes in a Doubly Linked List2017-12-30T00:00:00+00:002017-12-30T00:00:00+00:00http://vparticles.com/data%20stuctures/doubly-linked-lists<p>When I first learned about Doubly Linked Lists what I found really tricky about them was how to sort them and more specifically how to swap two nodes. So I decided to make a post about it.</p>
<p>Let’s assume that our list consists of only 4 nodes. Those are enough for us to cover every possible scenario during swapping.</p>
<p>Our swap function is going to take two parameters, our left and right nodes.</p>
<p><code class="highlighter-rouge">void swap(struct list* left, struct list* right)</code></p>
<p>Before we start messing with <strong>left</strong> and <strong>right’s</strong> pointers we have to make sure that the Nodes pointing to them are updated.</p>
<p>First we make sure that there is a Node that has <strong>left</strong> as next <code class="highlighter-rouge">if ( left->previous )</code> then we make that Node to have <strong>right</strong> as next <code class="highlighter-rouge">left->previous->next = right;</code>. If however, there are no nodes before <strong>left</strong> that means that it is the <strong>head</strong>. In that case, we now have to set our <strong>head</strong> equal to <strong>right</strong> <code class="highlighter-rouge">head = right;</code> since <strong>left</strong> is going to no longer be the <strong>head</strong> after the swap.</p>
<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="k">if</span> <span class="p">(</span> <span class="n">left</span><span class="o">-></span><span class="n">previous</span> <span class="p">){</span>
<span class="n">left</span><span class="o">-></span><span class="n">previous</span><span class="o">-></span><span class="n">next</span> <span class="o">=</span> <span class="n">right</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">head</span> <span class="o">=</span> <span class="n">right</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>We follow a similar procedure for the Node after <strong>right</strong>.</p>
<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="k">if</span> <span class="p">(</span> <span class="n">right</span><span class="o">-></span><span class="n">next</span> <span class="p">){</span>
<span class="n">right</span><span class="o">-></span><span class="n">next</span><span class="o">-></span><span class="n">previous</span> <span class="o">=</span> <span class="n">left</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Now it’s just <strong>left</strong> and <strong>right</strong>. We now simply need to swap <strong>left</strong> and <strong>right’s</strong> pointers.</p>
<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="n">left</span><span class="o">-></span><span class="n">next</span> <span class="o">=</span> <span class="n">right</span><span class="o">-></span><span class="n">next</span><span class="p">;</span>
<span class="n">right</span><span class="o">-></span><span class="n">previous</span> <span class="o">=</span> <span class="n">left</span><span class="o">-></span><span class="n">previous</span><span class="p">;</span>
<span class="n">right</span><span class="o">-></span><span class="n">next</span> <span class="o">=</span> <span class="n">left</span><span class="p">;</span>
<span class="n">left</span><span class="o">-></span><span class="n">previous</span> <span class="o">=</span> <span class="n">right</span><span class="p">;</span>
</code></pre></div></div>
<p>Notice how I assigned <strong>left</strong> to be <strong>right’s</strong> next Node and <strong>right</strong> to be <strong>left’s</strong> previous Node.</p>Nick Garlisnick@vparticles.comWhen I first learned about Doubly Linked Lists what I found really tricky about them was how to sort them and more specifically how to swap two nodes. So I decided to make a post about it.