Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert new node at the beginning of Linked-List
    primarykey
    data
    text
    <p>In a simple Linked List implementation on C, I couldn’t figure out a line of function named insert(). It takes a char and add to the linked list in alphabetical order. The line is about creating a new node when the list is empty. And since there will be only one node on the list, the line should be like I’ve commented, am I wrong?</p> <pre><code>/****************************************************/ void insert( ListNodePtr *sPtr, char value ){ ListNodePtr newPtr; ListNodePtr previousPtr; ListNodePtr currentPtr; newPtr = malloc( sizeof( ListNode) ); if( newPtr != NULL ){ //is space available newPtr-&gt;data = value; //place value in node newPtr-&gt;nextPtr = NULL; //node does not link to another node previousPtr = NULL; currentPtr = *sPtr; //indirection to startPtr while( currentPtr != NULL &amp;&amp; value &gt; currentPtr-&gt;data ){ previousPtr = currentPtr; //walk to ... currentPtr = currentPtr-&gt;nextPtr; //... next node } //insert new node at the beginning of the list if( previousPtr == NULL ){ newPtr-&gt;nextPtr = *sPtr; /////////////////////////////////////////////// newPtr-&gt;nextPtr = NULL ??? *sPtr = newPtr; } else{ //insert new node between previousPtr and currentPtr previousPtr-&gt;nextPtr = newPtr; newPtr-&gt;nextPtr = currentPtr; } } else printf( "%c not inserted. No memory available.\n", value); }//end-of insert /*******************************************************/ </code></pre> <p>the typedef instructions in main() are;</p> <pre><code>typedef struct listNode ListNode; typedef ListNode* ListNodePtr; </code></pre> <p>and the function insert() is called in main() like this;</p> <pre><code>insert( &amp;startPtr, item); </code></pre> <p>initialization of startPointer in main();</p> <pre><code>ListNodePtr startPtr = NULL; </code></pre>
    singulars
    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.
 

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