代码如下:
/*
** 插入到一个有序的单链表, 函数的参数是一个指向链表第一个节点的指针,
** 以及一个需要插入的新值
*/
#include <stdio.h>
#include <stdlib.h>
#include "sll_node.h"
#define FALSE 0
#define TRUE 1
int
sll_insert( register Node **linkp, int new_value )
{
register Node *current;
register Node *new;
/*
** 寻找正确的插入位置,方法是按序访问链表,直到到达一个其值大于或等于新值的节点
*/
while( ( current = *linkp ) != NULL && current->value < new_value )
linkp = ¤t -> link;
/*
** 为新节点分配内存, 并把新值存储到新节点中,如果内存分配失败,函数返回FALSE
*/
new = ( Node *)malloc( sizeof(int));
if (new == NULL )
return FALSE;
new->value = new_value;
/*
** 在链表中插入新节点,并返回TURE
*/
new->link = current;
*linkp = new;
return TURE;
}