www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-22-2009, 10:23 AM
    bluks bluks is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 2
    Question FIFO queue

    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; 
    	}
    }
    Reply With Quote
      #2  
    Old 11-22-2009, 11:46 AM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,561
    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.
    Reply With Quote
      #3  
    Old 11-22-2009, 07:20 PM
    twocold twocold is offline
    Registered User
     
    Join Date: Mar 2009
    Posts: 16
    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.
    Reply With Quote
      #4  
    Old 11-22-2009, 08:08 PM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,561
    Lightbulb Consider this ...

    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>
    Reply With Quote
      #5  
    Old 11-23-2009, 06:12 PM
    bluks bluks is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 2
    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!
    Reply With Quote
      #6  
    Old 11-23-2009, 06:15 PM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,561
    You're most welcome.
    Happy to help.
    You can also modify slightly to have a 'LIFO' also, or both if required.
    Good Luck!
    Reply With Quote
      #7  
    Old 12-02-2009, 02:39 AM
    Matyss Matyss is offline
    Registered User
     
    Join Date: Dec 2009
    Posts: 1
    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!
    Reply With Quote
      #8  
    Old 12-02-2009, 10:05 AM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,561
    Arrow

    Quote:
    Originally Posted by Matyss View Post
    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!
    I don't understand the question ... maybe it's because I don't see the source code you have created!
    Reply With Quote
      #9  
    Old 12-02-2009, 10:56 AM
    svidgen's Avatar
    svidgen svidgen is offline
    Angry Stickman
     
    Join Date: Jan 2007
    Location: Wisconsin
    Posts: 846
    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))
    Reply With Quote
      #10  
    Old 12-02-2009, 11:35 AM
    Jeff Mott's Avatar
    Jeff Mott Jeff Mott is offline
    Super Moderator
     
    Join Date: Jul 2003
    Location: The City of Roses
    Posts: 2,041
    Quote:
    JMRKER: Why so elaborate?
    Quote:
    svidgen: I'm not sure I understand the need for any custom code here.
    This is obviously a school project.
    __________________
    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";
    Reply With Quote
      #11  
    Old 12-02-2009, 12:19 PM
    svidgen's Avatar
    svidgen svidgen is offline
    Angry Stickman
     
    Join Date: Jan 2007
    Location: Wisconsin
    Posts: 846
    Quote:
    This is obviously a school project.
    Well then, the teacher should be proud of the student who learns to use the [optimized] methods that are already available
    __________________
    Jon Wire
    Webmaster and Author of Ridiculousness

    why is the sun so hot? | fling poo! | Social Bookmarking (BETA (but still really bloody cool))
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:15 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.