Circular queue Code implemented by array in javascript
Source: Internet
Author: User
// 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;
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service