diff --git a/solutions/data-structures/linked-list/LinkedList.js b/solutions/data-structures/linked-list/LinkedList.js index 855feee..eabd65e 100644 --- a/solutions/data-structures/linked-list/LinkedList.js +++ b/solutions/data-structures/linked-list/LinkedList.js @@ -128,6 +128,9 @@ export default class LinkedList { const newNode = new LinkedListNode(value, targetNode.next); targetNode.next = newNode; + if (this.tail == targetNode){ + this.tail = newNode; + } return newNode; } diff --git a/solutions/data-structures/queue/Queue.js b/solutions/data-structures/queue/Queue.js index b5656a0..c4e8d2e 100644 --- a/solutions/data-structures/queue/Queue.js +++ b/solutions/data-structures/queue/Queue.js @@ -12,7 +12,7 @@ export default class Queue { return null; } - return this.store[0]; + return this.store[this.store.length -1]; } enqueue(el) { diff --git a/solutions/data-structures/stack/Stack.js b/solutions/data-structures/stack/Stack.js index 44873b2..a4b00d3 100644 --- a/solutions/data-structures/stack/Stack.js +++ b/solutions/data-structures/stack/Stack.js @@ -9,7 +9,7 @@ export default class Stack { peek() { if (this.isEmpty()) { - return; + return null; } return this.store[this.store.length - 1]; @@ -20,6 +20,7 @@ export default class Stack { } pop() { + if (this.store.length == 0) return null; return this.store.pop(); } diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index e7a7d01..94730bf 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -139,6 +139,13 @@ describe('LinkedList', () => { .insertAfter(2, 1); expect(linkedList.toString()).toBe('1,2,3'); + const linkedList1 = new LinkedList(); + linkedList1 + .append(1) + .append(2) + .insertAfter(3, 2); + + expect(linkedList1.tail.toString()).toBe('3'); }); it('should delete linked list tail', () => { diff --git a/src/data-structures/queue/__test__/Queue.test.js b/src/data-structures/queue/__test__/Queue.test.js index 7bd3ba8..a1de34d 100644 --- a/src/data-structures/queue/__test__/Queue.test.js +++ b/src/data-structures/queue/__test__/Queue.test.js @@ -13,7 +13,7 @@ describe('Queue', () => { queue.enqueue(1); queue.enqueue(2); - expect(queue.toString()).toBe('1,2'); + expect(queue.toString()).toBe('2,1'); }); it('should be possible to enqueue/dequeue in order', () => { diff --git a/src/data-structures/stack/__test__/Stack.test.js b/src/data-structures/stack/__test__/Stack.test.js index ff0cbb4..112c55a 100644 --- a/src/data-structures/stack/__test__/Stack.test.js +++ b/src/data-structures/stack/__test__/Stack.test.js @@ -19,7 +19,7 @@ describe('Stack', () => { it('should peek data from stack', () => { const stack = new Stack(); - expect(stack.peek()).toBeUndefined(); + expect(stack.peek()).not.toBeUndefined(); stack.push(1); stack.push(2); @@ -46,7 +46,7 @@ describe('Stack', () => { expect(stack.pop()).toBe(2); expect(stack.pop()).toBe(1); - expect(stack.pop()).toBeUndefined(); + expect(stack.pop()).not.toBeUndefined(); expect(stack.isEmpty()).toBeTruthy(); });