Skip to content

Commit 4b69e88

Browse files
committed
Updated documentation
1 parent 6abdf61 commit 4b69e88

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Data structures covered so far -
4646
- [Graph](#graph)
4747
- [Queue](#queue)
4848
- [Linked List](#link-list)
49+
- [Stack](#stack)
4950

5051
# Contribution
5152
Your contribution is highly appreciated. You can contribute in several ways -
@@ -242,3 +243,37 @@ Usage as spread operator
242243
```js
243244
const items = [...list] // ['firstVal', 'Mid', 'xyz', 'secondVal']
244245
```
246+
247+
# <a name="stack"></a> Stack (Linked List Implementation)
248+
249+
Import Stack data structure and create a list object.
250+
251+
```js
252+
import { Stack } from '@js-labs/data-structures/lib/ds';
253+
// const { Stack } = require('@js-labs/data-structures/lib/ds')
254+
const stack = new Stack;
255+
```
256+
257+
Get size of the stack and check if the stack is empty
258+
259+
```js
260+
stack.size() //0
261+
stack.isEmpty() //true
262+
```
263+
264+
Push items in the stack
265+
266+
```js
267+
stack.push('item1');
268+
stack.size() //1
269+
270+
stack.push('item2');
271+
stack.size() //2
272+
```
273+
274+
Pop items from the stack
275+
276+
```js
277+
stack.pop() //item2
278+
stack.pop() //item1
279+
```

src/stack/stack.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,51 @@ export class Stack {
99
this[_stack] = new LinkedList;
1010
}
1111

12+
/**
13+
* getter for _stack object
14+
*/
1215
get stack() {
1316
return this[_stack];
1417
}
1518

19+
/**
20+
* Push an item on top of the stack.
21+
* Ideally the insertFirst method of linked list is called to add the item in the first position in linked list
22+
* @param {any} item
23+
*/
1624
push(item) {
1725
this[_stack].insertFirst(item);
1826
}
1927

28+
/**
29+
* Pop the top item from the stack
30+
* Ideally removeFirst method of stack is called
31+
*/
2032
pop() {
2133
if (!this.isEmpty()) {
2234
return this[_stack].removeFirst().key;
2335
}
24-
2536
}
2637

38+
/**
39+
* Returns the top item of the stack without popping it
40+
*/
2741
peek() {
2842
if (!this.isEmpty()) {
2943
return this[_stack].head.key;
3044
}
3145
}
3246

47+
/**
48+
* Returns the size of the stack
49+
*/
3350
size() {
3451
return this[_stack].size();
3552
}
3653

54+
/**
55+
* Checks if the stack is empty
56+
*/
3757
isEmpty() {
3858
return this.size() === 0;
3959
}

0 commit comments

Comments
 (0)