Question regarding queue codes

This is a discussion on Question regarding queue codes within the C++ Programming forums, part of the General Programming Boards category; Hey all. I'm making a simple program to enqueue and dequeue a string of characters as entered into an array. ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    Question regarding queue codes

    Hey all. I'm making a simple program to enqueue and dequeue a string of characters as entered into an array. I'm fairly close to completing the code but I've run into a bit of difficulty. After a certain number of enqueues/dequeues the operation seems to break down to the point where it doesn't display some of the members of the queue. Here's the code... and then with an example of what I mean.

    Code:
    void enqueue (char queue[], int &in, int &full)
    {
        char ch;
        if (in == 4)
            { in = 0; }
        if (full == 5)
            { cout << "Enqueue: Queue is full.\n\n"; }
        else
            {
            cout << "Enqueue: ";
            cin >> ch;
            cout << endl;
            queue[in] = ch;
            in++, full++;
            }
    }
    void dequeue (char queue[], int &out, int &full)
    {
        char ch;
        if (out == 4)
            { out = 0; }
        if (full == 1)
            { cout << "Dequeue: Queue is empty.\n"; }
        else
            {
            ch = queue[out];
            cout << "Dequeue: " << ch << endl;
            out++, full--;
            }
    }
    void display (char queue[], int &out, int &full)
    {
        cout << "Queue status:";
        if (full == 1)
            { cout << " Empty"; }
        for (int i = out; i <= (full+1); i++)
            { cout << " " << queue[i]; }
        cout << endl << endl;
    }
    main ()
    {
       char selection ;
       char mainqueue[4] ;
       int n = 0, d = 0;
       int full = 1;
       /*  Declare and initialize various counters.  */
    
       cout << endl ;
       cout << "Enter e to enqueue, d to dequeue or q to quit." ;
       cout << endl << endl << "e, d or q: " ;
       cin >> selection ;
       while ( ( selection == 'e' ) || ( selection == 'd' ) )
          {
          switch ( selection )
             {
             case 'e' :  enqueue (mainqueue, n, full);
                break ;
             case 'd' :  dequeue (mainqueue, d, full);
                break ;
             }
          display (mainqueue, d, full) ;
          cout << "e, d or q: " ;
          cin >> selection ;
          }
    }
    A sample output after several enqueue/dequeue progressions (my "notes" are in bold):

    Code:
    Enter e to enqueue, d to dequeue or q to quit.
    FIRST RUN
    e, d or q: e
    
    Enqueue: a
    
    Queue status: a
    
    SECOND RUN
    e, d or q: e
    
    Enqueue: b
    
    Queue status: a b
    
    THIRD RUN
    e, d or q: e
    
    Enqueue: c
    
    Queue status: a b c
    
    FOURTH RUN
    e, d or q: e
    
    Enqueue: d
    
    Queue status: a b c d
    
    FIFTH RUN
    e, d or q: e
    
    Enqueue: Queue is full.
    
    Queue status: a b c d
    
    SIXTH RUN
    e, d or q: d
    
    Dequeue: a
    
    Queue status: b c d
    
    SEVENTH RUN
    e, d or q: d
    
    Dequeue: b
    
    Queue status: c d
    
    EIGHTH RUN
    e, d or q: d
    
    Dequeue: c
    
    Queue status: d
    
    NINTH RUN
    e, d or q: d
    
    Dequeue: d
    
    Queue status: Empty
    
    TENTH RUN -- HERE'S WHERE IT BREAKS DOWN
    e, d or q: e
    
    Enqueue: g
    
    Queue status: Should output a "g" here...
    
    ELEVENTH RUN
    e, d or q: e
    
    Enqueue: g
    
    Queue status: Should output "g g" here
    
    TWELFTH RUN
    e, d or q: e
    
    Enqueue: k
    
    Queue status: Should show "g g k"...
    
    THIRTEENTH RUN
    e, d or q: e
    
    Enqueue: l
    
    Queue status: Should show "g g k l"...
    
    RUN 14
    e, d or q: e
    
    Enqueue: Queue is full.
    
    Queue status: Doesn't display queue again.
    
    RUN 15
    e, d or q: d
    
    Dequeue: g
    
    Queue status: g k l
    
    e, d or q: d
    
    Dequeue: g
    
    Queue status: k l
    
    e, d or q: d
    
    Dequeue: k
    
    Queue status: l
    
    e, d or q: d
    
    Dequeue: l
    
    Queue status: Empty
    
    e, d or q: q It runs fine for the dequeue as far as displaying the queue.
    Basically, it doesn't display the queue after I've enqueued/dequeued a full (read: 4-element) queue. It was deemed to be a "circular" queue too. Any advice?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How can n decrease, when you pass d to dequeue?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Again, it's supposed to be a circular queue. Once it reaches the end of the queue (in this case, when in/n = 4), it should revert back to 0... I'm not quite sure what you're getting at here. The problem seems to be in the display function.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The print statement is using out as a starter -- and out is still 4 until you call dequeue again. You need to do the circle right when you do in++ or out++, not the next time through the function.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    When I put the in/out check after the in++ and out++, it only exacerbates the problem:

    Code:
    #include <iostream>
    using namespace std ;
    
    void enqueue (char queue[], int &in, int &full)
    {
        char ch;
        if (full == 4)
            { cout << "Enqueue: Queue is full.\n"; }
        else
            {
            cout << "\nEnqueue: ";
            cin >> ch;
            cout << endl;
            queue[in] = ch;
            in++, full++;
            }
        if (in == 4)
            { in = 0; }
    }
    void dequeue (char queue[], int &out, int &full)
    {
        char ch;
        if (full == 0)
            { cout << "Dequeue: Queue is empty.\n"; }
        else
            {
            ch = queue[out];
            cout << "Dequeue: " << ch << endl;
            out++, full--;
            }
        if (out == 4)
            { out = 0; }
    }
    void display (char queue[], int &out, int &full)
    {
        cout << "Queue status:";
        if (full == 0)
            { cout << " Empty"; }
        for (int i = out; i <= full; i++)
            { cout << " " << queue[i]; }
        cout << endl << endl;
    }
    main ()
       {
       char selection ;
       char mainqueue[4] ;
       int n = 0, d = 0;
       int full = 0;
    
       cout << endl ;
       cout << "Enter e to enqueue, d to dequeue or q to quit." ;
       cout << endl << endl << "e, d or q: " ;
       cin >> selection ;
       while ( ( selection == 'e' ) || ( selection == 'd' ) )
          {
          switch ( selection )
             {
             case 'e' :  enqueue (mainqueue, n, full);
                break ;
             case 'd' :  dequeue (mainqueue, d, full);
                break ;
             }
          display (mainqueue, d, full) ;
          cout << "e, d or q: " ;
          cin >> selection ;
          }
       }
    Sample output...

    Code:
    Enter e to enqueue, d to dequeue or q to quit.
    
    e, d or q: e
    
    Enqueue: a
    
    Queue status: a
    
    e, d or q: e
    
    Enqueue: b
    
    Queue status: a b
    
    e, d or q: e
    
    Enqueue: c
    
    Queue status: a b c
    
    e, d or q: e
    
    Enqueue: d
    
    Queue status: a b c d
    
    e, d or q: e
    Enqueue: Queue is full.
    Queue status: a b c d
    
    e, d or q: d
    Dequeue: a
    Queue status: b c d
    
    e, d or q: d
    Dequeue: b
    Queue status: c
    
    e, d or q: d
    Dequeue: c
    Queue status:
    
    e, d or q: d
    Dequeue: d
    Queue status: Empty a
    
    e, d or q: d
    Dequeue: Queue is empty.
    Queue status: Empty a
    
    e, d or q: e
    
    Enqueue: g
    
    Queue status: g b
    
    e, d or q: e
    
    Enqueue: h
    
    Queue status: g h c
    
    e, d or q: e
    
    Enqueue: i
    
    Queue status: g h i d
    
    e, d or q: e
    
    Enqueue: j
    
    Queue status: g h i j
    
    e, d or q: e
    Enqueue: Queue is full.
    Queue status: g h i j
    
    e, d or q: d
    Dequeue: g
    Queue status: h i j
    
    e, d or q: d
    Dequeue: h
    Queue status: i
    
    e, d or q: d
    Dequeue: i
    Queue status:
    
    e, d or q: q

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If by "exacerbated" you mean "exposed all the other errors that were hiding behind the first one", then yes. Your display is still broken, in that (a) it doesn't print the right number of things (when you have one element in the queue, full is then 2, which means you actually print three elements mainqueue[0], mainqueue[1], and mainqueue[2]) and (b) it has no way to "wrap around" -- for instance if you had enqueued four, dequeued two, and then enqueued one, you would have your queue in mainqueue[2], mainqueue[3], and mainqueue[0] -- but you would print 2, 3, 4 instead.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Quote Originally Posted by tabstop View Post
    If by "exacerbated" you mean "exposed all the other errors that were hiding behind the first one", then yes.
    Touche. I'm not even going to pretend I'm a great programmer... I'm going off hunches when I do these first off and then I have to go back and fix them once I see they're incorrect... and sometimes the errors aren't obvious to me. haha

    Your display is still broken, in that (a) it doesn't print the right number of things (when you have one element in the queue, full is then 2, which means you actually print three elements mainqueue[0], mainqueue[1], and mainqueue[2]) and (b) it has no way to "wrap around" -- for instance if you had enqueued four, dequeued two, and then enqueued one, you would have your queue in mainqueue[2], mainqueue[3], and mainqueue[0] -- but you would print 2, 3, 4 instead.
    Okay. In that case, a check for my "i" would be necessary to get it to wrap around, but I can see that such a check (inside the for-loop) would cause a never-ending loop. I get what you're saying (because I traced out the program myself a few times to see where the breakdown(s) was/were), but I'm unsure of how to really implement such a check.

    Anybody able to give any other advice?
    Last edited by Velocity; 11-02-2008 at 01:29 PM.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    It seems like if I had a hybrid of my first and second code sets (the ones with outputs given) it'd be close to being correct. Of course, the problem with the display function is still nagging. How am I supposed to be able to get the [i] to reset to zero in such a case where components [2], [3] and [0] (for example) are supposed to be output? Without creating an infinite loop?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think the array index and the thing controlling the loop should be the same? If your loop control was j, your array index would have to be (out+j)%5.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    The problem in that case is that after dequeuing just one item, it skips the second item in the queue.

    a b c d

    Dequeue a

    Queue Status: c d

    Dequeue b

    Queue Status: [shows nothing]

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    j doesn't start at out; it starts at 0 and goes to (full-1) I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Queue Question
    By RobJ in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 09:28 AM
  2. Priority Queue Question
    By ac251404 in forum C++ Programming
    Replies: 26
    Last Post: 08-16-2006, 11:51 AM
  3. Is this a true Queue?
    By chad101 in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2005, 05:00 AM
  4. linked-list queue
    By the_winky_files in forum C Programming
    Replies: 17
    Last Post: 11-21-2005, 02:57 PM
  5. Need help quick, compiler/file problem.
    By alex0486 in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2005, 01:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21