Thread: Printing all items in a Queue

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Printing all items in a Queue

    Hello,

    I'm currently attempting to print out all the items in a Queue. Here is the code I'm using to do this:

    Code:
          while (event >> word2) {
        if (word2 == "1") {
            string elem1 = run.front();
            if (ready.size() != 0 ) {
                    string toRun1 = ready.front();
                    run.pop();
                    run.push(toRun1);
            }
        }
        else if (word2 == "2") {
            string elem2 = run.front();
            run.pop();
            wait.push(elem2);
            if (ready.size() != 0) {
                    string toRun2 = ready.front();
                    run.push(toRun2);
            }
            else {
                    string toRun2 = wait.front();
                    run.push(toRun2);
            }
        }
        else if (word2 == "3") {
            string elem3 = wait.front();
            wait.pop();
            ready.push(elem3);
            if (ready.size() == 0 && run.size() == 1) {
                    string toRun3 = ready.front();
                    ready.pop();
                    run.push(toRun3);
            }
        }
    
        int runSize = run.size();
        int readySize = ready.size();
        int waitSize = wait.size();
      
    
    // Printing items begins here
      cout << "Event #" << eventNum << endl;
      cout << "Running: ";
      while (runSize != 0) {
        string running = run.front();
        cout << running << " ";
        run.pop();
        run.push(running);
        runSize--;
      }
      cout << endl;
      cout << "Ready: ";
      while (readySize != 0) {
        string readyToGo = ready.front();
        cout << readyToGo << " ";
        ready.pop();
        ready.push(readyToGo);
        readySize--;
      }
      cout << endl;
      cout << "Waiting: ";
      while (waitSize != 0) {
        string waiting = wait.front();
        cout << waiting << " ";
        wait.pop();
        wait.push(waiting);
        waitSize--;
      }
      cout << endl;
      eventNum++;
      }
    This prints everything in the Queues, but I think it alters my results as it pushes items back on to it that I wanted to remove earlier in my code. What I want to do is remove items from the Queues depending on the situation. When I print the results, nothing is removed from the Queues, but things are added to them. Any suggestions?


    EDIT: Actually, I just realized that the if (word2 == "3") part of the first part of the code is working correctly. It removes from the wait Queue and puts it on to the ready queue. However, the first two if statements are not removing from their Queues.
    Last edited by Zildjian; 09-11-2004 at 12:43 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <deque>
    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    template <typename T, typename Container = deque<T> >
    class traversable_queue: public queue<T, Container> {
    public:
      typedef typename Container::iterator iterator;
      typedef typename Container::const_iterator const_iterator;
    
      iterator begin() { return c.begin(); }
      const_iterator begin() const { return c.begin(); }
      iterator end() { return c.end(); }
      const_iterator end() const { return c.end(); }
    };
    
    int main()
    {
      typedef traversable_queue<int> tr_queue;
      tr_queue q;
    
      for ( int i = 0; i < 10; i++ )
        q.push ( i );
    
      for ( tr_queue::const_iterator it = q.begin(); it != q.end(); ++it )
        cout<< *it;
      cout<<endl;
    
      for ( tr_queue::const_iterator it = q.begin(); it != q.end(); ++it )
        cout<< *it;
      cout<<endl;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Hahaha, thanks! This will help a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM