Skip to content

Commit cc29c56

Browse files
committed
fix: peek() should show the front item of queue
1 parent cbe4a1e commit cc29c56

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/_DataStructures_/Queue/Queue.test.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ describe('Data Structure : Queue', () => {
1212
queue = new Queue();
1313
});
1414

15-
it('Should add() element to a queue', () => {
16-
queue.add(5);
17-
expect(queue.data).toEqual([5]);
15+
it('Should enqueue() element to a queue', () => {
16+
queue.enqueue(5);
17+
expect(queue.peek()).toEqual([5]);
1818
});
1919

20-
it('Should remove() an element from the queue', () => {
21-
queue.add(2);
22-
queue.add(3);
20+
it('Should dequeue() an element from the queue', () => {
21+
queue.enqueue(2);
22+
queue.enqueue(3);
2323

24-
expect(queue.remove()).toEqual(2);
25-
expect(queue.data).toEqual([3]);
24+
expect(queue.dequeue()).toEqual(2);
25+
expect(queue.peek()).toEqual(3);
26+
expect(queue.size()).toEqual(1);
2627
});
2728

2829
describe('peek()', () => {
2930
beforeEach(() => {
30-
queue.add(2);
31-
queue.add(5);
31+
queue.enqueue(2);
32+
queue.enqueue(5);
3233
});
3334

3435
it('Should return the elemet to be removed using peek()', () => {

src/_DataStructures_/Queue/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Queue extends SinglyLinkedLists {
1717
}
1818

1919
peek() {
20-
return this.getLast();
20+
return this.getFirst();
2121
}
2222

2323
size() {
@@ -41,7 +41,7 @@ class Queue extends SinglyLinkedLists {
4141
throw new Error(this.NotAllowed);
4242
}
4343

44-
getFirst() {
44+
getLast() {
4545
throw new Error(this.NotAllowed);
4646
}
4747

0 commit comments

Comments
 (0)