[数据结构]顺序单链表插入

一,单链表插入操作

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. typedef struct NODE {  
  2.     struct NODE *link;  
  3.     int value;  
  4. }Node;  
  5.   
  6. #include <stdlib.h>  
  7. #include <stdio.h>  
  8.   
  9. #define FALSE 0  
  10. #define TRUE 1  
  11.   
  12. int s_insert(Node **rootp,int new_value)  //root是一个指向Node的指针,所以指针root的指针类型是Node**  
  13. {  
  14.     Node *current;  
  15.     Node *new;  
  16.     Node *previous;  
  17.       
  18.     current = *rootp;  
  19.     previous = NULL;  
  20.       
  21.     while(current != NULL && current->value < new_value)  
  22.     {  
  23.         previous = current;  
  24.         current = current->link;  
  25.     }  
  26.       
  27.     new = (Node*)malloc(sizeof(Node));  
  28.     if(new == NULL)  
  29.     {  
  30.         return FALSE;  
  31.     }  
  32.     new->value = new_value;  
  33.       
  34.     new->link = current;  
  35.     if(previous == NULL)  
  36.     {  
  37.         *rootp = new;  
  38.     }  
  39.     else  
  40.     {  
  41.         previous->link = new;  
  42.     }  
  43.     return TRUE;  
  44. }  
原文地址:https://www.cnblogs.com/zhiliao112/p/4237195.html