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);
}
}
}