Build and use your own minimal queue class using an array of type String of fixe
ID: 3861354 • Letter: B
Question
Build and use your own minimal queue class using an array of type String of fixed size to hold the queue.
The array should have the default size of 5.
The array size should be setable via a constructor.
The following methods must be implemented:
enqueue – inserts a value at the rear of the queue
dequeue – returns the value at the front of the queue, removing the value from the queue
isEmpty – returns true if the queue is empty, false otherwise
isFull - – returns true if the queue is full, false otherwise
Tip: In a queue of 2 or more elements, the index of the rear is “larger” than the index of the front – EXCEPT, the queue will need to “wrap around” the array. For example: If the array is 20 elements. the queue front is at index 15 and the queue rear is at index 19, the next enqueue will be at index 20, which doesn't exist. The index must “wrap around” the array, i. e. the next enqueue will be at index 0. The solution is to modulus by the array size whenever increasing an index, either rear of front. For example, to change the index of the rear to the next index,use the formula
rear = (rear + 1) % arraySize;
Explanation / Answer
#include <iostream>
using namespace std;
class CircularQueue
{
private :
string *arr ;//to array of string
int F, R ;
int MAX;
public :
CircularQueue( int size = 5 ) ;//constructor
void enqueue ( string data ) ;//insert in queue
string dequeue( ) ;//delete from queue
bool isEmpty();//check if empty
bool isFull();//check if full
void display( ) ;
} ;
CircularQueue :: CircularQueue( int size )
{
MAX = size ;
arr = new string [ MAX ];//initialise array of string
F = R = -1 ;
}
bool CircularQueue::isFull()
{
if(( R + 1 ) % MAX == F)//its full
return true;
return false;
}
bool CircularQueue::isEmpty()
{
if(F == -1 )//its empty
return true;
return false;
}
void CircularQueue :: enqueue ( string data )
{
if ( isFull() )
{
cout << " Queue is full" ;
return ;
}
R = ( R + 1 ) % MAX;//move to next
arr[R] = data ;//inert data
if ( F == -1 )
F = 0 ;
}
string CircularQueue :: dequeue( )
{
string data ;
if ( isEmpty())//queue is empty
{
cout << " Queue is empty" ;
return NULL ;
}
data = arr[F] ;
arr[F] = "" ;
if ( F == R )
{
F = -1 ;
R = -1 ;
}
else
F = ( F + 1 ) % MAX;
return data ;
}
void CircularQueue :: display( )//display data in queue
{
cout << endl ;
for ( int i = 0 ; i < MAX ; i++ )
cout << arr[i] << " " ;
cout << endl ;
}
int main( )
{
CircularQueue a (5) ;//create object of class
a.enqueue ( "AKSHAY" ) ;
a.enqueue ( "RAJ" ) ;
a.enqueue ( "HEMANT" ) ;
a.enqueue ( "JAYESH" ) ;
a.enqueue ( "PRANAV" ) ;
cout << " Elements in the circular queue: " ;
a.display( ) ;
string i = a.dequeue( ) ;
cout << "data deleted: " << i ;
i = a.dequeue( ) ;
cout << " data deleted: " << i ;
cout << " Elements in the circular queue after deletion: " ;
a.display( ) ;
}
===========================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ circularqueu.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Elements in the circular queue:
AKSHAY RAJ HEMANT JAYESH PRANAV
data deleted: AKSHAY
data deleted: RAJ
Elements in the circular queue after deletion:
HEMANT JAYESH PRANAV
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.