Hey guys. I am trying to take a normal queue and change it into a cyclic queue, which basically means that the enqueue operation will make the elements shift to make space if the rear pointer is pointing at the end of the queue.

I am getting an error about a "Missing External Link," but I also am not too sure if I am implementing the class correctly as this is my first program using OOP. If you guys have any suggestions that would be awesome. Thanks :)

Also, if you guys could let me know how to add line numbers to my code that would be huge, as I'm sure its annoying to try and help without them.

#include <iostream>
using namespace std;

class CirQueue
{
private:
	int queue_size;
	int n;
protected:
	int *buffer;
	int front;
	int rear;
public:
	CirQueue(void)
	{
		front=0;
		rear=0;
		queue_size=10;
		buffer= new int[queue_size];
	}
	CirQueue (int n)
	{
		front=0;
		rear=0;
		queue_size=n;
		buffer=new int[queue_size];
	}
	~CirQueue(void)
	{
		delete buffer;
		buffer= NULL;
	}
	void enqueue(int v)
	{
		if (rear<queue_size)
			buffer[rear++]=v;
		else if(rear==n)
			if(front>0)
				rear=0;
		
		else
			if(compact())
				buffer[rear++]=v;
	}
	int dequeue(void)
	{
		if(front<rear)
			return buffer[front++];
		else
		{
			return -1;
		}
	}
private:
	bool compact(void);
};

void main()
{
	CirQueue Q1(5);
	Q1.enqueue(2);
	Q1.enqueue(8);
	int x = Q1.dequeue();
	int y = Q1.dequeue();
}

1. main returns an int - no ifs buts or maybes.

2. bool compact(void);
But where is the code to do this?
You have to write the implementation as well.

> Also, if you guys could let me know how to add line numbers to my code that would be huge
In the code tags, you say
code=c++
Like so

int main ( ) {
  return 0;
}

Ok, so the last thing that I want to do with this is create a displayqueue operation instead of using the cout in the main. I know I can't use the dequeue function in the displayQueue function, so I was wondering what would be an adequate way of approaching this.

Any suggestions?

#include <iostream>
using namespace std;

class CirQueue
{
private:
	int queue_size;
	int n;
	int i;
protected:
	int *buffer;
	int front;
	int rear;
public:
	CirQueue(void)
	{
		front=0;
		rear=0;
		queue_size=10;
		buffer= new int[queue_size];
	}
	CirQueue (int n)
	{
		front=0;
		rear=0;
		queue_size=n;
		buffer=new int[queue_size];
	}
	~CirQueue(void)
	{
		delete buffer;
		buffer= NULL;
	}
	void enqueue(int v)
	{
		if (rear<queue_size)
			buffer[rear++]=v;
		else if(rear==queue_size)
			if(front>0)
				rear=0;
			else
				cout<< "The queue is full" << endl;
		
		else
			if(compact())
				buffer[rear++]=v;
	}
	int dequeue(void)
	{
		if(front<rear)
			return buffer[front++];
		else
		{
			cout << "The queue is empty" << endl;
		}
	}
	bool compact(void)
	{
		if (front==0)
		{
			return false;
		}
		else
		{
			for(int i=0;i<rear-front;i++)
				buffer[i]=buffer[i+front];
			rear=rear - front;
			front=0;
			return true;
		}
	}
};

int main()
{
	CirQueue Q1(4);
	Q1.enqueue(2);
	Q1.enqueue(8);
	Q1.enqueue(10);
	Q1.enqueue(12);
	int w = Q1.dequeue();
	int x = Q1.dequeue();
	int y = Q1.dequeue();
	int z = Q1.dequeue();
	cout<< "x=" << x << endl << "y=" << y << endl << "z=" << z <<endl;
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.