2018-10-04 14:47:55 +01:00
---
title: Create a Queue Class
---
2019-07-24 00:59:27 -07:00
# Create a Queue Class
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-20 23:12:13 +05:30
- A Queue is an abstract Data Structure.
- A Queue folow FIFO/LILO principle.
- In this challenge we nede to implement `enqueue()` , `dequeue()` , `front()` , `size()` , `isEmpty()` methods.
- `enqueue()` - This method adds the element to the queue.
- `dequeue()` - This method removes the first element from the queue.
- `front()` - This method returns the first element in the queue that'd be dequeue'd.
- `size()` - This method returns the size of the queue.
- `isEmpty()` - This method returns if the queue is empty.
-
| DS | Access | Search | Insert | Delete |
| ----- | ------ | ------ | ------ | ------ |
| Queue | n | n | 1 | 1 |
- 
2018-10-04 14:47:55 +01:00
2018-10-20 23:12:13 +05:30
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
**Note:** This solution is not exactly a queue, the shift() method used in the dequeue() method is of complexity `O(n)` and not `O(1)` . However, the advanced solution rectifies this and uses Object(HashTables) instead of Array to implement Queue.
2018-10-20 23:12:13 +05:30
```js
2019-07-24 00:59:27 -07:00
function Queue() {
var collection = [];
this.print = function() {
console.log(collection);
};
this.enqueue = function(val) {
collection.push(val);
};
this.dequeue = function() {
return collection.shift();
};
this.front = function() {
return collection[0];
};
this.size = function() {
return collection.length;
};
this.isEmpty = function() {
return collection.length === 0;
};
2018-10-20 23:12:13 +05:30
}
```
2019-07-24 00:59:27 -07:00
< / details >
< details > < summary > Solution 2 (Click to Show/Hide)< / summary >
2018-10-20 23:12:13 +05:30
```js
class Queue {
2019-07-24 00:59:27 -07:00
constructor() {
this.collection = {};
this.start = 0;
this.end = 0;
}
print() {
console.log(this.collection);
}
enqueue(val) {
this.collection[this.end++] = val;
}
dequeue() {
return this.collection[this.start++];
}
front() {
return this.collection[this.start];
}
size() {
return this.end - this.start;
}
isEmpty() {
return this.size() === 0;
}
2018-10-20 23:12:13 +05:30
}
```
2019-07-24 00:59:27 -07:00
#### Relevant Links
2018-10-20 23:12:13 +05:30
- [Wikipedia ](https://en.wikipedia.org/wiki/Queue_(abstract_data_type ))
2019-07-24 00:59:27 -07:00
< / details >