 <p>Without a <a href="http://stackoverflow.com/help/mcve">Minimal, Complete, and Verifiable example</a> nobody can really help you. Nevertheless, this sticks out in a major way:</p> <pre><code>if(prev-&gt;next=NULL) { prev-&gt;next=temp; } </code></pre> <p>You assign <code>NULL</code> to <code>prev-&gt;next</code> instead of comparing it with <code>==</code>.</p>