zoukankan      html  css  js  c++  java
  • 单链表的插入

    预编译

    #include <stdio.h>
    #include <stdlib.h>
    #define status int
    #define TRUE 1
    #define FALSE 0

    数据结构

    typedef struct NODE{
        struct NODE *next;        /* 指向下一个结点 */
        int value;                /* 存放结点值 */
    }Node, *PNode, **List;

    插入代码

     1 /*
     2 功能描述:
     3     把一个值插入到单链表(从小到大)
     4 
     5 参数:
     6     rootp -- 指向根结点
     7     new_value -- 存放新值
     8 
     9 返回值:
    10     如果插入成功,返回TRUE;否则,返回FALSE 
    11 */
    12 status
    13 Insert( List linkp, int new_value )
    14 {
    15     if ( linkp == NULL ){
    16         return FALSE;
    17     }
    18 
    19     Node *current = NULL;
    20 
    21     /* 寻找插入位置 */
    22     while ( (current = *linkp) != NULL &&  current->value <= new_value ){
    23         if ( current->value == new_value ){
    24             printf( "%d已存在
    ", new_value );
    25             return FALSE;
    26         }
    27 
    28         linkp = &current->next;
    29     }
    30 
    31     /* 创建新结点 */
    32     Node *_new = (PNode) malloc ( sizeof( Node ) );
    33     if ( _new == NULL ){
    34         printf( "内存不足
    " );
    35         return FALSE;
    36     }
    37     _new->value = new_value;
    38 
    39     /* 插入新结点 */
    40     _new->next = current;
    41     *linkp = _new;    
    42     return TRUE;
    43 }
  • 相关阅读:
    gorm 更新数据时,0值会被忽略
    xshell评估过期解决办法
    安装zoom
    aria2 加速百度网盘下载
    ubuntu17.10 安装firefox的flash
    c++ 回调函数使用
    ubuntu17 安装中文输入法
    ubuntu python3.6 找不到_sqlite3
    linux 获取CPU个数
    centos7 yum与Python3冲突
  • 原文地址:https://www.cnblogs.com/the-one/p/4634428.html
Copyright © 2011-2022 走看看