Modify the implementation of queue data structure such that the array used to implement the queue is
Question:
Modify the implementation of queue data structure such that the array used to
implement the queue is dynamically allocated.
And deallocate the dynamically allocate queue and implement the main.cpp driver file.
/* This is the header file */
#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
using namespace std;
const int MAX_SIZE = 7;
class emptyQueue{};
class fullQueue{};
template
class Queue{
private:
ItemType data [MAX_SIZE];
int front;
int rear;
public:
Queue();
bool isEmpty();
bool isFull();
void enQueue(ItemType);
void deQueue();
ItemType getFront();
};
#include "Queue.tpp"
#endif // QUEUE_H_INCLUDED
/* This the function definition file. */
#include "Queue.h"
#include
using namespace std;
template
Queue
this->front = -1;
this->rear = -1;
}
template
bool Queue
return (this->front == -1 && this->rear == -1);
}
template
bool Queue
return ((this->rear+1)%MAX_SIZE==this->front);
// if(this->rear==MAX_SIZE-1 && this->front==0)
// return true;
// else if(this->rear+1 == this->front)
// return true;
// else
// return false;
}
template
void Queue
if(isFull())
throw fullQueue();
else if(isEmpty()){
this->front++;
this->rear++;
data[this->rear] = item;
}
else
{
this->rear=(this->rear+1)%MAX_SIZE;
// if(this->rear == MAX_SIZE-1)
// this->rear = 0;
// else
// this->rear++;
data[this->rear] = item;
}
}
template
void Queue
if(isEmpty())
throw emptyQueue();
else if(this->front==this->rear){
this->front = -1;
this->rear = -1;
}
else{
this->front=(this->front+1)%MAX_SIZE;
// if(this->front==MAX_SIZE-1)
// this->front = 0;
// else
// this->front++;
}
}
template
ItemType Queue
if(isEmpty())
throw emptyQueue();
return data[this->front];
}
Practical Introduction To Data Structures And Algorithm Analysis Java Edition
ISBN: 9780136609117
1st Edition
Authors: Clifford A. Shaffer