Fix Stack pop comlexity to be O(1) (#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.
This commit is contained in:
Yavorski 2018-09-24 07:31:18 +03:00 committed by Oleksii Trekhleb
parent 45fb2a24be
commit 9f3561d291
2 changed files with 14 additions and 15 deletions

View File

@ -4,7 +4,7 @@ export default class Stack {
constructor() { constructor() {
// We're going to implement Stack based on LinkedList since these // We're going to implement Stack based on LinkedList since these
// structures are quite similar. Compare push/pop operations of the Stack // structures are quite similar. Compare push/pop operations of the Stack
// with append/deleteTail operations of LinkedList. // with prepend/deleteHead operations of LinkedList.
this.linkedList = new LinkedList(); this.linkedList = new LinkedList();
} }
@ -12,8 +12,8 @@ export default class Stack {
* @return {boolean} * @return {boolean}
*/ */
isEmpty() { isEmpty() {
// The stack is empty if its linked list doesn't have a tail. // The stack is empty if its linked list doesn't have a head.
return !this.linkedList.tail; return !this.linkedList.head;
} }
/** /**
@ -25,8 +25,8 @@ export default class Stack {
return null; return null;
} }
// Just read the value from the end of linked list without deleting it. // Just read the value from the start of linked list without deleting it.
return this.linkedList.tail.value; return this.linkedList.head.value;
} }
/** /**
@ -34,18 +34,18 @@ export default class Stack {
*/ */
push(value) { push(value) {
// Pushing means to lay the value on top of the stack. Therefore let's just add // Pushing means to lay the value on top of the stack. Therefore let's just add
// the new value at the end of the linked list. // the new value at the start of the linked list.
this.linkedList.append(value); this.linkedList.prepend(value);
} }
/** /**
* @return {*} * @return {*}
*/ */
pop() { pop() {
// Let's try to delete the last node (the tail) from the linked list. // Let's try to delete the first node (the head) from the linked list.
// If there is no tail (the linked list is empty) just return null. // If there is no head (the linked list is empty) just return null.
const removedTail = this.linkedList.deleteTail(); const removedHead = this.linkedList.deleteHead();
return removedTail ? removedTail.value : null; return removedHead ? removedHead.value : null;
} }
/** /**
@ -54,8 +54,7 @@ export default class Stack {
toArray() { toArray() {
return this.linkedList return this.linkedList
.toArray() .toArray()
.map(linkedListNode => linkedListNode.value) .map(linkedListNode => linkedListNode.value);
.reverse();
} }
/** /**

View File

@ -13,7 +13,7 @@ describe('Stack', () => {
stack.push(1); stack.push(1);
stack.push(2); stack.push(2);
expect(stack.toString()).toBe('1,2'); expect(stack.toString()).toBe('2,1');
}); });
it('should peek data from stack', () => { it('should peek data from stack', () => {
@ -58,7 +58,7 @@ describe('Stack', () => {
const stringifier = value => `${value.key}:${value.value}`; const stringifier = value => `${value.key}:${value.value}`;
expect(stack.toString(stringifier)).toBe('key1:test1,key2:test2'); expect(stack.toString(stringifier)).toBe('key2:test2,key1:test1');
expect(stack.pop().value).toBe('test2'); expect(stack.pop().value).toBe('test2');
expect(stack.pop().value).toBe('test1'); expect(stack.pop().value).toBe('test1');
}); });