Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit20b0c48

Browse files
committed
Add comments to Stack code.
1 parent571d931 commit20b0c48

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

‎src/data-structures/stack/Stack.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import LinkedList from '../linked-list/LinkedList';
22

33
exportdefaultclassStack{
44
constructor(){
5+
// We're going to implement Queue based on LinkedList since this
6+
// structures a quite similar. Compare push/pop operations of the Stack
7+
// with append/deleteTail operations of LinkedList.
58
this.linkedList=newLinkedList();
69
}
710

811
/**
912
*@return {boolean}
1013
*/
1114
isEmpty(){
15+
// The queue is empty in case if its linked list don't have tail.
1216
return!this.linkedList.tail;
1317
}
1418

@@ -17,23 +21,29 @@ export default class Stack {
1721
*/
1822
peek(){
1923
if(this.isEmpty()){
24+
// If linked list is empty then there is nothing to peek from.
2025
returnnull;
2126
}
2227

28+
// Just read the value from the end of linked list without deleting it.
2329
returnthis.linkedList.tail.value;
2430
}
2531

2632
/**
2733
*@param {*} value
2834
*/
2935
push(value){
36+
// Pushing means to lay the value on top of the stack. Therefore let's just add
37+
// new value at the end of the linked list.
3038
this.linkedList.append(value);
3139
}
3240

3341
/**
3442
*@return {*}
3543
*/
3644
pop(){
45+
// Let's try to delete the last node from linked list (the tail).
46+
// If there is no tail in linked list (it is empty) just return null.
3747
constremovedTail=this.linkedList.deleteTail();
3848
returnremovedTail ?removedTail.value :null;
3949
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp