单向链表

<textarea cols="50" rows="15" name="code" class="c-sharp">/*&sup2;&aring;&Egrave;&euml;&micro;&frac12;&Ograve;&raquo;&cedil;&ouml;&Oacute;&ETH;&ETH;&ograve;&micro;&yen;&Iuml;&ograve;&Aacute;&acute;&plusmn;&iacute;&pound;&not;&ordm;&macr;&Ecirc;&yacute;&micro;&Auml;&sup2;&Icirc;&Ecirc;&yacute;&Ecirc;&Ccedil;&Ograve;&raquo;&cedil;&ouml;&Ouml;&cedil;&Iuml;&ograve;&Aacute;&acute;&plusmn;&iacute;&micro;&Uacute;&Ograve;&raquo;&cedil;&ouml;&frac12;&Uacute;&micro;&atilde;&micro;&Auml;&Ouml;&cedil;&Otilde;&euml;&pound;&not;&Ograve;&Ocirc;&frac14;&deg;&Ograve;&raquo;&cedil;&ouml;&ETH;&egrave;&Ograve;&ordf;&sup2;&aring;&Egrave;&euml;&micro;&Auml;&ETH;&Acirc;&Ouml;&micro;*/ #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &quot;sll_node.h&quot; #define FALSE 0 #define TRUE 1 int sll_insert(register Node **linkp,int new_value) { register Node *current; register Node *new; /*&Ntilde;&deg;&Otilde;&Ograve;&Otilde;&yacute;&Egrave;&middot;&micro;&Auml;&sup2;&aring;&Egrave;&euml;&Icirc;&raquo;&Ouml;&Atilde;&pound;&not;&middot;&frac12;&middot;&uml;&Ecirc;&Ccedil;&deg;&acute;&ETH;&ograve;&middot;&Atilde;&Icirc;&Ecirc;&Aacute;&acute;&plusmn;&iacute;&pound;&not;&Ouml;&plusmn;&micro;&frac12;&micro;&frac12;&acute;&iuml;&Ograve;&raquo;&cedil;&ouml;&AElig;&auml;&Ouml;&micro;&acute;&oacute;&Oacute;&Uacute;&raquo;&ograve;&micro;&Egrave;&Oacute;&Uacute;&ETH;&Acirc;&Ouml;&micro;&micro;&Auml;&frac12;&Uacute;&micro;&atilde;*/ while((current = *linkp) != NULL &amp;&amp; current-&gt;value &lt; new_value) linkp = &amp;current-&gt;link; /*&Icirc;&ordf;&ETH;&Acirc;&frac12;&Uacute;&micro;&atilde;&middot;&Ouml;&Aring;&auml;&Auml;&Uacute;&acute;&aelig;&pound;&not;&sup2;&cent;&deg;&Ntilde;&ETH;&Acirc;&Ouml;&micro;&acute;&aelig;&acute;&cent;&micro;&frac12;&ETH;&Acirc;&frac12;&Uacute;&micro;&atilde;&Ouml;&ETH;&pound;&not;&Egrave;&ccedil;&sup1;&ucirc;&Auml;&Uacute;&acute;&aelig;&middot;&Ouml;&Aring;&auml;&Ecirc;&sect;&deg;&Uuml;&pound;&not;&ordm;&macr;&Ecirc;&yacute;&middot;&micro;&raquo;&Oslash;FALSE*/ new = (Node *)malloc(sizeof(Node)); if(new == NULL) return FALSE; new-&gt;value = new_value; /*&Ocirc;&Uacute;&Aacute;&acute;&plusmn;&iacute;&Ouml;&ETH;&sup2;&aring;&Egrave;&euml;&ETH;&Acirc;&frac12;&Uacute;&micro;&atilde;&pound;&not;&sup2;&cent;&middot;&micro;&raquo;&Oslash;TRUE*/ new-&gt;link = current; *linkp = new; return TRUE; }&nbsp;</textarea>

<textarea cols="50" rows="15" name="code" class="cpp">typedef struct NODE{ struct NODE *link; int value; }Node; sll_node.h </textarea>

 

单向链表插入的改进

 

<textarea cols="50" rows="15" name="code" class="cpp">/*&sup2;&aring;&Egrave;&euml;&micro;&frac12;&Ograve;&raquo;&cedil;&ouml;&Oacute;&ETH;&ETH;&ograve;&micro;&yen;&Iuml;&ograve;&Aacute;&acute;&plusmn;&iacute;&pound;&not;&ordm;&macr;&Ecirc;&yacute;&micro;&Auml;&sup2;&Icirc;&Ecirc;&yacute;&Ecirc;&Ccedil;&Ograve;&raquo;&cedil;&ouml;&Ouml;&cedil;&Iuml;&ograve;&Aacute;&acute;&plusmn;&iacute;&cedil;&ugrave;&Ouml;&cedil;&Otilde;&euml;&micro;&Auml;&Ouml;&cedil;&Otilde;&euml;&pound;&not;&Ograve;&Ocirc;&frac14;&deg;&Ograve;&raquo;&cedil;&ouml;&ETH;&egrave;&Ograve;&ordf;&sup2;&aring;&Egrave;&euml;&micro;&Auml;&ETH;&Acirc;&Ouml;&micro;*/ #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &quot;sll_node.h&quot; #define FALSE 0 #define TRUE 1 int sll_insert(Node **rootp,int new_value) { Node *current; Node *previous; Node *new; /*&micro;&Atilde;&micro;&frac12;&Ouml;&cedil;&Iuml;&ograve;&micro;&Uacute;&Ograve;&raquo;&cedil;&ouml;&frac12;&Uacute;&micro;&atilde;&micro;&Auml;&Ouml;&cedil;&Otilde;&euml;*/ current = *rootp; previous = NULL; /*&Ntilde;&deg;&Otilde;&Ograve;&Otilde;&yacute;&Egrave;&middot;&micro;&Auml;&sup2;&aring;&Egrave;&euml;&Icirc;&raquo;&Ouml;&Atilde;&pound;&not;&middot;&frac12;&middot;&uml;&Ecirc;&Ccedil;&deg;&acute;&ETH;&ograve;&middot;&Atilde;&Icirc;&Ecirc;&Aacute;&acute;&plusmn;&iacute;&pound;&not;&Ouml;&plusmn;&micro;&frac12;&micro;&frac12;&acute;&iuml;&Ograve;&raquo;&cedil;&ouml;&AElig;&auml;&Ouml;&micro;&acute;&oacute;&Oacute;&Uacute;&raquo;&ograve;&micro;&Egrave;&Oacute;&Uacute;&ETH;&Acirc;&Ouml;&micro;&micro;&Auml;&frac12;&Uacute;&micro;&atilde;*/ while(current != NULL &amp;&amp; current-&gt;value &lt; new_value){ previous = current; current = current-&gt;link; } /*&Icirc;&ordf;&ETH;&Acirc;&frac12;&Uacute;&micro;&atilde;&middot;&Ouml;&Aring;&auml;&Auml;&Uacute;&acute;&aelig;&pound;&not;&sup2;&cent;&deg;&Ntilde;&ETH;&Acirc;&Ouml;&micro;&acute;&aelig;&acute;&cent;&micro;&frac12;&ETH;&Acirc;&frac12;&Uacute;&micro;&atilde;&Ouml;&ETH;&pound;&not;&Egrave;&ccedil;&sup1;&ucirc;&Auml;&Uacute;&acute;&aelig;&middot;&Ouml;&Aring;&auml;&Ecirc;&sect;&deg;&Uuml;&ordm;&macr;&Ecirc;&yacute;&middot;&micro;&raquo;&Oslash;FALSE*/ new = (Node *)malloc(sizeof(Node)); if(new == NULL) return FALSE; new-&gt;value = new_value; /*&deg;&Ntilde;&ETH;&Acirc;&frac12;&Uacute;&micro;&atilde;&sup2;&aring;&Egrave;&euml;&micro;&frac12;&Aacute;&acute;&plusmn;&iacute;&Ouml;&ETH;&pound;&not;&sup2;&cent;&middot;&micro;&raquo;&Oslash;TRUE*/ new-&gt;link = current; if(previous == NULL) *rootp = new; else previous-&gt;link = new; return TRUE; }</textarea>

 

 

插入函数的参数Node **rootp,使用指针的指针是为了能够让程序访问到root;

特别注意在开头和末尾对链表的插入。


下次复习双向链表

你可能感兴趣的:(struct,null,insert)