Repository navigation
Expand file tree
/
Copy pathArrayStack.php
More file actions
86 lines (72 loc) · 1.55 KB
/
Copy pathArrayStack.php
File metadata and controls
86 lines (72 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Datastruct;
require_once("./autoload.php");
use Datastruct\TestArray;
use Datastruct\StackInterface;
/*require_once('./TestArray.php');
require_once('./StackInterface.php');*/
class ArrayStack implements StackInterface
{
//$data: type TestArray
protected $data;
public function __construct(int $capacity = 1)
{
//采用动态数组 TestArray 作为栈的存储类型
$this->data = new TestArray($capacity);
}
/**
*入栈
*/
public function push($ele):void
{
$this->data->push($ele);
}
/**
*出栈
*/
public function pop()
{
return $this->data->removeLast();
}
/**
* 获取栈顶元素
*/
public function peek()
{
return $this->data->getLast();
}
/**
* 获取栈是否为空
*/
public function isEmpty():bool
{
return $this->data->isEmpty();
}
/**
* 获取栈容量
*/
public function getLength():int
{
return $this->data->getLength();
}
public function getSize():int
{
return $this->data->getSize();
}
/**
* 打印数组时的格式
* @return string
*/
public function __toString():string
{
$str = sprintf("Array Length: %d, Size: %d\n(\n", $this->data->getLength(), $this->data->getSize());
for ($i = 0; $i < $this->data->getSize(); $i++) {
$str .= ' [' . $i . '] => ' . $this->data->get($i);
if ($i < ($this->data->getSize() - 1)) {
$str .= "\n";
}
}
$str .= " top \n)";
return $str;
}
}