DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Circular Queue iteration

  1. #1
    Join Date
    Feb 2004
    Posts
    8

    Circular Queue iteration

    How you doing guys,

    i'm trying to loop through and array of queues and i'm suppose to keep looping in order until all the queues are empty. here what it looks like

    int currentQ=0;
    int numQueue; // total number of queues
    boolean customer = true;
    while (customer) { //infinite loop

    //Are all the Queues Empty?
    // if (allEmpty(queues)) {
    // System.out.println(
    // "No more customers to see, all lines are clear");
    // break;
    // }

    if (queues[currentQ].isEmpty()) { // if this Queue is empty, go to the next
    currentQ++;
    continue;
    }

    if (currentQ == numQueue-1) {
    currentQ = 0; // get set back to Queue 1


    Ticket tickSeen = (Ticket) queues[currentQ].dequeue();
    System.out.println(tickSeen);
    custome = false;
    }

    } //end of infinite while


    ps: allEmpty() just check every queues to see if there empty

    I'm running out of idea, it doesn't so difficult but i just don't see it, can anyone direct me in the right direction? lol

    thanks,
    Mel
    If A equals success, then the formula is: A = X + Y + Z where X is work; Y is play; Z is keep your mouth shut. Albert Einstein

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    thanks to wacky use of {} brackets, you only ever dequeue items from the first queue..

    there's 2 ways you can empty queues.. one at a time, or all in parallel. your method looks parallel so here is what i present. you will have to fill out this code:

    Code:
    boolean somQueuesHaveItems
    
    while someQueuesHaveItems{
    
      someQueuesHaveItems = false
    
      for each index in theQueues array{
    
        if the queue at this index is empty
          continue on to the next iteration of the for loop
        else{
          create a temporary ticket
          dequeue the front item into the temp ticket
          print the item
          if the queue at this index is NOT empty (yet - remember we dequeued)
            some queues still have items is true
        }
      }
    }
    can you see how that logic works.. as long as at least one queue has an item left in it, the for loop will start again. if no queue has items remaining, the boolean is never set to true and the while loop exits. you COULD do this with one loop, but 2 is easier to think about, basically because you have a loop that pulls items off, and another loop that decides if that loop should run again
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    8

    Nice it works

    Thanks for the info cjard, you learn something new everyday , i'll make sure i put this to good use.
    If A equals success, then the formula is: A = X + Y + Z where X is work; Y is play; Z is keep your mouth shut. Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center