feat: Add PHP Codes for chapter_array_and_linkedlist

This commit is contained in:
DoWake 2024-05-19 22:54:54 +08:00
parent 422694cf2c
commit 70df1515c1
4 changed files with 412 additions and 0 deletions

View File

@ -0,0 +1,108 @@
<?php
/**
* File: array.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
/* 随机访问元素 */
function randomAccess($nums)
{
// 在区间 [0, nums.length) 中随机抽取一个数字
$length = count($nums);
$randomIndex = mt_rand(0, $length - 1);
// 获取并返回随机元素
$randomNum = $nums[$randomIndex];
return $randomNum;
}
/* 扩展数组长度 */
// 请注意PHP 的 Array 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作长度不可变的数组
function extend($nums, $enlarge)
{
// 初始化一个扩展长度后的数组
$res = array_fill(0, count($nums) + $enlarge, 0);
// 将原数组中的所有元素复制到新数组
for ($i = 0; $i < count($nums); $i++) {
$res[$i] = $nums[$i];
}
// 返回扩展后的新数组
return $res;
}
/* 在数组的索引 index 处插入元素 num */
function insert(&$nums, $num, $index)
{
// 把索引 index 以及之后的所有元素向后移动一位
for ($i = count($nums) - 1; $i > $index; $i--) {
$nums[$i] = $nums[$i - 1];
}
// 将 num 赋给 index 处的元素
$nums[$index] = $num;
}
/* 删除索引 index 处的元素 */
function remove(&$nums, $index)
{
// 把索引 index 之后的所有元素向前移动一位
for ($i = $index; $i < count($nums) - 1; $i++) {
$nums[$i] = $nums[$i + 1];
}
}
/* 遍历数组 */
function traverse($nums)
{
$count = 0;
// 通过索引遍历数组
for ($i = 0; $i < count($nums); $i++) {
$count += $nums[$i];
}
// 直接遍历数组元素
foreach ($nums as $num) {
$count += $num;
}
}
/* 在数组中查找指定元素 */
function find($nums, $target)
{
for ($i = 0; $i < count($nums); $i++) {
if ($nums[$i] === $target) {
return $i;
}
}
return -1;
}
/* Driver Code */
/* 初始化数组 */
$arr = array_fill(0, 5, 0);
echo '数组 arr = [' . join(', ', $arr) . ']' . PHP_EOL;
$nums = [1, 3, 2, 5, 4];
echo '数组 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 随机访问 */
$randomNum = randomAccess($nums);
echo "在 nums 中获取随机元素 {$randomNum}" . PHP_EOL;
/* 长度扩展 */
$nums = extend($nums, 3);
echo '将数组长度扩展至 8 ,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 插入元素 */
insert($nums, 6, 3);
echo '在索引 3 处插入数字 6 ,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 删除元素 */
remove($nums, 2);
echo '删除索引 2 处的元素,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 遍历数组 */
traverse($nums);
/* 查找元素 */
$index = find($nums, 3);
echo "在 nums 中查找元素 3 ,得到索引 = {$index}" . PHP_EOL;

View File

@ -0,0 +1,90 @@
<?php
/**
* File: linked_list.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
require_once '../utils/ListNode.php';
require_once '../utils/PrintUtil.php';
/* 在链表的节点 n0 之后插入节点 P */
function insert($n0, $P)
{
$n1 = $n0->next;
$P->next = $n1;
$n0->next = $P;
}
/* 删除链表的节点 n0 之后的首个节点 */
function remove($n0)
{
if ($n0->next === null) {
return;
}
// n0 -> P -> n1
$P = $n0->next;
$n1 = $P->next;
$n0->next = $n1;
}
/* 访问链表中索引为 index 的节点 */
function access($head, $index)
{
for ($i = 0; $i < $index; $i++) {
if ($head === null) {
return null;
}
$head = $head->next;
}
return $head;
}
/* 在链表中查找值为 target 的首个节点 */
function find($head, $target)
{
$index = 0;
while ($head !== null) {
if ($head->val === $target) {
return $index;
}
$head = $head->next;
$index++;
}
return -1;
}
/* Driver Code */
/* 初始化链表 */
// 初始化各个节点
$n0 = new ListNode(1);
$n1 = new ListNode(3);
$n2 = new ListNode(2);
$n3 = new ListNode(5);
$n4 = new ListNode(4);
// 构建节点之间的引用
$n0->next = $n1;
$n1->next = $n2;
$n2->next = $n3;
$n3->next = $n4;
echo '初始化的链表为' . PHP_EOL;
PrintUtil::printLinkedList($n0);
/* 插入节点 */
insert($n0, new ListNode(0));
echo '插入节点后的链表为' . PHP_EOL;
PrintUtil::printLinkedList($n0);
/* 删除节点 */
remove($n0);
echo '删除节点后的链表为' . PHP_EOL;
PrintUtil::printLinkedList($n0);
/* 访问节点 */
$node = access($n0, 3);
echo "链表中索引 3 处的节点的值 = {$node->val}" . PHP_EOL;
/* 查找节点 */
$index = find($n0, 2);
echo "链表中值为 2 的节点的索引 = {$index}" . PHP_EOL;

View File

@ -0,0 +1,58 @@
<?php
/**
* File: list.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
/* 初始化列表 */
$nums = [1, 3, 2, 5, 4];
echo '列表 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 访问元素 */
$num = $nums[1];
echo "访问索引 1 处的元素,得到 num = {$num}" . PHP_EOL;
/* 更新元素 */
$nums[1] = 0;
echo '将索引 1 处的元素更新为 0 ,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 清空列表 */
$nums = [];
echo '清空列表后 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 在尾部添加元素 */
array_push($nums, 1);
array_push($nums, 3);
array_push($nums, 2);
array_push($nums, 5);
array_push($nums, 4);
echo '添加元素后 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 在中间插入元素 */
array_splice($nums, 3, 0, [6]);
echo '在索引 3 处插入数字 6 ,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 删除元素 */
array_splice($nums, 3, 1);
echo '删除索引 3 处的元素,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 通过索引遍历列表 */
$count = 0;
for ($i = 0; $i < count($nums); $i++) {
$count += $nums[$i];
}
/* 直接遍历列表元素 */
foreach ($nums as $x) {
$count += $x;
}
/* 拼接两个列表 */
$nums1 = [6, 8, 7, 10, 9];
$nums = array_merge($nums, $nums1);
echo '将列表 nums1 拼接到 nums 之后,得到 nums = [' . join(', ', $nums) . ']' . PHP_EOL;
/* 排序列表 */
sort($nums);
echo '排序列表后 nums = [' . join(', ', $nums) . ']' . PHP_EOL;

View File

@ -0,0 +1,156 @@
<?php
/**
* File: my_list.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
/* 列表类 */
class MyList
{
private $arr = []; // 数组(存储列表元素)
private $capacity = 10; // 列表容量
private $size = 0; // 列表长度(当前元素数量)
private $extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
public function __construct()
{
$this->arr = array_fill(0, $this->capacity, 0);
}
/* 获取列表长度(当前元素数量) */
public function size()
{
return $this->size;
}
/* 获取列表容量 */
public function capacity()
{
return $this->capacity;
}
/* 访问元素 */
public function get($index)
{
// 索引如果越界,则抛出异常,下同
if ($index < 0 || $index >= $this->size) {
throw new Exception('索引越界');
}
return $this->arr[$index];
}
/* 更新元素 */
public function set($index, $num)
{
if ($index < 0 || $index >= $this->size) {
throw new Exception('索引越界');
}
$this->arr[$index] = $num;
}
/* 在尾部添加元素 */
public function add($num)
{
// 元素数量超出容量时,触发扩容机制
if ($this->size === $this->capacity()) {
$this->extendCapacity();
}
$this->arr[$this->size] = $num;
// 更新元素数量
$this->size++;
}
/* 在中间插入元素 */
public function insert($index, $num)
{
if ($index < 0 || $index >= $this->size) {
throw new Exception('索引越界');
}
// 元素数量超出容量时,触发扩容机制
if ($this->size === $this->capacity()) {
$this->extendCapacity();
}
// 将索引 index 以及之后的元素都向后移动一位
for ($j = $this->size - 1; $j >= $index; $j--) {
$this->arr[$j + 1] = $this->arr[$j];
}
$this->arr[$index] = $num;
// 更新元素数量
$this->size++;
}
/* 删除元素 */
public function remove($index)
{
if ($index < 0 || $index >= $this->size) {
throw new Exception('索引越界');
}
$num = $this->arr[$index];
// 将索引 index 之后的元素都向前移动一位
for ($j = $index; $j < $this->size - 1; $j++) {
$this->arr[$j] = $this->arr[$j + 1];
}
// 更新元素数量
$this->size--;
// 返回被删除的元素
return $num;
}
/* 列表扩容 */
public function extendCapacity()
{
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
$this->arr = array_pad($this->arr, $this->capacity() * $this->extendRatio, 0);
// 更新列表容量
$this->capacity = count($this->arr);
}
/* 将列表转换为数组 */
public function toArray()
{
$size = $this->size();
// 仅转换有效长度范围内的列表元素
$arr = [];
for ($i = 0; $i < $size; $i++) {
$arr[$i] = $this->get($i);
}
return $arr;
}
}
/* Driver Code */
/* 初始化列表 */
$nums = new MyList();
/* 在尾部添加元素 */
$nums->add(1);
$nums->add(3);
$nums->add(2);
$nums->add(5);
$nums->add(4);
echo "列表 nums = [" . join(', ', $nums->toArray()) . "] ,容量 = {$nums->capacity()} ,长度 = {$nums->size()}" . PHP_EOL;
/* 在中间插入元素 */
$nums->insert(3, 6);
echo '在索引 3 处插入数字 6 ,得到 nums = [' . join(', ', $nums->toArray()) . ']' . PHP_EOL;
/* 删除元素 */
$nums->remove(3);
echo '删除索引 3 处的元素,得到 nums = [' . join(', ', $nums->toArray()) . ']' . PHP_EOL;
/* 访问元素 */
$num = $nums->get(1);
echo "访问索引 1 处的元素,得到 num = {$num}" . PHP_EOL;
/* 更新元素 */
$nums->set(1, 0);
echo '将索引 1 处的元素更新为 0 ,得到 nums = [' . join(', ', $nums->toArray()) . ']' . PHP_EOL;
/* 测试扩容机制 */
for ($i = 0; $i < 10; $i++) {
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
$nums->add($i);
}
echo "扩容后的列表 nums = [" . join(', ', $nums->toArray()) . "] ,容量 = {$nums->capacity()} ,长度 = {$nums->size()}" . PHP_EOL;