11/**
2- * @datastructures -js/stack
3- * @copyright 2020 Eyas Ranjous <eyas.ranjous@gmail.com>
42 * @license MIT
5- */
6-
7- /**
8- * @class Stack
9- * implements LIFO principle.
3+ * @copyright 2020 Eyas Ranjous <eyas.ranjous@gmail.com>
4+ *
5+ * @class
106 */
117class Stack {
8+ /**
9+ * Creates a stack.
10+ * @param {array } [elements]
11+ */
1212 constructor ( elements ) {
1313 this . _elements = Array . isArray ( elements ) ? elements : [ ] ;
1414 }
1515
1616 /**
17+ * Checks if the stack is empty.
1718 * @public
18- * checks if the stack is empty
1919 * @returns {boolean }
2020 */
2121 isEmpty ( ) {
2222 return this . _elements . length === 0 ;
2323 }
2424
2525 /**
26+ * Returns the number of elements in the stack.
2627 * @public
27- * returns the number of elements in the stack
2828 * @returns {number }
2929 */
3030 size ( ) {
3131 return this . _elements . length ;
3232 }
3333
3434 /**
35+ * Returns the top element in the stack.
3536 * @public
36- * returns the top element in the stack
3737 * @returns {object }
3838 */
3939 peek ( ) {
@@ -43,17 +43,17 @@ class Stack {
4343 }
4444
4545 /**
46+ * Adds an element to the top of the stack.
4647 * @public
47- * adds an element to the top of the stack
4848 * @param {object } element
4949 */
5050 push ( element ) {
5151 this . _elements . push ( element ) ;
5252 }
5353
5454 /**
55+ * Removes and returns the top element in the stack.
5556 * @public
56- * removes and returns the top element in the stack
5757 * @returns {object }
5858 */
5959 pop ( ) {
@@ -63,36 +63,36 @@ class Stack {
6363 }
6464
6565 /**
66+ * Returns the remaining elements as an array.
6667 * @public
67- * returns the remaining elements as an array
6868 * @returns {array }
6969 */
7070 toArray ( ) {
71- return this . _elements . slice ( 0 ) ;
71+ return this . _elements . slice ( ) ;
7272 }
7373
7474 /**
75+ * Clears all elements from the stack.
7576 * @public
76- * clears all elements from the stack
7777 */
7878 clear ( ) {
7979 this . _elements = [ ] ;
8080 }
8181
8282 /**
83+ * Creates a shallow copy from the stack.
8384 * @public
84- * creates a shallow copy from the stack
8585 * @return {Stack }
8686 */
8787 clone ( ) {
88- return new Stack ( this . _elements . slice ( 0 ) ) ;
88+ return new Stack ( this . _elements . slice ( ) ) ;
8989 }
9090
9191 /**
92+ * Creates a stack from an existing array
9293 * @public
9394 * @static
94- * creates a stack from an existing array
95- * @param {array } elements
95+ * @param {array } [elements]
9696 * @return {Stack }
9797 */
9898 static fromArray ( elements ) {
0 commit comments