Make it possible to add objects to LinkedList.

This commit is contained in:
Oleksii Trekhleb
2018-04-09 12:15:28 +03:00
parent cdf72208b3
commit 8c46dbfb6d
10 changed files with 206 additions and 191 deletions

View File

@@ -18,11 +18,15 @@ export default class Stack {
}
push(value) {
this.linkedList.append({ value });
this.linkedList.append(value);
}
pop() {
const removedTail = this.linkedList.deleteTail();
return removedTail ? removedTail.value : null;
}
toString(callback) {
return this.linkedList.toString(callback);
}
}

View File

@@ -13,7 +13,7 @@ describe('Stack', () => {
stack.push(1);
stack.push(2);
expect(stack.linkedList.toString()).toBe('1,2');
expect(stack.toString()).toBe('1,2');
});
it('should peek data from stack', () => {
@@ -49,4 +49,17 @@ describe('Stack', () => {
expect(stack.pop()).toBeNull();
expect(stack.isEmpty()).toBeTruthy();
});
it('should be possible to push/pop objects', () => {
const stack = new Stack();
stack.push({ value: 'test1', key: 'key1' });
stack.push({ value: 'test2', key: 'key2' });
const stringifier = value => `${value.key}:${value.value}`;
expect(stack.toString(stringifier)).toBe('key1:test1,key2:test2');
expect(stack.pop().value).toBe('test2');
expect(stack.pop().value).toBe('test1');
});
});