In: Computer Science
please answer my question...
Write a template class that implements an extended
queue (use Linked List).
Ex:
ExtendedQueue<int> int_queue;
ExtendedQueue<double> double_queue;
ExtendedQueue<char> char_queue;
–Write a program to test this template class in c++.
the details of the ExtendedQueue:
insert
delete
front
rear
queue size.
the output will be:
Inserting 2
Inserting 4
Inserting 6
Front element is: 2
Removing 2
Inserting 8
Queue size is 3
Removing 4
Removing 6
Removing 8
Queue Is Empty
Source code:
template <class T>
class ExtendedQueue{
private:
class Node{
public:
T data;
Node *link;
Node(){
link=NULL;
}
Node(T item){
data=item;
link=NULL;
}
};
Node *FRONT,*REAR;
int total;
public:
ExtendedQueue();
~ExtendedQueue();
void insertQueue(T item);
T deleteQueue();
T front();
T rear();
int size();
void printQueue();
};
template <class T>
ExtendedQueue <T>:: ExtendedQueue(){
FRONT=REAR=NULL;
total=0;
}
template <class T>
ExtendedQueue <T>:: ~ExtendedQueue(){
Node*temp;
while(REAR){
temp=REAR->link;
delete REAR;
REAR=temp;
}
}
template <class T>
int ExtendedQueue<T> :: size(){
return total;
}
template <class T>
T ExtendedQueue<T> :: front(){
if(FRONT==NULL){
cout<<"Queue is Empty."<<endl;
return 0;
}
return FRONT->data;
}
template <class T>
T ExtendedQueue<T> :: rear(){
if(REAR==NULL){
cout<<"Queue is Empty."<<endl;
return 0;
}
return REAR->data;
}
template <class T>
void ExtendedQueue<T> :: insertQueue(T item){
Node *p=new Node(item);
if(!p){
cout<<"\nMemory is not available"<<endl;
return;
}
if(FRONT==NULL)
FRONT=REAR=p;
else{
FRONT->link=p;
FRONT=p;
}
total++;
cout<<"Inserting "<<item<<endl;
}
template <class T>
T ExtendedQueue <T>:: deleteQueue()
{
if(REAR==NULL){
cout<<"Queue is Empty."<<endl;
return 0;
}
T item=REAR->data;
if(FRONT == REAR)
FRONT=REAR=NULL;
else
REAR=REAR->link;
total--;
cout<<"Removing "<<item<<endl;
return item;
}
#include<iostream>
using namespace std;
#include "ExtendedQueue.h"
int main(){
ExtendedQueue<int> Q;
Q.insertQueue(2);
Q.insertQueue(4);
Q.insertQueue(6);
cout<<"Front element is: "<<Q.front()<<endl;
Q.deleteQueue();
Q.insertQueue(8);
cout<<"Queue size is: "<<Q.size()<<endl;
Q.deleteQueue();
Q.deleteQueue();
Q.deleteQueue();
Q.deleteQueue();
return 0;
}
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================