C++ Project Create a templated queue class with an array based variant. An examp
ID: 3825338 • Letter: C
Question
C++ Project
Create a templated queue class with an array based variant.
An example header file below is given holding elements of type class DataType. This class should be templated
const int ARRAY_MAX = 1000;
class ArrayQueue{
public:
ArrayQueue(); //Instantiate a new Queue object with no valid data
ArrayQueue(int size, const DataType& value); // Instantiate a new Queue object which will hold int size number of elements in total, all of them initialized to be equal to parameter //value
ArrayQueue(const ArrayQueue& other); //Instantiate a new Queue object which will be a seperate copy of the data of the other Queue object which is getting copied
~ArrayQueue(); // Destroys the instance of the Queue object
ArrayQueue& operator=(const ArrayQueue& other_arrayQueue); // Will assign a new value to the calling Queue object, which will be an exact copy of the other_arrayQueue object //passed as a parameter. Returns a reference to the calling object to be used for cascading operator=
DataType& front(); //returns a reference to the front element of the queue (before calling this method ensure the queue isnt empty)
const DataType& front() const;
DataType& back(); //returns a reference to the back element of the queue (before calling this method ensure the queue isnt empty)
const DataType& back() const;
void push(const DataType& value); // Inserts at the back of the Queue an element of the given value
void pop(); //removes from the front element of the queue
int size() const; // returns the size of the current queue
bool empty() const; // will return true if the queue is empty
bool full() const; // will return true if the queue is full
void clear(); // after called the queue will be considered empty
friend std::ostream& operator<<(std::ostream& os, const ArrayQueue& arrayQueue); //will output the complete content of the calling queue object. (Not required to be templated)
private:
DataType m_array[ARRAY_MAX]; //The array that holds the data. Both parameters determined via template parameters
int m_front; // an int with the respective m_array index of the front element of the queue
int m_back; // an int with the respective m_array index of the last element of the queue
int m_size; // keeps track of how many elements are in the queue (should not exceed ARRAY_MAX)
};
/*The implementation will be a wrap around queue, meaning that,
a) pushing an element will move the back by one (m_back=(m_back+1)%ARRAY_MAXSIZE) //and increase the size by one
b) popping an element will move the front by one (m_front=(m_front+1)%ARRAY_MAXSIZE) // and decrease size by one
*/
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert();
int deletee();
void display();
struct queue
{
int no;
struct queue *next;
}
*start=NULL;
typedef struct stack st;
void main()
{
int choice,item;
char ch;
clrscr();
do
{
printf("Enter your option ");
printf("1 . push ");
printf(" 2 . deleation ");
printf("3 . display ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: item=deletee();
printf("Deleted element is = %d ",item);
break;
case 3: display();
break;
default:printf("You entered wrong choice");
}
printf(" Do you wish to continue");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y' || ch=='Y');
}
void insert()
{
struct queue *p,*temp;
printf("%d",start);
temp=start;
p=(struct queue*)malloc(sizeof(struct queue));
printf("Enter number");
scanf("%d",&p->no);
printf("&p=%d ",&p->next);
p->next=NULL;
printf("p=%d ",p);
if(start==NULL)
{
start=p;
printf("s=%d ",start);
}
else
{
while(temp->next!=NULL)
{
printf("t=%d",temp->next);
temp=temp->next;
}
printf("p=%d",p);
temp->next=p;
}
}
int deletee()
{
struct queue *temp;
int value;
if(start==NULL)
{
printf("queue is empty");
getch();
exit(0);
}
else
{
temp=start;
value=temp->no;
start=start->next;
free(temp);
}
return(value);
}
void display()
{
struct queue *temp;
temp=start;
while(temp->next!=NULL)
{
printf(" no= %d",temp->no);
temp=temp->next;
}
printf(" no= %d",temp->no);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.