zoukankan      html  css  js  c++  java
  • 双向链表插入删除

    #include <stdio.h>
    #include 
    <stdlib.h>

    typedef 
    struct node_t {
        
    int                data;
        
    struct    node_t*    prev;
        
    struct    node_t*    next;
    } node;

    node
    * init( void )
    {
        node
    * p;

        p 
    = (node *)malloc(sizeof(node));
        
    if( p == NULL ) {
            printf(
    "malloc error\n");
            
    return 0;
        }

        p
    ->prev = NULL;
        p
    ->next = NULL;
        p
    ->data = 0;

        
    return p;
    }

    /*    insert a new node after specified postion, postion 0 is available    */
    int insert_node( node* head, int pos, int data )
    {
        node
    *    p;
        node
    *    q;

        
    if ( head == NULL || pos < 0 ) {
            printf(
    "parameter error\n");
            
    return -1;
        }

        q 
    = head;
        
    while ( pos-- > 0 && q != NULL ) {
            q 
    = q->next;
        }

        
    if ( q == NULL ) {
            printf(
    "parameter error\n");
            
    return -1;
        }

        p 
    = (node *)malloc(sizeof(node));
        
    if( p == NULL ) {
            printf(
    "malloc error\n");
            
    return -1;
        }

        p
    ->prev = q;
        p
    ->next = q->next;
        p
    ->data = data;

        
    /*    Is the last node ?    */
        
    if ( q->next )
            q
    ->next->prev = p;
        q
    ->next    = p;

        
    return 0;
    }

    /*    insert a new node after specified postion    */
    int remove_node( node* head, int pos )
    {
        node
    *    q;

        
    if ( head == NULL || pos <= 0 ) {
            printf(
    "parameter error\n");
            
    return -1;
        }

        q 
    = head;
        
    while ( pos-- > 0 && q != NULL ) {
            q 
    = q->next;
        }

        
    if ( q == NULL ) {
            printf(
    "parameter error\n");
            
    return -1;
        }

        q
    ->prev->next = q->next;
        
    /*    Is the last node ?    */
        
    if ( q->next )
            q
    ->next->prev = q->prev;
        free( q );

        
    return 0;
    }

    void print_node( node* head )
    {
        node
    * p;

        p 
    = head->next;
        
    while ( p ) {
            printf(
    "%d ", p->data);
            p 
    = p->next;
        }
        printf(
    "\n");
    }

    int main( void )
    {
        node
    * head;
        
    int n = 5;

        head 
    = init();
        
    while ( n > 0 ) {
            insert_node( head, 
    0, n );
            n
    --;
        }

        print_node( head );
        insert_node( head, 
    1010);
        insert_node( head, 
    00);
        print_node( head );
        insert_node( head, 
    66);
        print_node( head );

        remove_node( head, 
    0 );
        remove_node( head, 
    10 );
        remove_node( head, 
    6 );
        print_node( head );
        remove_node( head, 
    6 );
        print_node( head );

        
    return 0;
    }

  • 相关阅读:
    2015-05-27 用正则把oracle时间转化到mysql时间
    linux版idea14界面美观和windows,mac基本一致
    ubuntu 下自定义快捷键,,用着舒服
    ubuntu 方便好用的截图软件
    Integer 包装器类 大小比较
    win7、ubuntu双系统,遇到分区不可用问题,和卸载ubuntu后win7开不了机
    巧妙小思想
    读取16进制文件和校验图片格式的问题。 文件名后缀
    旧电脑变废为宝!
    Win10打开Autodesk软件时提示“管理员已阻止你运行此应用”
  • 原文地址:https://www.cnblogs.com/faraway/p/1243106.html
Copyright © 2011-2022 走看看