d help creating 2 function that will return the front and rear element of a circular queue in C++ for example: string frontEle(); string rearEle(); these 2 function should return a string of the element at their position and can be store in a variable inside main Here is my code currently: #include #include #define SIZE 5 using namespace std; class Queue {    private:        string items[SIZE];        int front, rear;        int size = 0;    public:        Queue()        {            front = -1;            rear = -1;        }        // Check if the queue is full        bool isFull()        {            if (front == 0 && rear == SIZE - 1)            {                return true;            }            if (front == rear + 1)            {

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need help creating 2 function that will return the front and rear element of a circular queue in C++

for example:

string frontEle();

string rearEle();

these 2 function should return a string of the element at their position and can be store in a variable inside main

Here is my code currently:

#include <iostream>
#include <string>
#define SIZE 5

using namespace std;

class Queue
{
   private:
       string items[SIZE];
       int front, rear;
       int size = 0;

   public:
       Queue()
       {
           front = -1;
           rear = -1;
       }
       // Check if the queue is full
       bool isFull()
       {
           if (front == 0 && rear == SIZE - 1)
           {
               return true;
           }
           if (front == rear + 1)
           {
               return true;
           }
           return false;
       }
       // Check if the queue is empty
       bool isEmpty()
       {
           if (front == -1)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
       // Adding an element
       void enQueue(string element)
       {
           if (isFull())
           {
               cout << "Queue is full" << endl;
           }
           else
           {
               if (front == -1)
               {  
                   front = 0;
               }
               rear = (rear + 1) % SIZE;
               items[rear] = element;
               cout << endl << "Inserted " << element << endl;
               size++;
           }
       }
       //
      
       // Removing an element
       string deQueue()
       {
           string element;
           if (isEmpty())
           {
               cout << "Queue is empty" << endl;
               return 0;
           }
           else
           {
               element = items[front];
               if (front == rear)
               {
                   front = -1;
                   rear = -1;
               }
       // Q has only one element,
       // so we reset the queue after deleting it.
           else
           {
               front = (front + 1) % SIZE;
           }
           size--;
           return element;
           }
   }
   int arraysize()
   {
       return size;
   }
};

int main()
{
   Queue q;

   q.enQueue("a");
   q.enQueue("b");
   q.enQueue("c");
   q.enQueue("d");
   q.enQueue("e");

   // Fails to enqueue because front == 0 && rear == SIZE - 1
   q.enQueue("f");

   string elem = q.deQueue();

   cout << elem << endl;

   q.enQueue("f");
  
   int s = q.arraysize();
   cout << s << endl;

   // Fails to enqueue because front == rear + 1
   q.enQueue("g");
   return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY