Long day, I can't seem to figure this out.
Why is this code not marking that the queue is full.. I'm just including my Add function that verifies if they want to add another number to the queue. The isFull function works fine, have used it on other programs.
template <class T> //Template currSize function
int Queue<T> :: currSize ()
{
return (rear - front + arraylength) % arraylength; //
}
void addTo(Queue <int> &nums)
{
char ans; //Holds input from user
int input;
cout << "Add a number? (Y/N): ";
cin >> ans;
while (ans != 'Y' && ans != 'y' && ans != 'n' && ans != 'N')
{
cout << "\nNot a valid response, try again: ";
cin >> ans;
}
if (ans == 'N')
{
return;
}
elseif ((ans == 'Y' || ans == 'y') && !nums.isFull())
{
cout << "\nWhat number?: ";
cin >> input;
nums.add(input);
cout << "\n" << 5-nums.currSize() << " spots left! ";
addTo(nums);
}
elseif (nums.isFull())
{
cout << "\nSorry the queue is already full!";
return;
}
}
The output goes all the way down to 1 spot left, lets the user enter the last element. Then it asks if they want to add another.. At this point, when they hit Y, it lets them add it and it says there are 5 spots left!
> Why is this code not marking that the queue is full
> The isFull function works fine
If the queue is full, but `isFull()' does not reflect that, then I would say that `isFull()' does not work fine.
¿How do you differentiate an empty queue from a full queue?
template <class T> //Template isFull function
int Queue<T> :: isFull()
{
if ((rear + 1) % arraylength == front) //if next spot is the front, we are full
{
return 1;
}
else
{
return 0;
}
}
//This program prints out the reverse order of a queue
#include <iostream>
#include <cctype>
#include <cstdlib>
usingnamespace std;
template <class T> //Template for a queue
class Queue //This is a queue class
{
public:
~ Queue (); // destructor for queue
Queue(int); //default constuctor, passed a int variable for size
T getFront(); //returns a COPY of the front element
void add (T element); // adds element to the queue
T deleteIt (); //deletes an element from the queue
T getBack (); //returns a COPY of the back element
int isEmpty(); //checks if queue is empty
int currSize(); //returns the current size of the queue
int isFull(); //checks if queue is full
private:
int front; //one ahead of the front element
int rear; //position of the back element
int arraylength; //queue capacity
T *q_ptr; //pointer holds a spot in memory
};
template <class T> //Template constructor
Queue<T> :: ~ Queue() //passed maxsize from initial declaration of stack
{
delete [] q_ptr; //deletes the queue
}
template <class T> //Template constructor
Queue<T> :: Queue(int maxsize) //passed maxsize from initial declaration of queue
{
arraylength = maxsize; //sets private length variable equal to size passed from declaration at main
front = 0; //front and rear are initally -1 because nothing is in queue
rear = 0;
q_ptr = new T[arraylength]; //dynamically allocates memory to where pointer was stationed
}
template <class T> //Template add function
void Queue<T> :: add (T element) //passed an element t add to queueu
{
if (!isFull()) //if isFull function is not true (not full)
{
rear = (rear + 1) % arraylength; //rear goes to next available spot
q_ptr [rear] = element; //add the element to this open spot
}
else
{
cout << "\nQueue is full";
}
}
template <class T> //Template delete function
T Queue<T> :: deleteIt ()
{
if (!isEmpty()) //if isEmpty function is not true (not empty)
{
front = (front+1) % arraylength; //front increments to next spot
// if it hits the rear, rebounds to zero again
return q_ptr[front]; //returns the front of the array
q_ptr[front].~T(); //delete this spot in memory
}
else
{
cout << "\nQueue is empty!" << endl;
}
void add(T element);
}
template <class T> //Template getBack function
T Queue<T> :: getBack ()
{
if (!isEmpty())
{
return q_ptr[rear]; //returns a copy of the rear
}
else
{
cout << "Queue is empty!" << endl;
}
}
template <class T> //Template getFront function
T Queue<T> :: getFront ()
{
if (!isEmpty()) // if isEmpty function is not true (not empty)
{
return q_ptr[front+1]; // returns a COPY of the front element
}
else
{
cout << "Queue is empty!" << endl;
}
}
template <class T> //Template isEmpty function
int Queue<T> :: isEmpty()
{
if (front == rear)
{
return 1;
}
else
{
return 0;
}
}
template <class T> //Template isFull function
int Queue<T> :: isFull()
{
if ((rear + 1) % arraylength == front) //if next spot is the front, we are full
{
return 1;
}
else
{
return 0;
}
}
template <class T> //Template currSize function
int Queue<T> :: currSize ()
{
return (rear - front + arraylength) % arraylength; //
}
void addTo(Queue <int> &nums)
{
char ans; //Holds input from user
int input;
cout << "Add a number? (Y/N): ";
cin >> ans;
while (ans != 'Y' && ans != 'y' && ans != 'n' && ans != 'N')
{
cout << "\nNot a valid response, try again: ";
cin >> ans;
}
if (ans == 'N' || ans == 'n')
{
return;
}
elseif ((ans == 'Y' || ans == 'y') && !nums.isFull())
{
cout << "\nWhat number?: ";
cin >> input;
nums.add(input);
cout << "\n" << 5-nums.currSize() << " spots left! ";
addTo(nums);
}
elseif (nums.isFull())
{
cout << "\nSorry the queue is already full!";
return;
}
}
int main()
{
Queue <int> nums(5); //declares a queue of ten spot
char ans;
int input;
addTo(nums);
return 0;
}
It has to do with the indexes I think...the front is set to -1, but when it reaches the last spot, the isFull fct takes the last spot, and you try to do the next it does ....
1 2 3 4 5 6 7 8 9 10
{
if ((rear + 1) % arraylength == front) //if next spot is the front, we are full
{
return 1;
}
else
{
return 0;
}
}
(4+1) % 5.... equals 0, so basically the rear is now 0 and the front is -1 still..not matching up, thus not realizing it is full..Playing around with the indexes and can't figure it out.
//This program prints out the reverse order of a queue
#include <iostream>
#include <cctype>
#include <cstdlib>
usingnamespace std;
template <class T> //Template for a queue
class Queue //This is a queue class
{
public:
~ Queue (); // destructor for queue
Queue(int); //default constuctor, passed a int variable for size
T getFront(); //returns a COPY of the front element
void add (T element); // adds element to the queue
T deleteIt (); //deletes an element from the queue
T getBack (); //returns a COPY of the back element
int isEmpty(); //checks if queue is empty
int currSize(); //returns the current size of the queue
int isFull(); //checks if queue is full
void clear();
friendvoid addTo(Queue <int> &);
friendvoid Print(Queue <int> &);
private:
int front; //one ahead of the front element
int rear; //position of the back element
int arraylength; //queue capacity
T *q_ptr; //pointer holds a spot in memory
};
template <class T> //Template constructor
Queue<T> :: ~ Queue() //passed maxsize from initial declaration of stack
{
delete [] q_ptr; //deletes the queue
}
template <class T> //Template constructor
Queue<T> :: Queue(int maxsize) //passed maxsize from initial declaration of queue
{
arraylength = maxsize; //sets private length variable equal to size passed from declaration at main
front = 0; //front and rear are initally -1 because nothing is in queue
rear = 0;
q_ptr = new T[arraylength]; //dynamically allocates memory to where pointer was stationed
}
void Print(Queue <int>& vads)
{
int k(vads.rear);
while (!vads.isEmpty())
{
cout << vads.getBack() << " ";
vads.rear--;
}
vads.rear = k;
cout << "\n";
}
template <class T> //Template add function
void Queue<T> :: add (T element) //passed an element t add to queueu
{
q_ptr [rear++ % arraylength] = element; //add the element to this open spot
}
template <class T> //Template delete function
T Queue<T> :: deleteIt ()
{
rear = front % arraylength;
delete [] q_ptr;
}
template <class T> //Template getBack function
T Queue<T> :: getBack ()
{
return q_ptr[rear-1]; //returns a copy of the rear
}
template <class T> //Template getFront function
T Queue<T> :: getFront ()
{
return q_ptr[front]; // returns a COPY of the front element
}
template <class T> //Template isEmpty function
int Queue<T> :: isEmpty()
{
return (front == rear);
}
template <class T> //Template isFull function
int Queue<T> :: isFull()
{
return (!isEmpty() and (rear % arraylength == front)); //if next spot is the front, we are full
}
template <class T> //Template currSize function
int Queue<T> :: currSize ()
{
return (rear - front); //
}
void addTo(Queue <int> &nums)
{
char ans; //Holds input from user
int input;
if (nums.isFull())
return;
cout <<"\n" << nums.arraylength-nums.currSize() << " spots left! ";
cout << "Add a number? (Y/N): ";
cin >> ans;
cin.ignore();
while ((ans | 0x20) != 'y' and (ans | 0x20) != 'n')
{
cout << "\nNot a valid response, try again: ";
cin >> ans;
cin.ignore();
}
if ( (ans | 0x20) != 'n' )
{
cout << "\nWhat number?: ";
cin >> input;
cin.ignore();
nums.add(input);
addTo(nums);
}
}
int main()
{
int input;
cout << "Enter number of elements: ";
cin.ignore();
cin >> input;
Queue <int> *nums(new Queue <int>(input)); //declares a queue of ten spot
addTo(*nums);
cout << "\nPrinting the list starting at back to front\n";
Print(*nums);
return 0;
}