class Queue{
constructor(){
this.items=[];
}
enqueue(element)
{
console.log(element+" is added to
Queue");
this.items.push(element);
}
dequeue()
{
console.log(this.items[0]+"
removed Queue");
this.items.shift();
}
peek()
{
let p= this.items[0];
console.log("First element in
queue is: "+p);
}
length()
{
let l= this.items.length;
console.log("length of queue is:
"+l);
}
print()
{
var str="";
for(var i=0;i<this.items.length;i++)
{
str+=this.items[i]+",";
}
return str;
}
clear()
{
console.log("Queue is
cleared");
this.items.length=0;
}
isEmpty()
{
let l = this.items.length;
if(l==0)
{
console.log("Queue is
Empty");
}
else
{
console.log("Queue is Not
Empty");
}
}
}
var Q =new
Queue();
console.log("1.
Enter Elemnet in Queue");
console.log("2.
Delete Element from Queue");
console.log("3.
Top Element of Queue");
console.log("4.
Length of Queue");
console.log("5.
Clear the Queue");
console.log("6.
Check Queue is Empty ");
console.log("7.
Display the Queue");
var ch;
do {
var ch = parseInt(prompt("Enter
the choice"));
switch(ch)
{
case 1: var x =
parseInt(prompt("Enter te elements to be pushed"));
Q.enqueue(x);
break;
case 2:
Q.dequeue();
break;
case 3: Q.peek();
break;
case 4: Q.length();
break;
case 5: Q.clear();
break;
case 6: Q.isEmpty();
break;
case 7: Q.print();
break;
case 8:console.log("Program
Ended");
break;
default:console.log("Invalid
choice");
}
}while(ch!=7);
Output:-
1. Enter
Element in Queue
2. Delete Element from Queue
3. Top Element
of Queue
4. Length of Queue
5. Clear the
Queue
6. Check Queue
is Empty
7. Display the
Queue
10 is added to
Queue
20 is added to
Queue
30 is added to
Queue
40 is added to
Queue
10 removed Queue
First element
in queue is: 20
length of queue
is: 3
Queue is Not
Empty
'20,30,40,'
0 Comments