<script>
const MAX = 100;
class Deque {
constructor(size){
this
.arr =
new
Array(size).fill(0);
this
.front = -1;
this
.rear = 0;
this
.size = size;
}
isFull()
{
return
((
this
.front == 0 &&
this
.rear ==
this
.size - 1) ||
this
.front ==
this
.rear + 1);
}
isEmpty() {
return
(
this
.front == -1);
}
insertfront(key)
{
if
(
this
.isFull()) {
console.log(
"overflow"
);
return
;
}
if
(
this
.front == -1) {
this
.front = 0;
this
.rear = 0;
}
else
if
(
this
.front == 0)
this
.front =
this
.size - 1;
else
this
.front =
this
.front - 1;
this
.arr[
this
.front] = key;
}
insertrear(key)
{
if
(
this
.isFull()) {
console.log(
"overflow"
);
return
;
}
if
(
this
.front == -1) {
this
.front = 0;
this
.rear = 0;
}
else
if
(
this
.rear ==
this
.size - 1)
this
.rear = 0;
else
this
.rear =
this
.rear + 1;
this
.arr[
this
.rear] = key;
}
deletefront()
{
if
(
this
.isEmpty()) {
console.log(
"Queue Underflow"
);
return
;
}
if
(
this
.front ==
this
.rear) {
this
.front = -1;
this
.rear = -1;
}
else
if
(
this
.front ==
this
.size - 1)
this
.front = 0;
else
this
.front =
this
.front + 1;
}
getFront()
{
if
(
this
.isEmpty()) {
console.log(
" Underflow"
);
return
-1;
}
return
this
.arr[
this
.front];
}
getRear()
{
if
(
this
.isEmpty() ||
this
.rear < 0) {
console.log(
" Underflow"
);
return
-1;
}
return
this
.arr[
this
.rear];
}
};
let dq =
new
Deque(5);
document.write(
"Inserted element at rear end : 10 \n"
);
dq.insertrear(10);
document.write(
"Inserted element at rear end : 15 \n "
);
dq.insertrear(15);
document.write(
"Inserted element at front end : 5 \n"
);
dq.insertfront(5);
document.write(
"Get rear element : "
, dq.getRear());
document.write(
"\n"
);
document.write(
"Get front element : "
, dq.getFront());
document.write(
"\n"
);
dq.deletefront();
document.write(
"After delete front element new front become : "
, dq.getFront());
</script>