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

@@ -13,7 +13,7 @@ describe('Stack', () => {
stack.push(1);
stack.push(2);
expect(stack.toString()).toBe('1,2');
expect(stack.toString()).toBe('2,1');
});
it('should peek data from stack', () => {
@@ -58,7 +58,7 @@ describe('Stack', () => {
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('test1');
});