66
77A wrapper around javascript array push/pop with a standard stack interface.
88
9- # Table of Contents
9+ <img src =" https://user-images.githubusercontent.com/6517308/121813242-859a9700-cc6b-11eb-99c0-49e5bb63005b.jpg " >
10+
11+
12+ # Contents
1013* [ Install] ( #install )
1114* [ require] ( #require )
1215* [ import] ( #import )
1316* [ API] ( #api )
14- * [ Construction ] ( #construction )
17+ * [ constructor ] ( #constructor )
1518 * [ .push(element)] ( #pushelement )
1619 * [ .peek()] ( #peek )
1720 * [ .pop()] ( #pop )
@@ -29,21 +32,23 @@ A wrapper around javascript array push/pop with a standard stack interface.
2932npm install --save @datastructures-js/stack
3033```
3134
32- ### require
35+ ### JS
3336``` js
3437const { Stack } = require (' @datastructures-js/stack' );
3538```
3639
37- ### import
40+ ### TS
3841``` js
3942import { Stack } from ' @datastructures-js/stack' ;
4043```
44+
4145## API
4246
43- ### Construction
47+ ### constructor
4448
4549#### using "new"
4650
51+ ##### JS
4752``` js
4853// empty stack
4954const stack = new Stack ();
@@ -52,8 +57,18 @@ const stack = new Stack();
5257const stack = new Stack ([10 , 3 , 8 , 40 , 1 ]);
5358```
5459
60+ ##### TS
61+ ``` TS
62+ // empty stack
63+ const stack = new Stack <number >();
64+
65+ // from an array
66+ const stack = new Stack <number >([10 , 3 , 8 , 40 , 1 ]);
67+ ```
68+
5569#### using ".fromArray"
5670
71+ ##### JS
5772``` js
5873// empty stack
5974const stack = Stack .fromArray ([]);
@@ -66,7 +81,12 @@ const stack = Stack.fromArray(list);
6681const stack = Stack .fromArray (list .slice ());
6782```
6883
69- ### .push(element)
84+ ##### TS
85+ ``` ts
86+ const stack = Stack .fromArray <number >([10 , 3 , 8 , 40 , 1 ]);
87+ ```
88+
89+ ### .push(element: T)
7090push an element to the top of the stack.
7191
7292<table >
@@ -76,14 +96,14 @@ push an element to the top of the stack.
7696 <th align="center">runtime</th>
7797 </tr >
7898 <tr >
79- <td align="center">element: any </td>
80- <td align="center">Stack</td>
99+ <td align="center">element: T </td>
100+ <td align="center">Stack<T> </td>
81101 <td align="center">O(1)</td>
82102 </tr >
83103</table >
84104
85105``` js
86- stack .push (' test ' );
106+ stack .push (11 );
87107```
88108
89109### .peek()
@@ -95,13 +115,13 @@ returns the top element in the stack.
95115 <th align="center">runtime</th>
96116 </tr >
97117 <tr >
98- <td align="center">any </td>
118+ <td align="center">T </td>
99119 <td align="center">O(1)</td>
100120 </tr >
101121</table >
102122
103123``` js
104- console .log (stack .peek ()); // test
124+ console .log (stack .peek ()); // 11
105125```
106126
107127### .pop()
@@ -113,13 +133,13 @@ removes and returns the top element of the stack.
113133 <th align="center">runtime</th>
114134 </tr >
115135 <tr >
116- <td align="center">any </td>
136+ <td align="center">T </td>
117137 <td align="center">O(1)</td>
118138 </tr >
119139</table >
120140
121141``` js
122- console .log (stack .pop ()); // test
142+ console .log (stack .pop ()); // 11
123143console .log (stack .peek ()); // null
124144```
125145
@@ -138,7 +158,7 @@ checks if the stack is empty.
138158</table >
139159
140160``` js
141- stack .push (' test ' );
161+ stack .push (11 );
142162console .log (stack .isEmpty ()); // false
143163```
144164
0 commit comments