Thread: Is this a true Queue?

  1. #1
    Pupil
    Join Date
    Oct 2005
    Location
    Toledo
    Posts
    27

    Question Is this a true Queue?

    Is this a true Queue? It uses FIFO but it only increments and decrements the “top” variable…not rear. Most queues work from the “rear” of a stack…right?

    I originally wrote a stack that worked off a one “int” variable. Now I’m trying to make a queue.

    Code:
    //----------------------------------------------------------
    // Written By: Chad Wood
    // Date: 11-13-05
    // Description: one int queue
    //----------------------------------------------------------
    
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    const int MAX_ITEMS = 8;
    template <class ItemType>
     class Queue
      {
        private:
           ItemType stk;
           ItemType top;
           ItemType rear;
    
        public:
    
     Queue() : stk(0), rear(0), top(-1) {}
    
     void MakeEmpty()
      {
        stk = 0;
        top = -1;
      }
    
     bool IsEmpty() const
      {
        return (top == -1);
      }
    
     bool IsFull() const
      {
        return (top == MAX_ITEMS - 1);
      }
    
     void Enqueue(ItemType temp)
      {
        if (IsFull())
          {
            cout << "\n Error! Queue is full \n";
            return;
          }
    
        if (temp >15 || temp < 0)
          {
            cout << "\nError! Range is not 0-15 \n";
            return;
          }
    
        stk += temp  << (++top * 4);
    
    
      }   //end enqueue
    
     void Dequeue()
      {
        int temp = 0;
    
        if (IsEmpty())
         {
           cout << "\n Error! Queue is empty";
           return;
         }
    
        ItemType print[MAX_ITEMS]; //temp array
    
        for(ItemType i = 0; i < MAX_ITEMS; i++)
          print[i] = ( stk >> (i * 4) ) & 0x0F;  //place queue on array
    
        ItemType rv = rear;
    
    
        while(rv < top)
          {
            print[temp++] = ( stk >> (++rv * 4) ) & 0x0F;
          }
    
        stk = 0; //empty queue
    
    
        for (ItemType i = 0; temp > 0; temp--)
          {
            // push numbers back on queue
            //move each number up one space
            stk += print[i++] << (i * 4);
          }
    
        top--; //decrement top
    
      }  // end dequeue
    
    
     ItemType Top()
      {
       if (IsEmpty())
         {
           cout << "\n Error! Queue is empty";
           return 0;
         }
    
        ItemType temp = ( stk >> (rear * 4) ) & 0x0F;
        return temp;
    
      } // end Top
    
    
     void PrintStack() const
      {
        ItemType print;
        for (ItemType i = 0; i <= top; i++) //will not print if top = -1
          {
            print = ( stk >> (i * 4) ) & 0x0F;
            cout << print << endl;
          }
    
       } //end PrintStack
    
    
       }; //end class
    
    
    
    int main()
    {
      Queue<int> st;
      char i;
      while(i != '5')
        {
          cout << "\n****Press 5 to quit****";
          cout << "\n 1 Enqueue | 2 Dequeue | 3 MakeEmpty ";
          cin >> i;
          cin.ignore();
          switch(i)
          {
            case '1':
              {
    
                int num;
                cout << "Enter number "; cin >>num;
                cin.ignore();
                st.Enqueue(num);
                st.PrintStack();
                break;
              }
    
            case '2':
              {
                st.Dequeue();
                st.PrintStack();
                break;
              }
    
    
             case '3':
                {
                  st.MakeEmpty();
                  cout << "\n Queue is empty " <<endl;
                  break;
                }
    
          }  // end switch
    
    
        } // end while loop
    
      system("PAUSE");
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Is this a true Queue? It uses FIFO but it only increments and decrements the “top” variable…not rear. Most queues work from the “rear” of a stack…right?
    A stack works from the rear of a stack. On the other hand, since the first elements out of a queue are at the front, how do you propose to ignore the front of a queue and only work with the rear? A queue has to monitor both ends because it needs to add elements at the rear, and it needs to remove elements from the front--that's what FIFO means. A stack both adds elements to the rear and removes elements from the rear--that's what LIFO means.
    Last edited by 7stud; 12-04-2005 at 06:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BIG problem. How do I find a bug in so much code?
    By Yarin in forum C++ Programming
    Replies: 44
    Last Post: 01-31-2008, 12:39 PM
  2. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  3. Heap management
    By Frost Drake in forum C Programming
    Replies: 12
    Last Post: 10-15-2006, 10:06 PM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. queue question
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2001, 05:06 PM