Skip to content

Commit aefc810

Browse files
committed
php 09_queue
1 parent 0b6dd0a commit aefc810

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

php/09_queue/QueueOnLinkedList.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* User: Hkesd
4+
* Date: 2018/10/13 11:26
5+
* Desc:
6+
*/
7+
8+
namespace Algo_09;
9+
10+
use Algo_06\SingleLinkedListNode;
11+
12+
/**
13+
* 队列 链表实现
14+
*
15+
* Class QueueOnLinkedList
16+
*/
17+
class QueueOnLinkedList
18+
{
19+
/**
20+
* 队列头节点
21+
*
22+
* @var SingleLinkedListNode
23+
*/
24+
public $head;
25+
26+
/**
27+
* 队列尾节点
28+
*
29+
* @var null
30+
*/
31+
public $tail;
32+
33+
/**
34+
* 队列长度
35+
*
36+
* @var int
37+
*/
38+
public $length;
39+
40+
/**
41+
* QueueOnLinkedList constructor.
42+
*/
43+
public function __construct()
44+
{
45+
$this->head = new SingleLinkedListNode();
46+
$this->tail = $this->head;
47+
48+
$this->length = 0;
49+
}
50+
51+
/**
52+
* 入队
53+
*
54+
* @param $data
55+
*/
56+
public function enqueue($data)
57+
{
58+
$newNode = new SingleLinkedListNode();
59+
$newNode->data = $data;
60+
61+
$this->tail->next = $newNode;
62+
$this->tail = $newNode;
63+
64+
$this->length++;
65+
}
66+
67+
/**
68+
* 出队
69+
*
70+
* @return SingleLinkedListNode|bool|null
71+
*/
72+
public function dequeue()
73+
{
74+
if (0 == $this->length) {
75+
return false;
76+
}
77+
78+
$node = $this->head->next;
79+
$this->head->next = $this->head->next->next;
80+
81+
$this->length--;
82+
83+
return $node;
84+
}
85+
86+
/**
87+
* 获取队列长度
88+
*
89+
* @return int
90+
*/
91+
public function getLength()
92+
{
93+
return $this->length;
94+
}
95+
96+
/**
97+
* 打印队列
98+
*/
99+
public function printSelf()
100+
{
101+
if (0 == $this->length) {
102+
echo 'empty queue' . PHP_EOL;
103+
return;
104+
}
105+
106+
echo 'head.next -> ';
107+
$curNode = $this->head;
108+
while ($curNode->next) {
109+
echo $curNode->next->data . ' -> ';
110+
111+
$curNode = $curNode->next;
112+
}
113+
echo 'NULL' . PHP_EOL;
114+
}
115+
}

php/09_queue/main.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* User: Hkesd
4+
* Date: 2018/10/13 11:46
5+
* Desc:
6+
*/
7+
8+
namespace Algo_09;
9+
10+
require_once "../vendor/autoload.php";
11+
12+
$queue = new QueueOnLinkedList();
13+
$queue->enqueue(1);
14+
$queue->enqueue(2);
15+
$queue->enqueue(3);
16+
$queue->enqueue(4);
17+
$queue->enqueue(5);
18+
$queue->printSelf();
19+
var_dump($queue->getLength());
20+
21+
$queue->dequeue();
22+
$queue->printSelf();
23+
$queue->dequeue();
24+
$queue->dequeue();
25+
$queue->dequeue();
26+
$queue->printSelf();
27+
28+
$queue->dequeue();
29+
$queue->printSelf();

php/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
* findMiddleNode 求链表的中间结点
1818

1919
#### 08_stack
20-
* 链栈实现
20+
* 链栈实现
21+
22+
#### 09_stack
23+
* 队列链表实现

php/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"psr-4": {
88
"Algo_06\\": "06_linkedlist/",
99
"Algo_07\\": "07_linkedlist/",
10-
"Algo_08\\": "08_stack/"
10+
"Algo_08\\": "08_stack/",
11+
"Algo_09\\": "09_queue/"
1112
}
1213
}
1314
}

0 commit comments

Comments
 (0)