Question

In: Computer Science

Write a template class that implements an extended queue. Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue;...

Write a template class that implements an extended queue. 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 classes . one for node one for queue one for extended queue inherited for queue.

Solutions

Expert Solution

//========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;
}

Sample Output:

**Fell free to ask any queries in the comment section. I am happy to help you. if you like our work, please give Thumbs up**


Related Solutions

Write a template class that implements an extended queue (use singly Linked List) in c++ please...
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...
please answer my question... Write a template class that implements an extended queue (use Linked List)....
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
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
Write a Java program that implements a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static stack of integers. Write a template that will create a static stack of any data type. Demonstrate the class with a driver program. Dynamic Stack Template In this chapter you studied DynIntStack, a class that implements a dynamic stack of integers. Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program. Static Queue Template...
Convert the Queue to Generic implementation: public class MyQueueImpl implements MyQueue {    private int capacity;...
Convert the Queue to Generic implementation: public class MyQueueImpl implements MyQueue {    private int capacity;    private int front;    private int rear;    private int[] arr;    public MyQueueImpl(int capacity){        this.capacity = capacity;        this.front = 0;        this.rear = -1;        this.arr = new int[this.capacity];    }    @Override    public boolean enQueue(int v) {                  if(this.rear == this.capacity - 1) {                //Perform shift   ...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S,...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT