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

Commit9f3561d

Browse files
Yavorskitrekhleb
Yavorski
authored andcommitted
Fix Stack pop comlexity to be O(1) (trekhleb#214)
* By definition Stack push/pop time complexity should be O(1).* Fix is applied by removing head instead of tail in pop method.* Push method now do preprend instead of append.* Fix consistency between toString and toArray methods.
1 parent45fb2a2 commit9f3561d

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ export default class Stack {
44
constructor(){
55
// We're going to implement Stack based on LinkedList since these
66
// structures are quite similar. Compare push/pop operations of the Stack
7-
// withappend/deleteTail operations of LinkedList.
7+
// withprepend/deleteHead operations of LinkedList.
88
this.linkedList=newLinkedList();
99
}
1010

1111
/**
1212
*@return {boolean}
1313
*/
1414
isEmpty(){
15-
// The stack is empty if its linked list doesn't have atail.
16-
return!this.linkedList.tail;
15+
// The stack is empty if its linked list doesn't have ahead.
16+
return!this.linkedList.head;
1717
}
1818

1919
/**
@@ -25,27 +25,27 @@ export default class Stack {
2525
returnnull;
2626
}
2727

28-
// Just read the value from theend of linked list without deleting it.
29-
returnthis.linkedList.tail.value;
28+
// Just read the value from thestart of linked list without deleting it.
29+
returnthis.linkedList.head.value;
3030
}
3131

3232
/**
3333
*@param {*} value
3434
*/
3535
push(value){
3636
// Pushing means to lay the value on top of the stack. Therefore let's just add
37-
// the new value at theend of the linked list.
38-
this.linkedList.append(value);
37+
// the new value at thestart of the linked list.
38+
this.linkedList.prepend(value);
3939
}
4040

4141
/**
4242
*@return {*}
4343
*/
4444
pop(){
45-
// Let's try to delete thelast node (thetail) from the linked list.
46-
// If there is notail (the linked list is empty) just return null.
47-
constremovedTail=this.linkedList.deleteTail();
48-
returnremovedTail ?removedTail.value :null;
45+
// Let's try to delete thefirst node (thehead) from the linked list.
46+
// If there is nohead (the linked list is empty) just return null.
47+
constremovedHead=this.linkedList.deleteHead();
48+
returnremovedHead ?removedHead.value :null;
4949
}
5050

5151
/**
@@ -54,8 +54,7 @@ export default class Stack {
5454
toArray(){
5555
returnthis.linkedList
5656
.toArray()
57-
.map(linkedListNode=>linkedListNode.value)
58-
.reverse();
57+
.map(linkedListNode=>linkedListNode.value);
5958
}
6059

6160
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Stack', () => {
1313
stack.push(1);
1414
stack.push(2);
1515

16-
expect(stack.toString()).toBe('1,2');
16+
expect(stack.toString()).toBe('2,1');
1717
});
1818

1919
it('should peek data from stack',()=>{
@@ -58,7 +58,7 @@ describe('Stack', () => {
5858

5959
conststringifier=value=>`${value.key}:${value.value}`;
6060

61-
expect(stack.toString(stringifier)).toBe('key1:test1,key2:test2');
61+
expect(stack.toString(stringifier)).toBe('key2:test2,key1:test1');
6262
expect(stack.pop().value).toBe('test2');
6363
expect(stack.pop().value).toBe('test1');
6464
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp