• 双链表的插入


    双链表与单链表的不同之处是 前者有两个根指针, 一个指向首结点, 一个指向尾结点.   后者只有一个,  指向首结点。

    预编译

    #include <stdlib.h>
    #include <stdio.h>
    #define status int
    #define TRUE 1
    #define FALSE 0
    typedef struct NODE{
        int value;            /* 存放结点值 */
        struct NODE *prev;  /* 指向前一个结点 */
        struct NODE *next;  /* 指向下一个结点 */
    }Node;
    数据结构
     1 /*
     2 功能描述:
     3     插入新结点( 从小到大 )
     4 
     5 参数:
     6     rootp -- 指向根结点的双指针
     7     new_value -- 要插入链表的新值
     8 
     9 返回值:
    10     如果插入成功,返回TRUE;
    11     否则,返回FALSE
    12 */
    13 status
    14 Insert( Node **rootp, int new_value )
    15 {
    16     Node *current = *rootp;    
    17     Node *_new = NULL;
    18 
    19     /* 寻找插入位置 */
    20     while ( current != NULL ){
    21         if ( current->value > new_value )
    22             break;
    23 
    24         if ( current->value == new_value ){
    25             printf( "值%d已存在
    ", new_value );
    26             return FALSE;
    27         }
    28         current = current->next;
    29     }
    30 
    31     /* 创建新结点 */
    32     _new = (Node *)malloc( sizeof( Node ) );
    33     if ( _new == NULL ){
    34         printf( "内存不够
    " );
    35         return FALSE;
    36     }
    37     _new->value = new_value;
    38 
    39     /* 插入新结点 */
    40     if ( current == NULL ){
    41         if ( current == *rootp ){    /* 插入到空链表 */
    42             *rootp = _new;
    43             _new->prev = _new;
    44             _new->next = NULL;
    45         
    46         }else{                        /* 插入到尾部 */
    47             _new->next = NULL;
    48             _new->prev = (*rootp)->prev;
    49             (*rootp)->prev->next = _new;
    50             (*rootp)->prev = _new;
    51         }
    52     }else{
    53         if ( current == *rootp ){    /* 插入到根结点前 */
    54             _new->prev = (*rootp)->prev;
    55             _new->next = current;
    56             current->prev = _new;
    57             *rootp = _new;
    58 
    59         }else{                        /* 插入到中间某个结点前 */
    60             _new->prev = current->prev;
    61             current->prev->next = _new;
    62             _new->next = current;
    63             current->prev = _new;
    64         }
    65     }
    66 
    67     return TRUE;
    68 }
    插入代码
     1 /*
     2 功能描述:
     3     插入新结点( 从小到大 )
     4 
     5 参数:
     6     rootp -- 指向根结点的双指针
     7     new_value -- 要插入链表的新值
     8 
     9 返回值:
    10     如果插入成功,返回TRUE;
    11     否则,返回FALSE
    12 */
    13 status
    14 Insert( Node **rootp, int new_value )
    15 {
    16     Node *current = *rootp;    
    17     Node *_new = NULL;
    18 
    19     /* 寻找插入位置 */
    20     while ( current != NULL ){
    21         if ( current->value > new_value )
    22             break;
    23 
    24         if ( current->value == new_value ){
    25             printf( "值%d已存在
    ", new_value );
    26             return FALSE;
    27         }
    28         current = current->next;
    29     }
    30 
    31     /* 创建新结点 */
    32     _new = (Node *)malloc( sizeof( Node ) );
    33     if ( _new == NULL ){
    34         printf( "内存不够
    " );
    35         return FALSE;
    36     }
    37     _new->value = new_value;
    38 
    39     /* 插入新结点 */
    40     if ( current == NULL ){            /* 插入根结点或尾结点 */
    41         if ( current != *rootp ){
    42             (*rootp)->prev->next = _new;        
    43         }
    44         _new->prev = (current == *rootp) ? _new : (*rootp)->prev;
    45         _new->next = NULL;
    46         ((current == *rootp) ? *rootp : (*rootp)->prev ) = _new; 
    47         
    48     }else{                            /* 插入到根结点或中间结点之前 */
    49         _new->prev = current->prev;
    50         ((current == *rootp) ? current->prev : current->prev->next ) = _new;
    51         _new->next = current;
    52         ((current == *rootp) ? *rootp : current->prev ) = _new;
    53     }
    54 
    55     return TRUE;
    56 }
    优化后的插入代码
  • 相关阅读:
    mysql 函数
    flanneld和calico
    k8s1.20.5
    pre
    docker与K8S源远
    Form Trigger Sequence Demo Form
    消息中间件RabbitMQ
    asp.net mvc4压缩混淆JavaScript
    C# 基础语法
    APP测试 理论总结
  • 原文地址:https://www.cnblogs.com/the-one/p/4628335.html
Copyright © 2020-2023  润新知