douda5706 2011-09-09 00:01
浏览 36

如何在php中创建循环链表?

I have made a single linkedlist in php now i wish to make it circular , any help is really appreciated

code for linkedList

class listNode{


    public $data;
    public  $next;

    public function __construct($data)
    {
        $this->data=$data;
        $this->next=null;
    }
}


class linkedList {

    public $firstNode;

    public $lastNode;

    public $link;





    public function __construct()
    {
        $this->firstNode = NULL;
        $this->lastNode = NULL;
        $this->link=NULL;
    }

    public function insertFirst($data)
    {


        $tempStore=new listNode($data);
        $this->firstNode=clone($tempStore);
        $tempStore->next=$this->link;

        $this->link=$tempStore;


        if($this->lastNode == NULL){
            $this->lastNode = $this->link;
            }

    }

    public function insertLast($data)
    {

        if($this->firstNode==null)
        {
            $this->insertFirst($data);
        }else{
            $tempStore=new listNode($data);
            $this->lastNode->next=$tempStore;

            print_r($this->lastNode);
            $this->lastNode=$tempStore;
            print_r($this->lastNode);

        }

    }


    public function makeCircular()
    {



    }
} 




$totalNodes=5;

$theList = new linkedList();

for($i=1; $i <= $totalNodes; $i++)
{
    $theList->insertLast($i);
}


print_r($theList);

linkedList Object ( [firstNode] => listNode Object ( [data] => 1 [next] => )

[lastNode] => listNode Object
    (
        [data] => 5
        [next] =>
    )

[link] => listNode Object
    (
        [data] => 1
        [next] => listNode Object
            (
                [data] => 2
                [next] => listNode Object
                    (
                        [data] => 3
                        [next] => listNode Object
                            (
                                [data] => 4
                                [next] => listNode Object
                                    (
                                        [data] => 5
                                        [next] =>
                                    )

                            )

                    )

            )

    )

)

  • 写回答

1条回答 默认 最新

  • dongshanya2008 2011-09-09 00:50
    关注

    Assuming your code works correctly and builds the correct data structure for the linked list, making it circular is just a matter of making the last node point to the first node, e.g.:

    $this->lastNode->next = $this->firstNode;
    

    You also need to make sure this link is maintained when you then add more nodes with insertFirst or insertLast, i.e. always set lastNode->next = firstNode when a new first/last node is inserted.

    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)