Skip to content

Commit 9f3561d

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 parent 45fb2a2 commit 9f3561d

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/data-structures/stack/Stack.js

+12-13
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-
// with append/deleteTail operations of LinkedList.
7+
// with prepend/deleteHead operations of LinkedList.
88
this.linkedList = new LinkedList();
99
}
1010

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

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

28-
// Just read the value from the end of linked list without deleting it.
29-
return this.linkedList.tail.value;
28+
// Just read the value from the start of linked list without deleting it.
29+
return this.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 the end of the linked list.
38-
this.linkedList.append(value);
37+
// the new value at the start of the linked list.
38+
this.linkedList.prepend(value);
3939
}
4040

4141
/**
4242
* @return {*}
4343
*/
4444
pop() {
45-
// Let's try to delete the last node (the tail) from the linked list.
46-
// If there is no tail (the linked list is empty) just return null.
47-
const removedTail = this.linkedList.deleteTail();
48-
return removedTail ? removedTail.value : null;
45+
// Let's try to delete the first node (the head) from the linked list.
46+
// If there is no head (the linked list is empty) just return null.
47+
const removedHead = this.linkedList.deleteHead();
48+
return removedHead ? removedHead.value : null;
4949
}
5050

5151
/**
@@ -54,8 +54,7 @@ export default class Stack {
5454
toArray() {
5555
return this.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

+2-2
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
const stringifier = 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)