Question

(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 array. You may use a vector

(rather than a primitive array) as the underlying array structure.

template <typename Object>

class Queue {

private:

       vector<Object> que;

       int front, rear,capacity,size;

public:

       Queue(int maxsize);

       void enqueue(const Object & x);

       const Object& dequeue();

       bool empty() const;

       bool full() const;;

};

Write an appropriate main to test the functionality of queue class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

#include<iostream>

#include<vector>

using namespace std;

template <typename T>

class Queue

{

private:

vector<T> que;

int front, rear, capacity, size;

public:

//Constructor

Queue(int maxSize)

{

this->front = this->rear = -1;

this->size = maxSize;

this->que.resize(maxSize);

}

//enqueue function

void enqueue(const T x)

{

if (this->full())

{

cout << "queue is full";

}

else

{

this->rear = (this->rear + 1) % this->size;

this->que[this->rear] = x;

if (this->front == -1)

this->front = this->rear;

}

}

//dequeue function

const T dequeue()

{

if (this->empty())

{

cout << "Queue is Empty";

return 0;

}

else

{

T& data = this->que[this->front];

if (this->front == this->rear)

this->front = this->rear = -1;

else

this->front = (this->front + 1) % this->size;

return data;

}

}

bool empty()

{

return (this->front == -1);

}

bool full()

{

return ((this->rear + 1) % this->size == this->front);

}

void display()

{

int front_pos = this->front, rear_pos = this->rear;

if (front == -1)

{

cout << "Queue is empty\n";

return;

}

cout << "Queue elements :\n";

if (front_pos <= rear_pos)

{

while (front_pos <= rear_pos)

{

cout << this->que[front_pos] << " ";

front_pos++;

}

}

else

{

while (front_pos <= this->size - 1)

{

cout << this->que[front_pos] << " ";

front_pos++;

}

front_pos = 0;

while (front_pos <= rear_pos)

{

cout << this->que[front_pos] << " ";

front_pos++;

}

}

cout << "\n";

cout << endl;

}

};

//main function to test code

int main()

{

int choice, item;

Queue<int> q(10);

do

{

cout << "1.Enqueue\n";

cout << "2.Dequeue\n";

cout << "3.Display\n";

cout << "4.Quit\n";

cout << "Enter your choice : ";

cin >> choice;

switch (choice)

{

case 1:

cout << "Enter the element to insert in the queue : ";

cin >> item;

q.enqueue(item);

break;

case 2:

q.dequeue();

break;

case 3:

q.display();

break;

case 4:

break;

default:

cout << "Wrong choice\n";

}/*End of switch*/

} while (choice != 4);

return 0;

}

Output:

Add a comment
Know the answer?
Add Answer to:
(C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...
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
  • Balment a la medicul Quoc that speciala a circular que has the following private data members...

    Balment a la medicul Quoc that speciala a circular que has the following private data members and public member functions. The circular que simplemented using an atay. Your submission should consist of four separate files the three source code file header file.implementation file and main program or routine and the program otput. When making the submission, please do not submit it as a file Private data members: int the tray int current size oprema prinete the first met of the...

  • 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...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • 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...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • C++ / Visual Studio Implement the member function below that returns true if the given value...

    C++ / Visual Studio Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data. template< typename Value, class Container > bool member( const Value& val, const Container& cont ); just implement this function in the code below #include <iostream> #include <array> #include <deque> #include <list> #include <set> #include <string> #include <vector> using namespace std; #define...

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
ADVERTISEMENT