2525
2626namespace doganoo \PHPAlgorithms \Datastructure \Stackqueue ;
2727
28+ use doganoo \PHPAlgorithms \Common \Util \Comparator ;
29+
2830
2931/**
3032 * PHP implementation of a stack.
5153 * @package StackQueue
5254 */
5355class Stack {
56+ public const ASCENDING = 1 ;
57+ public const DESCENDING = 2 ;
5458 /**
5559 * The stack is represented as an array. Note that a stack can also be implemented using a linked list.
5660 *
@@ -59,45 +63,32 @@ class Stack {
5963 private $ stack = [];
6064
6165 /**
62- * push() adds an item to the stack.
66+ * sorts the stack in descending order
6367 *
64- * @param $item
65- * @return bool
68+ * TODO add ascending order
6669 */
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 ());
7082 }
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 ;
7783 }
7884
7985 /**
80- * checks if the stack element ( the array) is null
86+ * returns a boolean that determines if the stack is empty or not
8187 *
8288 * @return bool
8389 */
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 ;
10192 }
10293
10394 /**
@@ -131,11 +122,44 @@ public function pop() {
131122 }
132123
133124 /**
134- * returns a boolean that determines if the stack is empty or not
125+ * peek() returns the element 'on top' of the stack
135126 *
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
136143 * @return bool
137144 */
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 ;
140164 }
141165}
0 commit comments