In: Computer Science
Write a template class that implements an extended queue (use singly Linked List) in c++
please create 3 classes
please create 3 classes
please create 3 classes
please create 3 classes
please create 3 classes
Ex:
ExtendedQueue int_queue;
ExtendedQueue double_queue;
ExtendedQueue char_queue;
–Write a program to test this template class.
you have to use inheritance
so you will create 3 classes :
so you will create 3 classes :
so you will create 3 classes :
so you will create 3 classes :
so you will create 3 classes :
so you will create 3 classes :
one for node
one for queue
one for extended queue inherited for queue .
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
//========ExtendedQueue.h=====
template <class T>
class ExtendedQueue{
private:
//node definition
class Node{
public:
T data;
Node *link;
Node(){
link=NULL;
}
Node(T item){
data=item;
link=NULL;
}
};
//node definition ends
Node *FRONT,*REAR;
int total;
public:
ExtendedQueue();
~ExtendedQueue();
void insertQueue(T item);
T deleteQueue();
T front();
T rear();
int size();
void printQueue();
};
//_____constructor_________________
template <class T>
ExtendedQueue <T>:: ExtendedQueue(){
FRONT=REAR=NULL;
total=0;
}
//______destructor________________
template <class T>
ExtendedQueue <T>:: ~ExtendedQueue(){
Node*temp;
while(REAR){
temp=REAR->link;
delete REAR;
REAR=temp;
}
}
//______size of queue________________
template <class T>
int ExtendedQueue<T> :: size(){
return total;
}
//_______front_______________
template <class T>
T ExtendedQueue<T> :: front(){
if(FRONT==NULL){
cout<<"Queue is Empty."<<endl;
return 0;
}
return FRONT->data;
}
//______rear________________
template <class T>
T ExtendedQueue<T> :: rear(){
if(REAR==NULL){
cout<<"Queue is Empty."<<endl;
return 0;
}
return REAR->data;
}
//_______insertQueue_______________
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;
}
//_______deleteQueue_______________
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;
}
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TestMain.cpp
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#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;
}
SCREENSHOT OF PROGRAM AND OUTPUT RAN ON DEV++