簡體   English   中英

如何使用PHP在指定索引處添加/刪除特定節點?

[英]How to add/delete a specific node at specified index using PHP?

我正在嘗試使用PHP實現一個鏈表。 我已經完成了其中的一部分,但是,我不確定我的代碼是否正確。 您能否建議如何在指定索引處添加/刪除特定節點?

請參考以下代碼:(共3個文件)

1)[ListNode.class.php]

我定義的節點結構:

<?php


class ListNode
{
    protected $next;        // Next node in the list
    protected $value;       // Value of the node

    // Constructor
    // Input: Value of the node
    public function __construct($value)
    {
        $this->value = $value;
        $this->next = NULL;
    }

    public function __get($name)
    {
        return $this->$name;
    }

    public function __set($name, $value)
    {
        $this->$name = $value;
    }

    // Return the node as string
    public function toString()
    {
        return $this->value . "\n";
    }
}

2)[LinkList.class.php]

鏈表和我還沒有完成的操作:

<?php


require('ListNode.class.php');

class LinkedList
{
    protected $first;           // First node of the list
    protected $last;            // Last node of the list
    protected $count;             // Total numbers of nodes in the list

    // Constructor
    // Input: Array of values (Optional)
    public function __construct($values = array())
    {
        $this->first = null;
        $this->last = null;
        $this->count = 0;

        foreach ($values as $value) {
            $this->add($value);
        }
    }
    public function isEmpty()

        if ($this->sizeOf() !== 0) 
        return ($this->first == NULL);
    }

    // Add a node at the beginning of the list
    public function add($value)
    {
        $link = new ListNode($value);
        $link->next = $this->first;
        $this->first = &$link;

        if($this->last == NULL)
           $this->last = &$link;

        $this->count++;
    }

    // Add a node at the specified index
    public function addAtIndex($value, $index)
    {

    }

    // Remove a node at the end of the list
    public function remove()
    {
        if($this->first != NULL)
        {
          if($this->first->next == NULL)
          {
            $this->first == NULL;         
            $this->cout--;
        }
        else
        {
             $previous = $this->first;
             $current = $this->first->next;

             while($current->next != NULL)
             {
              $previous = $current;
              $current = $current->next;

             $previous->next = NULL;
             $this->count--;
        }
    }

    // Remove a node at the specified index
    public function removeAtIndex($index)
    {

    }

    // Return the value of the first node
    public function getNode()
    {

    }

    // Return the value of the node at the specified index
    public function getNodeAtIndex($index)
    {
         if($index <= $this->count)
        {
            $current = $this->firstNode;
            $pos = 1;
            while($pos != $index)
            {
                if($current->next == NULL)
                    return null;
                else
                    $current = $current->next;

                $pos++;
            }
            return $current->value;
        }
        else
            return NULL;  
    }

    // Return the number of nodes
    public function sizeOf()
    {
        return $this->count;
    }

    // Return the list as string
    public function toString()
    {
        $list = "";
        $node = $this->first;

        while ($node != null) {
            $list .= $node->toString();
            $node = $node->next;
        }

        return $list;
    }
}

如果需要鏈接列表,為什么不使用標准PHP庫? 那里有SplDoublyLinkedList ,其中包含您需要的所有功能:

這些方法是已實現的ArrayAccess接口的一部分,這意味着您不必直接調用它們,但可以執行以下操作:

$list = new SplDoublyLinkedList;
$list->push('item 1');
$list->push('item 2');
$list->push('item 3');
echo $list[1];
unset($list[1]);

foreach($list as $index => $value) {
    echo "\n$index: $value";
}

輸出:

item 2
0: item 1
1: item 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM