zoukankan      html  css  js  c++  java
  • 编程12:在单链表和双链表中删除倒数第K个结点

    <?php
    header("content-type:text/html;charset=utf-8");
    /*
     *在单链表和双链表中删除倒数第K个结点 P35
     */
    class SingleNode{
        public $value;
        public $next;
        public function __construct($value)
        {
            $this->value = $value;
        }
    }
    
    function removeSingleLastKthNode(SingleNode $head,$Kth){
        if($head == null || $Kth == 0){
            return $head;
        }
        $cur = $head;
        while ($cur != null){
            $Kth--;
            $cur = $cur->next;
        }
        if($Kth > 0){
            return $head;
        }
        elseif ($Kth == 0){
            return $head->next;
        }
        else{
            $cur = $head;
            while (++$Kth != 0){
     //           $Kth++;
                $cur = $cur->next;
            }
            $cur->next = $cur->next->next;
            return $head;
        }
    }
    
    class DoubleNode{
        public $value;
        public $last;
        public $next;
        public function __construct($value)
        {
            $this->value = $value;
        }
    }
    
    function removeDoubleLastKthNode(DoubleNode $head,$Kth){
        if($head == null || $Kth == 0){
            return $head;
        }
        $cur = $head;
        while ($cur != null){
            $Kth--;
            $cur = $cur->next;
        }
    
        if ($Kth == 0){
            $head =  $head->next;
            $head->last = null;
        }
        if($Kth < 0){
            $cur = $head;
            while (++$Kth != 0){
      //          $Kth ++;
                $cur = $cur->next;
            }
            $next = $cur->next->next;
            $cur->next = $next;
            if($next != null){
                $next->last = $cur;
            }
    
        }
        return $head;
    }
    
    
    $head1 = new SingleNode(1);
    $head1->next = new SingleNode(3);
    $head1->next->next = new SingleNode(5);
    $head1->next->next->next = new SingleNode(7);
    $head1->next->next->next->next = new SingleNode(9);
    $head1->next->next->next->next->next = new SingleNode(10);
    $head1->next->next->next->next->next->next = new SingleNode(11);
    $head1->next->next->next->next->next->next->next = new SingleNode(12);
    
    echo "单链表为:";
    echo "</br>";
    print_r($head1);
    echo "</br>";
    echo "</br>";
    echo "删除倒数第三个结点后的单链表为:";
    echo "</br>";
    print_r(removeSingleLastKthNode($head1,3));
    
    
    echo "</br>";
    echo "</br>";
    $head2 = new DoubleNode(1);
    $head2->last = null;
    $head2->next = new DoubleNode(3);
    $head2->next->last = $head2;
    $head2->next->next = new DoubleNode(5);
    $head2->next->next->last = $head2->next;
    $head2->next->next->next = new DoubleNode(7);
    $head2->next->next->next->last = $head2->next->next;
    $head2->next->next->next->next = null;
    
    
    echo "双向链表为:";
    echo "</br>";
    print_r($head2);
    echo "</br>";
    echo "</br>";
    echo "删除倒数第三个结点后的双向链表为:";
    echo "</br>";
    print_r(removeDoubleLastKthNode($head2,3));

    输出结果:

  • 相关阅读:
    cocos2dx3.0戳青蛙游戏(打地鼠)
    深入理解Tomcat系列之五:Context容器和Wrapper容器
    linux下拷贝隐藏文件
    8.8.1 运行计划
    UVALive
    堆排序实现
    C语言中的signal函数
    uboot和内核分区的改动
    Android缩放动画
    .Net 自定义应用程序配置
  • 原文地址:https://www.cnblogs.com/xlzfdddd/p/10027869.html
Copyright © 2011-2022 走看看