Question

Page 3 of 5 based queue - coding question rray-based fine an array-bag ray-based queue template class ArrQueue that uses a on please write them in parts and not the whole code
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* here in this program as per the question variable back is taken as rear named variable */

/* enqueue member function */

template <class X>
void ArrQueue <X>::enqueue(X item)

{
   if(front==-1)
           {
               front = 0;
           }
          
       rear=(rear+1)%sz;
       items[rear]=item;
cout<<endl<<"Inserted : "<<item<<endl;

}

/* dequeue member function */

template <class X>
X ArrQueue <X>::dequeue()
{
   X element;

element=items[front];
if(front==rear){
front=-1;
rear=-1;
}
else
           {
front=(front+1)%sz;
}
return(element);

}

/* peek Front memeber function */

template <class X>
X ArrQueue <X>::peekFront()
{

   if(front>=0)
   cout<<"peek front element is :"<< items[front];
   else
   cout<<"No peek element in queue";
}

/* isEmpty member function */

template <class X>
void ArrQueue <X>::isEmpty()
{
   if(front==-1)
   {
       cout<<"Circular Queue is empty";
   }
   else
   {
           cout<<"Circular Queue is not empty";
   }
}

/* ArrQueue class with template */

template <class X>
class ArrQueue
{
   X *items; // dynamically allocated array //      
   int capacity;  
   int front;      
   int rear;      
   int count;      
public:
   ArrQueue (int size=sz);   // constructor //  
   X dequeue(); // delete element//
   void enqueue(X x); // insert element //
   X peekFront(); // front element //
   int size(); // count elements //
   void isEmpty(); // check empty or not //
   void display(); // display queue //
};

/* full program */

/* here for better under standing the full program is given, which will help to co relate the above functions and other functions with rest of the program */

/* here in this program as per the question variable back is taken as rear named variable */

#include <iostream>
#include <cstdlib>
using namespace std;

// max size of queue is 20//
#define sz 20

// template class representation of circular queue //
template <class X>
class ArrQueue
{
   X *items; // dynamically allocated array //      
   int capacity;  
   int front;      
   int rear; // as per the question back named variable //
   int count;      
public:
   ArrQueue (int size=sz);   // constructor //  
   X dequeue(); // delete element//
   void enqueue(X x); // insert element //
   X peekFront(); // front element //
   int size(); // count elements //
   void isEmpty(); // check empty or not //
   void display(); // display queue //
};


template <class X>
ArrQueue <X>::ArrQueue(int size)
{
   items = new X[size]; // dynamically allocate in circular array//
   capacity = size;
   front = -1;
   rear = -1;
   count = 0;
}

// return type is X which is integer //
template <class X>
X ArrQueue <X>::dequeue()
{
   X element;

element=items[front];
if(front==rear){
front=-1;
rear=-1;
}
else
           {
front=(front+1)%sz;
}
return(element);

}


template <class X>
void ArrQueue <X>::enqueue(X item)

{

if(front==-1)
           {
               front = 0;
           }
          
      
          rear=(rear+1)%sz;
       items[rear]=item;
cout<<endl<<"Inserted : "<<item<<endl;

  
  
}

template <class X>
void ArrQueue <X>::display()
{
  
   int index;
  
  
if(front==-1)
{
   cout<<"No element is found to be displayed";
           }
           else
           {
          
           cout<<endl<<"Items in the Circular Queue are: \n ";
for(index=front;index!=rear;index=(index+1)%sz)
{
       cout<<items[index]<<" ";
       }

cout<<items[index];
}
  
}


template <class X>
X ArrQueue <X>::peekFront()
{

   if(front>=0)
   cout<<"peek front element is :"<< items[front];
   else
   cout<<"No peek element in queue";
}


template <class X>
int ArrQueue <X>::size()
{
   return count;
}


template <class X>
void ArrQueue <X>::isEmpty()
{
   if(front==-1)
   {
       cout<<"Circular Queue is empty";
   }
   else
   {
           cout<<"Circular Queue is not empty";
   }
}

int main()
{
   // here template X is int //
   ArrQueue <int> q;

   int ch,val,p;
   char ch1;
   do
   {
   cout<<"1.Enque"<<endl;  
   cout<<"2.Deque"<<endl;
   cout<<"3.isEmpty"<<endl;  
   cout<<"4.Peek Front"<<endl;  
   cout<<"5.Display"<<endl;  
   cout<<"Enter the choice:";
   cin>>ch;
   switch(ch)
   {
       case 1:
       cout<<"Enter the item :";
       cin>>val;  
       q.enqueue(val);
       break;
       case 2:
       p=q.dequeue();
       cout<<"Removed element is:"<<p;
       break;
       case 3:
       q.isEmpty();
       break;
       case 4:
       q.peekFront();
       break;
       case 5:
       q.display();
       break;
          
       default: cout<<endl<<"Wrong option[ Enter from 1,2,3,4,5]"<<endl;
   }
   cout<<endl<<"Want to continue[Y/y]:";
   cin>>ch1;
} while(ch1=='Y' || ch1=='y');
  
}

SCREEN SHOT

OUTPUT

Add a comment
Know the answer?
Add Answer to:
please write them in parts and not the whole code Page 3 of 5 based queue...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front =...

    Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front = 0, back = 0; void enqueue(int x) { data[back] = x; back = (back + 1) % 6; } void dequeue() { front = (front + 1) % 6; } and we perform the following series of queue operations: enqueue(1); dequeue(); enqueue(2); dequeue(); enqueue(7); enqueue(3); enqueue(5); dequeue(); dequeue(); enqueue(4); enqueue(6); Write the state of the queue array after each operation, and at the end,...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • Write a C++ program to implement Queue ADT using singly linked structure. The program includes the...

    Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header file QueueLinked.h. // QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include <iostream> using namespace std; template <typename T> class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void...

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • Study the "Queue as linked list simple example" posted in this module. Rewrite the code to...

    Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT