Thread: circular queue

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    4

    circular queue

    Hello everyone. I am trying to create a circular queue and need some pointers. My questions are as follows:

    1. Will my queue class produce a circular queue?

    2. The random contributors are not being added correctly to the queue. When I dequeue() the queue, it thinks it carries default values assigned from my constructor.


    All help is greatly appreciated.

    Here is the code:

    main.cpp:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    #include "contributor.h"
    #include "queue.h"
    
    int main()
    {
    	MyQueue Q;
    	
    	double Con,ID;
    	short Sex;
    	
    	srand(time(0));
    	for(int j = 0; j < Q.getSize(); j++)
    	{
    		Con = rand() % 1000 + 1;
    		Sex = rand() % 10 + 1;
    		ID = rand() % 100 + 1;
    		if (Sex > 4)
    			Q.Enqueue(Contributor("Random Male",male,Con,ID));
    		else
    			Q.Enqueue(Contributor("Random Female",female,Con,ID));
    		cout << Q.Dequeue() << endl;
    	}
    	cout << "Size = " << Q.getSize() << endl;
    	cout << Q.ShowFront() << endl;
    		
    	return 0;
    }
    queue.h:
    Code:
    #ifndef QUEUE_H
    #define QUEUE_H
    
    #include "contributor.h"
    
    class MyQueue
    {
    public:
    	MyQueue();
    	~MyQueue(){delete [] mArray;}
    	void Enqueue(Contributor &);
    	const Contributor & Dequeue();
    	const Contributor & ShowFront() const{return mArray[mFront];}
    	bool Is_Empty() const{return mFront == mRear;}
    	bool Is_Full() const{return mRear + 1 % MaxSize == mFront;}
    	short getSize() const{return MaxSize;}
    	
    private:
    	int mSize,mFront,mRear;
    	short MaxSize;
    	Contributor *mArray;
    };
    
    #endif
    queue.h:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <stack>
    using namespace std;
    
    #include "queue.h"
    #include "contributor.h"
    
    MyQueue::MyQueue()
    {
    	MaxSize = 25;
    	mArray = new Contributor[MaxSize];
    	mRear = mFront = mSize = 0;
    }
    
    void MyQueue::Enqueue(Contributor &cont)
    {
    	if (!Is_Full())
    	{
    		mArray[mRear] = cont;
    		mRear = (mRear + 1) % MaxSize;
    		mSize++;
    	}
    	else
    		cout << "ERROR: could not add to the queue - queue is full." << endl;
    }
    
    const Contributor & MyQueue::Dequeue()
    {
    	if (!Is_Empty())
    	{
    		mFront = (mFront + 1) % MaxSize;
    		return mArray[mFront];
    		mSize--;
    	}
    	else
    	{
    		cout << "ERROR: could not remove from the queue - queue is empty." << endl;
    		exit(1);
    	}
    }
    contributor.h:
    Code:
    #ifndef CONTRIBUTOR_H
    #define CONTRIBUTOR_H
    
    enum Gender_type {male,female,unknown};
    
    class Contributor
    {
    public:
    	Contributor();
    	Contributor(string,Gender_type,double,int);
    	Contributor(const Contributor &);
    	~Contributor ();
    
    	friend ostream & operator<< (ostream &, const Contributor &);
    	friend istream & operator>> (istream &, Contributor &);
    
    	const Contributor & operator= (const Contributor &);
    		
    	string getName(){return Name;}
    	double getContribution(){return Contribution;}
    	int getIDKey(){return IDKey;}
    	Gender_type getGender(){return Gender;}
    	short getGender1();
    
    	bool operator< (const Contributor &) const;
    	bool operator> (const Contributor &) const;
    	bool operator>= (const Contributor &) const;
    	bool operator== (const Contributor &) const;
    	bool operator!= (const Contributor &) const;
    
    private:
    	string Name;
    	double Contribution;
    	int IDKey;
    	Gender_type Gender;
    };
    
    #endif
    contributor.cpp:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    using namespace std;
    
    #include "queue.h"
    #include "contributor.h"
    
    Contributor::Contributor()
    {
    	Contribution = 0.00;
    	Name = "Blank";
    	Gender = unknown;
    	IDKey = 0;
    }
    Contributor::Contributor(string name,Gender_type g,double contribution,int idkey)
    {
    	Name = name;
    	Gender = g;
    	Contribution = contribution;
    	IDKey = idkey;
    }
    Contributor::Contributor(const Contributor &c)
    {
    	Name = c.Name;
    	Gender = c.Gender;
    	Contribution = c.Contribution;
    	IDKey = c.IDKey;
    }
    
    Contributor::~Contributor () 
    {}
    
    ostream & operator<< (ostream &out, const Contributor &c)
    {
    	string temp;
    	if (c.Gender == male)
    		temp = "male  ";
    	if (c.Gender == female)
    		temp = "female";
    	if (c.Gender == unknown)
    		temp = "unknown";
    	out << "Name = " << c.Name << "\tGender = " << temp << "\t" << "Contribution = " << "$" << c.Contribution << ".00" << "\t" << "IDKey = " << c.IDKey;
    	return out;
    }
    
    istream & operator>> (istream &in, Contributor &r)
    {
    	string temp;
    	cout << "Enter a name (no spaces): ";
    	in >> r.Name;
    	cout << "Enter gender (male, female, unknown): ";
    	in >> temp;
    	if (temp == "male")
    		r.Gender = male;
    	if (temp == "female")
    		r.Gender = female;
    	if (temp == "unknown")
    		r.Gender = unknown;
    	if (temp != "unknown" && temp != "male" && temp != "female")
    		r.Gender = unknown;
    	cout << "Enter a contribution: ";
    	in >> r.Contribution;
    	cout << "Enter an ID Key: ";
    	in >> r.IDKey;
    	return in;
    }
    
    const Contributor & Contributor::operator= (const Contributor &rhs)
    {
    	Contribution = rhs.Contribution;
    	Name = rhs.Name;
    	Gender = rhs.Gender;
    	IDKey = rhs.IDKey;
    	return *this;
    }
    
    bool Contributor::operator< (const Contributor &rhs) const
    {
    	return (Contribution < rhs.Contribution);
    }
    bool Contributor::operator> (const Contributor &rhs) const
    {
    	return (Contribution > rhs.Contribution);
    }
    bool Contributor::operator>= (const Contributor &rhs) const
    {
    	return (Contribution >= rhs.Contribution);
    }
    bool Contributor::operator== (const Contributor &rhs) const
    {
    	return (Name == rhs.Name);
    }
    bool Contributor::operator!= (const Contributor &rhs) const
    {
    	return (Name != rhs.Name);
    }
    
    short Contributor::getGender1() 
    {
    	if (getGender() == male)
    		return 0;
    	if (getGender() == female)
    		return 1;
    	else
    		return 2;
    }

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Why reinvent the wheel?
    STL queue and deque containers won't do it for you?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1. Will my queue class produce a circular queue?
    Good question. Will it? You sound like you have no confidence in the code that you've written. I would recommend removing all unnecessary code from the Queue class so that you can test it thoroughly using something simple, like integers:
    Code:
    class Queue {
    public:
      Queue(int max_size);
      ~Queue();
    public:
      void enqueue(int val);
      int  dequeue();
    private:
      int *vals;
      int  size;
      int  front;
      int  rear;
    };
    
    Queue::Queue(
      int max_size
      )
      : size(max_size + 1)
      , front(max_size + 1)
      , rear(0)
    {
      vals = new int[size];
    }
    
    Queue::~Queue()
    {
      delete [] vals;
    }
    
    void
    Queue::enqueue(
      int val
      )
    {
      if (rear % size + 1 == front) {
        throw -1;
      }
      vals[rear++] = val;
      if (rear == size) {
        rear = 0;
      }
    }
    
    int
    Queue::dequeue()
    {
      if (front % size == rear) {
        throw -2;
      }
      if (front == size) {
        front = 0;
      }
      return vals[front++];
    }
    Once you're confident in your queue's correctness, the other problem should be easier to debug if it still exists.
    My best code is written with the delete key.

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