Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked List merging two lists, debug assertion error
    primarykey
    data
    text
    <p>I have a merge implementation for a Linked List. It accepts two parameters of type List which is a class that includes a <code>Node* head</code> pointer and a struct <code>Node</code> consisting of a <code>typename T data</code> and a <code>Node* next</code>. The issue I'm having is that my implementation isn't linking the nodes as it should, or maybe I'm just going about it wrong. What it needs to do is, if you do <code>list1.merge(list2, list3);</code> then list1 will become the combination of list2 and list3's nodes. I need to do this by pointer manipulation and no new memory allocation, so list2 and list3 will be modified. Here is what I have right now:</p> <pre><code>template &lt;typename T&gt; void List&lt;T&gt;::merge(List&amp; list1, List&amp; list2) { typename List&lt;T&gt;::Node* list1Ptr = list1.head; typename List&lt;T&gt;::Node* list2Ptr = list2.head; for(;;) { if (list1Ptr == NULL &amp;&amp; list2Ptr != NULL) { list1Ptr = list2Ptr-&gt;next; head = list1.head; break; } else if (list2Ptr == NULL &amp;&amp; list1Ptr != NULL) { list2Ptr = list1Ptr-&gt;next; head = list1.head; break; } else if (list1Ptr == NULL &amp;&amp; list2Ptr == NULL) { head = list1.head; break; } else if (list1Ptr != NULL &amp;&amp; list2Ptr != NULL) { if (list1Ptr-&gt;data &gt; list2Ptr-&gt;data){ typename List&lt;T&gt;::Node* temp; temp = list2Ptr-&gt;next; list1Ptr-&gt;next = list1Ptr; list2Ptr = temp; } else if (list1Ptr-&gt;data &lt; list2Ptr-&gt;data) { typename List&lt;T&gt;::Node* temp; temp = list1Ptr-&gt;next; list1Ptr-&gt;next = list2Ptr; list1Ptr = temp; } else if (list1Ptr-&gt;data == list2Ptr-&gt;data) { list1Ptr = list1Ptr-&gt;next; } } } } </code></pre> <p>The data that is contained in the nodes is of a class type that was provided for us, which contains all the proper overloaded operators that we need. The entire code runs just fine, until main goes out of scope and the destructor is called for what remains, after which I get a <code>Debug Assertion Failed Expression: _BLOCK_TYPE_IS_VALID(pHead-&gt;nBlockUse)</code>.</p> <p>I'm really not sure how to go about this, I've drawn it out many many times and it all seems to make sense to me. If anyone has any tips to put me in the right direction, I will greatly appreciate it. Thanks everyone for looking!</p>
    singulars
    1. This table or related slice is empty.
    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. 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