// Cyclic queue
Function CircleQueue (size ){
This. initQueue (size );
}
CircleQueue. prototype = {
// Initialize the queue
InitQueue: function (size ){
This. size = size;
This. list = new Array ();
This. capacity = size + 1;
This. head = 0;
This. tail = 0;
},
// Press into the queue
EnterQueue: function (ele ){
If (typeof ele = "undefined" | ele = ""){
Return;
}
Var pos = (this. tail + 1) % this. capacity;
If (pos = this. head) {// determines whether the queue is full.
Return;
} Else {
This. list [this. tail] = ele;
This. tail = pos;
}
},
// Retrieve the header data from the queue
DelQueue: function (){
If (this. tail = this. head) {// determines whether the queue is empty.
Return;
} Else {
Var ele = this. list [this. head];
This. head = (this. head + 1) % this. capacity;
Return ele;
}
},
// Query whether this element exists in the queue. If any subscript is returned,-1 is returned.
Find: function (ele ){
Var pos = this. head;
While (pos! = This. tail ){
If (this. list [pos] = ele ){
Return pos;
} Else {
Pos = (pos + 1) % this. capacity;
}
}
Return-1;
},
// Returns the number of elements in the queue.
QueueSize: function (){
Return (this. tail-this. head + this. capacity) % this. capacity;
},
// Clear the queue
ClearQueue: function (){
This. head = 0;
This. tail = 0;
},
// Determine whether the queue is empty
IsEmpty: function (){
If (this. head = this. tail ){
Return true;
} Else {
Return false;
}
}
}