curriculum/challenges/english/blocks/lab-implement-a-queue/69aaafeaa4037197c75587cd.md
In this lab, you will implement a queue data structure using functions. A queue is a First-In-First-Out (FIFO) data structure where elements are added to the back of the queue and removed from the front.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
enqueue function that adds an element to the back of the queue.dequeue function that removes and returns the front element of the queue.front function that returns the front element of the queue without removing it.size function that returns the number of elements in the queue.isEmpty function that returns true if the queue is empty and false otherwise.You should have an enqueue function.
assert.isFunction(enqueue);
enqueue function should add an element to the back of the queue.
const queue = initQueue();
enqueue(queue, 'first');
enqueue(queue, 'second');
assert.strictEqual(front(queue), 'first');
assert.strictEqual(size(queue), 2);
You should have a dequeue function.
assert.isFunction(dequeue);
dequeue function should remove and return the front element of the queue.
const queue = initQueue();
enqueue(queue, 'first');
enqueue(queue, 'second');
enqueue(queue, 'third');
assert.strictEqual(dequeue(queue), 'first');
assert.strictEqual(dequeue(queue), 'second');
assert.strictEqual(size(queue), 1);
dequeue should return undefined when called on an empty queue.
const queue = initQueue();
assert.isUndefined(dequeue(queue));
enqueue should continue to add elements to the back of the queue after dequeues.
const queue = initQueue();
enqueue(queue, 'first');
enqueue(queue, 'second');
assert.strictEqual(dequeue(queue), 'first');
enqueue(queue, 'third');
assert.strictEqual(front(queue), 'second');
assert.strictEqual(dequeue(queue), 'second');
assert.strictEqual(dequeue(queue), 'third');
Different queue instances should not share state.
const queueOne = initQueue();
const queueTwo = initQueue();
enqueue(queueOne, 'first');
enqueue(queueTwo, 'second');
assert.strictEqual(front(queueOne), 'first');
assert.strictEqual(front(queueTwo), 'second');
assert.strictEqual(size(queueOne), 1);
assert.strictEqual(size(queueTwo), 1);
You should have a front function.
assert.isFunction(front);
front function should return the front element of the queue without removing it.
const queue = initQueue();
enqueue(queue, 'first');
enqueue(queue, 'second');
assert.strictEqual(front(queue), 'first');
assert.strictEqual(front(queue), 'first');
assert.strictEqual(size(queue), 2);
front should return undefined when called on an empty queue.
const queue = initQueue();
assert.isUndefined(front(queue));
You should have a size function.
assert.isFunction(size);
size function should return the correct number of elements in the queue.
const queue = initQueue();
assert.strictEqual(size(queue), 0);
enqueue(queue, 'first');
assert.strictEqual(size(queue), 1);
enqueue(queue, 'second');
enqueue(queue, 'third');
assert.strictEqual(size(queue), 3);
dequeue(queue);
assert.strictEqual(size(queue), 2);
You should have an isEmpty function.
assert.isFunction(isEmpty);
isEmpty function should return true for an empty queue.
const queue = initQueue();
assert.isTrue(isEmpty(queue));
isEmpty function should return false for a non-empty queue.
const queue = initQueue();
enqueue(queue, 'element');
assert.isFalse(isEmpty(queue));
isEmpty function should return true after all elements are removed from the queue.
const queue = initQueue();
enqueue(queue, 'first');
enqueue(queue, 'second');
dequeue(queue);
dequeue(queue);
assert.isTrue(isEmpty(queue));
function initQueue() {
return {
collection: []
};
}
function print(queue) {
console.log(queue.collection);
}
function enqueue(queue, element) {
}
function dequeue(queue) {
}
function front(queue) {
}
function size(queue) {
}
function isEmpty(queue) {
}
function initQueue() {
return {
collection: []
};
}
function print(queue) {
console.log(queue.collection);
}
function enqueue(queue, element) {
queue.collection.push(element);
}
function dequeue(queue) {
return queue.collection.shift();
}
function front(queue) {
return queue.collection[0];
}
function size(queue) {
return queue.collection.length;
}
function isEmpty(queue) {
return queue.collection.length === 0;
}