• C++链表首尾节点的常规方案


        //链表首尾节点的常规方案
        //示例代码片段,不能编译
    
        ///////////////////////////////////////////////////////////////
        //循环,永远非空
        head->next = head;  //头插入
        t->next = x->next; x->next = t;     //x节点后插入t节点
        x->next = x->next->next;            //删除x后的节点
    
        t=head;
        do{ t=t->next; } while(t != head)   //循环遍历
    
        if(head->next ==  head)             //测试是否只有一个元素
    
        ///////////////////////////////////////////////////////////////
        //有头节点,尾节点为null
        head = 0;                           //初始化
    
        if(x==0) {head=t; head->next = 0;}  //在x节点后插入t节点
        else { t->next=x->next; x->next=t;}
    
        t = x->next; x->next=t->next;       //删除x后的节点
        //x->next = x->next->next 表达更简单
    
        for(t=head; t!=0; t=t->next)        //遍历循环
        if(head == 0)                       //测试是否为空
    
        ///////////////////////////////////////////////////////////////
        //有哑元头节点,尾节点为null
        head = new node; head->next=0;      //初始化
        t->next = x->next; x->next = t;     //x节点后插入t节点
        t = x->next; x->next=t->next;       //删除x后的节点
        //x->next = x->next->next 表达更简单
    
        for(t=head->next; t!=0; t=t->next)  //遍历循环
        if(head->next == 0)                 //测试是否为空
    
        ///////////////////////////////////////////////////////////////
        //有哑元头节点和尾节点
        head=new node;                      //初始化
        z=new node;
        head->next=z; z->next=z;
    
        t->next=x->next; x->next=t;         //在x节点后插入t节点
        x->next = x->next->next             //删除x后的节点
        for(t=head->next; t!=z; t=t->next)  //遍历循环
        if(head->next == z)                 //测试是否为空
  • 相关阅读:
    BZOJ 1040 (ZJOI 2008) 骑士
    BZOJ 1037 (ZJOI 2008) 生日聚会
    ZJOI 2006 物流运输 bzoj1003
    ZJOI 2006 物流运输 bzoj1003
    NOI2001 炮兵阵地 洛谷2704
    NOI2001 炮兵阵地 洛谷2704
    JLOI 2013 卡牌游戏 bzoj3191
    JLOI 2013 卡牌游戏 bzoj3191
    Noip 2012 day2t1 同余方程
    bzoj 1191 [HNOI2006]超级英雄Hero——二分图匹配
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2730222.html
Copyright © 2020-2023  润新知