【问题标题】:Insert node in the linklist at a specific index using PHP OOP使用 PHP OOP 在链接列表中的特定索引处插入节点
【发布时间】:2012-10-16 13:32:34
【问题描述】:

我想使用 PHP OOP 在链接列表中的特定索引处插入节点... 我在开头插入节点和在结尾插入节点的代码如下

//top class for creating node
class ListNode
{
    public $data;
    public $next;
    function __construct($data)
    {
        $this->data = $data;
        $this->next = NULL;
    }

    function readNode()
    {
        return $this->data;
    }
}

//main class which will insert node

class LinkList
{
    private $firstNode;
    private $lastNode;
    private $count;
    function __construct()
    {
        $this->firstNode = NULL;
        $this->lastNode = NULL;
        $this->count = 0;
    }
    //insertion in start of linklist

    public function insertFirst($data)
    {
        $link = new ListNode($data);
        $link->next = $this->firstNode;
        $this->firstNode = &$link;
        /* If this is the first node inserted in the list
           then set the lastNode pointer to it.
        */
        if($this->lastNode == NULL)
            $this->lastNode = &$link;

        $this->count++;
    }
    //insertion at the last of linklist
    public function insertLast($data)
    {
        if($this->firstNode != NULL)
        {
            $link = new ListNode($data);
            $this->lastNode->next = $link;
            $link->next = NULL;
            $this->lastNode = &$link;
            $this->count++;
        }
        else
        {
            $this->insertFirst($data);
        }
    }
  }

【问题讨论】:

  • 链表不是为了在第一个和最后一个之间插入节点而设计的。您是否考虑过其他数据结构?
  • 您可以回退到第一个节点并计算节点数,这就是我最近为计算移动平均线所做的,但使用的是 spl 双链表 (php.net/manual/de/class.spldoublylinkedlist.php)。如果您有太多节点要倒带和计数,那么您应该考虑使用“SkipList”。
  • 为什么有人会在 PHP 中使用自定义链表实现?只需使用数组,或者如果您真的需要效率(不太可能),请使用 C 实现的扩展,例如 SPLDoublyLinkedList

标签: php linked-list


【解决方案1】:

为了保持 OOP,您可以做的是在 ListNode 类上创建一些方法,例如 insertBeforeMe 和 insertAfterMe。正如@Flame 指出的那样,它不是 LinkList 业务

【讨论】:

    【解决方案2】:

    将此添加到 ListNode

    function addAtIndex($index, $data) {
          if ($index != 0) {
                //pass through links till you hit the index
                $next->addAtIndex($index-1, $data)
             } else {
                $node = new ListNode($data);
                $node->next = $this->next;
                $this->next = &node;
             }
    }
    

    这样调用:

    $firstNode->addAtIndex($index, $data)

    未测试...只是一个想法

    【讨论】:

      【解决方案3】:

      通过以下代码修复它

      public function insert($NewItem,$key){
              if($key == 0){
              $link = new ListNode($NewItem);
              $link->next = $this->firstNode;
              $this->firstNode = &$link;
      
              /* If this is the first node inserted in the list
                 then set the lastNode pointer to it.
              */
              if($this->lastNode == NULL)
                  $this->lastNode = &$link;
      
              $this->count++;
          }
          else{
              $link = new ListNode($NewItem);
              $current = $this->firstNode;
              $previous = $this->firstNode;
      
              for($i=0;$i<$key;$i++)
              {       
                      $previous = $current;
                      $current = $current->next;
              }
      
                 $previous->next = $link;
                 $link->next = $current; 
                 $this->count++;
          }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-18
        • 1970-01-01
        • 1970-01-01
        • 2021-08-02
        • 1970-01-01
        相关资源
        最近更新 更多