【问题标题】:PHP link list, outputing first and lastPHP链表,输出第一个和最后一个
【发布时间】:2015-10-16 22:55:57
【问题描述】:

我正在尝试构建一个链接列表。一切正常,除了我的函数createLinkList。有一个 if 条件来指定它是否是第一个条目。我认为问题在于我在else 中的逻辑。谁能阻止正在发生的事情?

我希望我的示例的输出类似于

object(createLinkList)#1 (1) {
  ["head"]=>
  object(node)#2 (2) {
    ["data":"node":private]=>
    string(4) "adam"
    ["link":"node":private]=>
    object(node)#3 (2) {
      ["data":"node":private]=>
      string(4) "andy"
      ["link":"node":private]=>
      object(node)#4 (2) {
          ["data":"node":private]=>
          string(4) "ben"
          ["link":"node":private]=>
      }
          //and so on...
    }
  }
}

相反,我得到了;

object(createLinkList)#1 (1) {
  ["head"]=>
  object(node)#2 (2) {
    ["data":"node":private]=>
    string(4) "adam"
    ["link":"node":private]=>
    object(node)#3 (2) {
      ["data":"node":private]=>
      string(4) "eric"
      ["link":"node":private]=>
      *RECURSION*
    }
  }
}

这是我的代码,它应该可以正常运行。真的很感谢有人解释我做错了什么。谢谢

$oLinkList = new createLinkList;
$oLinkList->createLinkList($aList);

echo "<pre>";
var_dump($oLinkList);


class createLinkList{

    // // link to the first node
    public $head;
    // link to the last node
    // public $tail;
    // public $next;


    //mutator method
    public function __set($property, $value) {
        $this->$property = $value;
    }

    //accessor method
    public function __get($property) {

        if (isset($this->$property)) {

            return $this->$property;

        } else {

            return false;
        }
    }


    // init the properties
    function __construct() {
        $this->head = null;
        // $this->tail = null;
      // $this->previous = null;

    }


    function createLinkList($aList){

        if($aList == null || empty($aList)){        
            //$this = null;
            return null;
        }

        $oPrevious;

        foreach ($aList as $data) {

            // create node/object 
            $link = new Node($data);


            // first entry, have already created the node, so save a reference to in in the head var
            if($this->head == null){

                $this->head = $link;
                $oPrevious = &$this->head;

            }else{ // update the previous nodes link with a pointer to the node createds

                $oPrevious->link = $link;
                // $this->previous= $link;
                $link->link = $this->head;

               //$this->head = $link;
            }
        }
    }

}// end class



class node{

    private $data = null;
    private $link;


    //mutator method
    public function __set($property, $value) {
        $this->$property = $value;
    }

    //accessor method
    public function __get($property) {

        if (isset($this->$property)) {

            return $this->$property;

        } else {

            return false;
        }
    }



    /* Node constructor */
    function __construct($data)
    {
        $this->data = $data;
        $this->link = null;
    }


}

【问题讨论】:

  • 感谢谁投了反对票,并没有费心告诉我原因。这不是问答网站吗?
  • 你打算用$oPrevious;这条线实现什么?
  • 我不确定,可能在想我需要在使用前声明它。我会删除它,谢谢
  • @Victor 你知道我做错了什么吗?感觉离我不远了。谢谢
  • 很多。实际上,您仅在 head 已经为空时才更新它,但您仅将其设置为空。你也没有对oPrevious做任何事情。

标签: php algorithm logic


【解决方案1】:

除非删除一个元素,否则永远不要更新前一个元素。

您应该始终只存储第一个元素,并在需要时将其设置为新节点。

这应该由方法而不是 foreach 来处理。

【讨论】:

  • 非常感谢!完成后将查看并发布代码更新。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
相关资源
最近更新 更多