-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingelStack.php
105 lines (92 loc) · 1.86 KB
/
SingelStack.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Created by PhpStorm.
* User: yangyu
* Date: 16/9/21
* Time: 上午10:32
*/
class Node
{
public $data;
public $next;
public function __construct($data = null)
{
$this->data = $data;
$this->next = null;
}
}
class SingelStack
{
private $top;
private $length;
public function __construct()
{
$this->top = null;
$this->length = 0;
}
public function destroyStack()
{
while ($this->top) {
$next = $this->top->next;
unset($this->top);
$this->top = $next;
}
$this->length = 0;
}
public function clearStack()
{
while ($this->top) {
$next = $this->top->next;
unset($this->top);
$this->top = $next;
}
$this->top = null;
$this->length = 0;
}
public function emptyStack()
{
if ($this->top == null) {
return true;
} else {
return false;
}
}
public function lengthStack()
{
return $this->length;
}
public function getTop()
{
if ($this->top == null) {
return 'Null';
}
return $this->top->data;
}
public function push($node)
{
$node->next = $this->top;
$this->top = $node;
$this->length++;
}
public function pop()
{
if ($this->top != null) {
$next = $this->top->next;
unset($this->top);
$this->top = $next;
$this->length--;
}
}
public function stackTraverse()
{
$arr = [];
if ($this->top != null) {
$current = $this->top;
while ($current) {
$arr[] = $current->data;
$current = $current->next;
}
}
return $arr;
}
}