📄 07c10-1.php
字号:
<?php// Define a class to implement linked lists, a linked list node.class Linked_List_Node { // Define the variable to hold our data: public $data; // And a variable to hold the next object in the chain: public $next; // A constructor method that allows for data to be passed in public function __construct($d = NULL) { $this->data = $d; } // Now create some methods that will make handling lists more automatic: // First of all a method that will insert a Node after this one public function insertAfter(Linked_List_Node $insert) { // This assumes a single node will be passed in not an entire list. // If you pass a node from a list the rest of the list will be lost. // Set the new node's next, to be this node's next. $insert->next = $this->next; // Now set this node's next, to the new node: $this->next = $insert; } // Now a method that will remove the node after this one: public function removeAfter() { // Store a reference to the node in question: $ref = $this->next; // Now jump over this node, linking to its child (if it existed) if (isset($this->next->next)) { $this->next = $this->next->next; } else { // Otherwise just make it null $this->next = NULL; } // Now, destroy the original object: unset($ref); } // Now also define a method that will output an entire linked // list, starting from this node. public function asString() { // If another child exists, then retrieve its value: $children = ''; if ($this->next) { $children = ', ' . $this->next->asString(); } // Return this data, concatenated with that of all it's children: return $this->data . $children; }}// Create a new linked list:$list = new Linked_List_Node('PHP');// Now add an element after that one:$list->insertAfter(new Linked_List_Node('Perl'));// And then insert one in between the two:$list->insertAfter(new Linked_List_Node('Javascript'));// Echo this out as a string: PHP, Javascript, Perlecho '<p>', $list->asString(), '</p>';// Now remove the last element:$list->next->removeAfter();// And output the string again: PHP, Javascriptecho '<p>', $list->asString(), '</p>';?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -