With monday.comâ€™s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
#include <iostream>
using namespace std;
#define SIZE 20
#include "queueel.h"
class QueueClass {
QueueElement* queue[SIZE];
int head, tail;
public:
QueueClass();
~QueueClass();
void qu(QueueElement* newElement);
QueueElement* dequ();
int size();
bool isEmpty();
};
QueueClass::QueueClass(): head(0) , tail(19){} // -1 is not correct init list is fine
QueueClass::~QueueClass () {}
void QueueClass::qu(QueueElement* newElement) //ok
{
//if(isEmpty()){
//queue[head]=queue[tail]=newElement;
//}
//else{
tail++;
queue[tail] = newElement;
}
QueueElement* QueueClass::dequ()
{
if (isEmpty()) {return NULL;}
else
{
head++;
return queue[head];
}
}
int QueueClass::size()
{
int counter =0;
int s;
for(s =0; s<SIZE; s++){
if (queue[s] ==queue[!NULL]){
counter++;
}
}
std::cout <<"Queue has "<<counter <<" Elements" <<std::endl;
return counter;
}
bool QueueClass::isEmpty()
{
if(size()==0) {
cout << "Queue is empty\n";
return 1;
}
else return 0;
}
int QueueClass::size()
{
int tmpTail;
int size;
if(tail>=head){
tmpTail=tail;
size=tmpTail;
return size;
}
else{
tmpTail= tail+SIZE;
size=tmpTail;
}
return size;
}
bool QueueClass::isEmpty()
{
if(size()==0) {
cout << "Queue is empty\n";
return 1;
}
else return 0;
}
bool QueueClass::isEmpty()
{
if(size()==0) {
cout << "Queue is empty\n";
return isEmpty;
}
else return isEmpty;
}
#include <iostream>
using namespace std;
#define SIZE 20
#include "queueel.h"
class QueueClass {
QueueElement* queue[SIZE];
int head, tail;
public:
QueueClass();
~QueueClass();
void qu(QueueElement* newElement);
QueueElement* dequ();
int size();
bool isEmpty();
int Increment(int offset);
};
QueueClass::QueueClass(): head(0) , tail(0){} // -1 is not correct init list is fine
QueueClass::~QueueClass () {}
int QueueClass::Increment(int offset) {
offset++;
if (offset >= SIZE) offset = 0;
return offset;
}
void QueueClass::qu(QueueElement* newElement) //ok
{
// Recognize a full queue
if (Increment(tail) == head) {
std::cout << "The queue is full" << std::endl;
// Add to a not-full queue
}
else {
tail = Increment(tail);
queue[tail] = newElement;
}
}
QueueElement* QueueClass::dequ()
{
if (isEmpty()) {return NULL;}
else
{
head++;
return queue[head];
}
}
int QueueClass::size()
{
int tempTail = (tail >= head) ? tail : tail + SIZE;
int size = tempTail - head;
return size;
}
bool QueueClass::isEmpty()
{
bool isEmpty = head == tail;
if (isEmpty) cout << "Queue is empty\n";
return isEmpty;
}
#include <iostream>
using namespace std;
#define SIZE 20
class QueueClass {
int queue[SIZE];
int head, tail;
public:
QueueClass(int, int);
~QueueClass();
void qu(int num);
int dequ();
int size();
bool isEmpty();
int Increment(int offset);
};
QueueClass::~QueueClass () {}
QueueClass::QueueClass (int a, int b) {
head = a;
tail = b;
}
int QueueClass::Increment(int offset) {
offset++;
if (offset > SIZE) offset = 0;
return offset;
}
void QueueClass::qu(int num)
{
// Recognize a full queue
if (Increment(tail) == head) {
std::cout << "The queue is full" << std::endl;
// Add to a not-full queue
}
else {
queue[tail] = num; // Reversed this one
tail = Increment(tail); // ..with this one
}
}
int QueueClass::dequ()
{
if (isEmpty()) {return 0;}
else
{
//head++;
//
//return queue[head];
int tempHead = head;
head = Increment(head);
return queue[tempHead];
}
}
int QueueClass::size()
{
int tempTail = (tail >= head) ? tail : tail + SIZE;
int size = tempTail - head;
return size;
}
bool QueueClass::isEmpty()
{
bool isEmpty = head == tail;
if (isEmpty) cout << "Queue is empty\n";
return isEmpty;
}
int main()
{
QueueClass queue1(0,0);
queue1.isEmpty();
cout <<"size is " <<queue1.size() <<std::endl;
queue1.qu(1);
queue1.isEmpty();
cout <<"size is " <<queue1.size() <<std::endl;
}
This means that the queue is empty when the tail pointer is 1 less than the head pointer, where subtraction is cyclic.
That is, 0 - 1 == 19.
I suggest it is simpler to consider a queue to be empty when the head and tail pointers are equal. This would mean the constructor sets them both to the same value, likely 0.