Question

In: Computer Science

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

Solutions

Expert Solution

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!
===========================================================================

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...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private...
Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */    private Node head;    private Node tail;       public class Node { // declare member variables (data and next)    Integer data;    Node next; // finish these constructors    public Node(int data, Node next) {               this.data=data;        this.next=next;    }...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT