moving nth number to front of queue

I have an assignment where I need to create a queue stored in an array and write a function, moveNthFront that allows the user to choose which element of the queue to move to the front of the queue. The items moved from the front of the queue must appear at the end in the same order they were removed. To do this, we are required to have a "reserved slot" at the beginning of the queue that is never filled so that we can tell when the queue is full without having a counter. We are also supposed to write a program to test the function. I have almost everything working - I think - but for some reason, the program does not return the first element of the queue, only garbage. I have gone over this for hours and can't figure out where I went wrong. Some guidance would be appreciated!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
HEADER FILE:
template <class Type>
class queueType: public queueADT<Type>
{
public:
	const queueType<Type>& operator=(const queueType<Type>&);
	bool isEmptyQueue() const;//determines if queue is empty
	bool isFullQueue() const;//determines if the queue is full 
	void initializeQueue();//initializes the queue to an empty state
	Type front() const;//returns the first element of the queue 
	Type back() const;//returns the last element of the queue 
	void addQueue(const Type& queueElement);//adds an element to the end 
	void deleteQueue();//removes first element of the queue
	queueType(int queueSize = 100);//constructor
	queueType(const queueType<Type>& otherQueue);//copy constructor-delete?
	~queueType();//destructor.
	virtual void moveNthFront(int n);
	void setQueue(int size);

protected:
	int maxQueueSize;//stores the maximum queue size
	int queueFront;//points to the first element of the queue
	int queueRear;//pointer to the last element in the queue
	Type *list;//pointer to the array that holds the queue elements
};
RELEVANT FUNCTION DEFINITIONS:
template <class Type>
 Type queueType<Type>::front() const//returns first element of the queue.  
 {
	 assert(!isEmptyQueue());
	 return list[queueFront + 1];
 }

 template <class Type>
 Type queueType<Type>::back() const//returns the last element of a queue.  
 {
	 assert(!isEmptyQueue());
	 return list[queueRear];
 }

template <class Type>
void queueType<Type>::addQueue(const Type& newElement) //function to add a new element to the end of the queue
//will this need to be modified to address the reserved index slot?
{
	if ( !isFullQueue() )
	{
		queueRear = (queueRear + 1) % maxQueueSize;
		list[queueRear] = newElement;
	}
	else
		cout << "Cannot add to a full queue." << endl;
}

template <class Type>
void queueType<Type>::deleteQueue() //function to delete an element from front
{
	if (!isEmptyQueue())
	{
		queueFront = (queueFront + 1) % maxQueueSize;
	}
	else
		cout << "Cannot remove from an empty queue." << endl;
}

  template <class Type>
void queueType<Type>::moveNthFront(int n)
{
	if (!isEmptyQueue() && n > 0)
	{
		int i = 1; 
		while (i < n)
		{
			Type temp = front();
			deleteQueue();
			addQueue(temp);
			i++;
		}
	}
	else
		cout << "Cannot find element in an empty queue" << endl;
I had to enlist the help of C++ software engineer with 10 years of experience. It took him several hours, so it probably would have taken me weeks to get the issue resolved. It's too long to post everything, but the problem has been resolved.
Topic archived. No new replies allowed.