25
25
26
26
namespace doganoo \PHPAlgorithms \Datastructure \Stackqueue ;
27
27
28
+ use doganoo \PHPAlgorithms \Common \Util \Comparator ;
29
+
28
30
29
31
/**
30
32
* PHP implementation of a stack.
51
53
* @package StackQueue
52
54
*/
53
55
class Stack {
56
+ public const ASCENDING = 1 ;
57
+ public const DESCENDING = 2 ;
54
58
/**
55
59
* The stack is represented as an array. Note that a stack can also be implemented using a linked list.
56
60
*
@@ -59,45 +63,32 @@ class Stack {
59
63
private $ stack = [];
60
64
61
65
/**
62
- * push() adds an item to the stack.
66
+ * sorts the stack in descending order
63
67
*
64
- * @param $item
65
- * @return bool
68
+ * TODO add ascending order
66
69
*/
67
- public function push ($ item ): bool {
68
- if (!$ this ->isValid ()) {
69
- return false ;
70
+ public function sort (): void {
71
+ $ r = new Stack ();
72
+ while (!$ r ->isEmpty ()) {
73
+ $ tmp = $ this ->pop ();
74
+
75
+ while ((!$ r ->isEmpty ()) && (Comparator::lessThan ($ r ->peek (), $ tmp ))) {
76
+ $ this ->push ($ r ->pop ());
77
+ }
78
+ $ r ->push ($ tmp );
79
+ }
80
+ while (!$ r ->isEmpty ()) {
81
+ $ this ->push ($ r ->pop ());
70
82
}
71
- /*
72
- * using array_push is the better option since it
73
- * takes the work of adding to the end.
74
- */
75
- array_push ($ this ->stack , $ item );
76
- return true ;
77
83
}
78
84
79
85
/**
80
- * checks if the stack element ( the array) is null
86
+ * returns a boolean that determines if the stack is empty or not
81
87
*
82
88
* @return bool
83
89
*/
84
- protected function isValid (): bool {
85
- return $ this ->stack !== null ;
86
- }
87
-
88
- /**
89
- * peek() returns the element 'on top' of the stack
90
- *
91
- */
92
- public function peek () {
93
- if (null === $ this ->stack ) {
94
- return null ;
95
- }
96
- if (0 === $ this ->stackSize ()) {
97
- return null ;
98
- }
99
- $ value = $ this ->stack [$ this ->stackSize () - 1 ];
100
- return $ value ;
90
+ public function isEmpty (): bool {
91
+ return $ this ->stackSize () === 0 ;
101
92
}
102
93
103
94
/**
@@ -131,11 +122,44 @@ public function pop() {
131
122
}
132
123
133
124
/**
134
- * returns a boolean that determines if the stack is empty or not
125
+ * peek() returns the element 'on top' of the stack
135
126
*
127
+ */
128
+ public function peek () {
129
+ if (null === $ this ->stack ) {
130
+ return null ;
131
+ }
132
+ if (0 === $ this ->stackSize ()) {
133
+ return null ;
134
+ }
135
+ $ value = $ this ->stack [$ this ->stackSize () - 1 ];
136
+ return $ value ;
137
+ }
138
+
139
+ /**
140
+ * push() adds an item to the stack.
141
+ *
142
+ * @param $item
136
143
* @return bool
137
144
*/
138
- public function isEmpty (): bool {
139
- return $ this ->stackSize () === 0 ;
145
+ public function push ($ item ): bool {
146
+ if (!$ this ->isValid ()) {
147
+ return false ;
148
+ }
149
+ /*
150
+ * using array_push is the better option since it
151
+ * takes the work of adding to the end.
152
+ */
153
+ array_push ($ this ->stack , $ item );
154
+ return true ;
155
+ }
156
+
157
+ /**
158
+ * checks if the stack element (the array) is null
159
+ *
160
+ * @return bool
161
+ */
162
+ protected function isValid (): bool {
163
+ return $ this ->stack !== null ;
140
164
}
141
165
}
0 commit comments