Skip to content

Commit

Permalink
Changes to data structures to fix bugs:m issue OperationCode#6
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcostello committed Jul 4, 2019
1 parent 81754b7 commit 90582db
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions solutions/data-structures/linked-list/LinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion solutions/data-structures/queue/Queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Queue {
return null;
}

return this.store[0];
return this.store[this.store.length -1];
}

enqueue(el) {
Expand Down
3 changes: 2 additions & 1 deletion solutions/data-structures/stack/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class Stack {

peek() {
if (this.isEmpty()) {
return;
return null;
}

return this.store[this.store.length - 1];
Expand All @@ -20,6 +20,7 @@ export default class Stack {
}

pop() {
if (this.store.length == 0) return null;
return this.store.pop();
}

Expand Down
7 changes: 7 additions & 0 deletions src/data-structures/linked-list/__test__/LinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/queue/__test__/Queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/data-structures/stack/__test__/Stack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});

Expand Down

0 comments on commit 90582db

Please sign in to comment.