|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey!
I have started to make some small project where i need some of queue functions. I managed to make some Last in First Out queue. But can't think how to make similar FIFO queue. Can somebody give some help or hints on this? Thanks Here is simplified version of LIFO. Code:
function LIFO(Size)
{
this.Buffer = new Array(Size);
this.TopOfStack = 0; // Empty
this.Push = Push;
this.Pop = Pop;
this.Show = Show;
}
function Push(Value)
{
if(this.TopOfStack >= this.Buffer.length)
{
return false;
}
else
{
this.Buffer[this.TopOfStack] = Value;
this.TopOfStack++
return true;
}
}
function Pop()
{
if(this.TopOfStack > 0)
{
this.TopOfStack--;
return this.Buffer[this.TopOfStack]
}
else
{
return false;
}
}
|
|
#2
|
|||
|
|||
|
Look into the .shift() and .unshift() functions which operate similar to the .push() and .pop() functions but at the other end of the array.
Should be fairly easy to implement from what you have started. Post back if you have problems. |
|
#3
|
|||
|
|||
|
Hello,I wrote down this codes just as the queue algorithms in java,hope it can help you.
function queueObject(size){ this.maxSize = size; this.queueArray = new Array(size); this.front = 0; this.rear = -1; this.insertItem = function(item){ if(this.rear == this.maxSize - 1) this.rear = -1; this.queueArray[++this.rear] = item; } this.removeItem = function(){ var item = this.queueArray[this.front++]; if(this.front == maxSize) this.front = 0; return item; } this.peekItem = function(){ return this.queueArray[this.front]; } this.isEmpty = function(){ return (this.rear + 1 == this.front || (this.front + this.maxSize - 1 == this.rear)); } this.isFull = function(){ return (this.rear + 2 == this.front || (this.front + this.maxSize - 2 == this.rear)); } this.getQueueSize = function(){ if(this.rear >= this.front){ return this.rear - this.front + 1; } else{ return (this.maxSize - this.front) + (this.rear + 1); } } } You can test it like this: var que = new queueObject(5); que.insertItem('a'); que.insertItem('b'); que.insertItem('c'); alert(que.isEmpty()); Last edited by twocold; 11-22-2009 at 07:31 PM. |
|
#4
|
|||
|
|||
|
Why so elaborate?
![]() Try this ... Code:
<html>
<head>
<title>FIFO Queue</title>
<script type="text/javascript">
// For: http://www.webdeveloper.com/forum/showthread.php?t=220340
Array.prototype.store = function (info) {
this.push(info);
}
Array.prototype.fetch = function() {
if (this.length <= 0) { return ''; } // nothing in array
return this.shift();
}
Array.prototype.display = function() {
return this.join('\n');
}
var FIFO = new Array();
function StoreEntry() {
var sel = document.getElementById('entry');
FIFO.store(sel.value);
sel.value = '';
sel.focus();
}
function FetchEntry() {
document.getElementById('entry').value = FIFO.fetch();
}
</script>
</head>
<body>
<h1>FIFO Queue Example</h1>
<input type="text" id="entry">
<br>
<button onclick="StoreEntry()">Store</button>
<button onclick="FetchEntry()">Fetch</button>
<button onclick="alert(FIFO.display())">Display</button>
</body>
</html>
|
|
#5
|
|||
|
|||
|
WoW you gave me a bunch of help! It looks like i'll mix some of these ideas together, and it will work just fine. Thank you very much! If there will be something else to ask i now know the best place to do it! Thanks again!
![]()
|
|
#6
|
|||
|
|||
|
You're most welcome.
Happy to help. You can also modify slightly to have a 'LIFO' also, or both if required. Good Luck!
|
|
#7
|
|||
|
|||
|
Question! I have a problem with this source code, how do I get situation when I fetch out an element but queue stands in place. Like I have buffer max of 10 elements and when it, is full I fetch first element, and add new one in it's place!
|
|
#8
|
|||
|
|||
|
Quote:
|
|
#9
|
||||
|
||||
|
I'm not sure I understand the need for any custom code here. JavaScript arrays should provide "all" the functionality you need. Use arrayname.push(element) to tack something onto one end and element = arrayname.shift() to get the oldest element out.
And of course, if you need to remove the pop() and unshift() methods for your queue for data integrity purposes, I think you can just remove them: arrayname.pop = null; arrayname.unshift = null;. Am I oversimplifying this in my head? Or did I possibly miss a point here?
__________________
Jon Wire Webmaster and Author of Ridiculousness why is the sun so hot? | fling poo! | Social Bookmarking (BETA (but still really bloody cool)) |
|
#10
|
||||
|
||||
|
Quote:
Quote:
__________________
for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"')) {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n"; |
|
#11
|
||||
|
||||
|
Quote:
__________________
Jon Wire Webmaster and Author of Ridiculousness why is the sun so hot? | fling poo! | Social Bookmarking (BETA (but still really bloody cool)) |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|