Note that there are some explanatory texts on larger screens.

plurals
  1. POpointer vs double pointer for Linked List and Binary Tree
    primarykey
    data
    text
    <ol> <li><p>For single linklist</p> <p>1.1. This is what I saw from a tutorial, I only wrote the important part.</p> <pre><code>sortedInsert(Node **root, int key){}; int main(){ Node *root = &amp;a; sortedInsert(&amp;root, 4); } </code></pre> <p>1.2. However I just used pointer rather than double pointer, and everything works fine, I can insert the key successfully.</p> <pre><code>sortedInsert(Node *root, int key){}; int main(){ Node *root = &amp;a; sortedInsert(root, 4); } </code></pre></li> <li><p>For binary Tree</p></li> </ol> <p>2.1. From tutorial(double pointer)</p> <pre><code> void insert_Tree(Tree **root, int key){ } int main(){ Tree *root = NULL; insert_Tree(&amp;root, 10); } </code></pre> <p>2.2. what I did is below, and I failed to insert the key, when I checked the node after insertion, the node is still null.(single pointer)</p> <pre><code> void insert_Tree(Tree *root, int key){ if(root == NULL){ root = (Tree *)malloc(sizeof(Tree)); root-&gt;val = key; root-&gt;left = NULL; root-&gt;right = NULL; cout&lt;&lt;"insert data "&lt;&lt;key&lt;&lt;endl; }else if(key&lt; root-&gt;val){ insert_Tree(root-&gt;left, key); cout&lt;&lt;"go left"&lt;&lt;endl; }else{ insert_Tree(root-&gt;right, key); cout&lt;&lt;"go right"&lt;&lt;endl; } } int main(){ Tree *root = NULL; insert_Tree(root, 10); } </code></pre> <p>I have a few questions</p> <p>1). which is right, 1.1/2.1 double pointer or 1.2/2.2 single pointer? Please explain in detail, it could be better if you can show an example, I think both of them are right.</p> <p>2). Why did I insert key successfully in the linkedlist with single pointer, however I failed in the tree insertion with single pointer?</p> <p>Thanks very much, I appreciate everyone's help.</p>
    singulars
    1. This table or related slice is empty.
    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