mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
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:
committed by
Oleksii Trekhleb
parent
45fb2a24be
commit
9f3561d291
@@ -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');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user