(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.
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:

(C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...
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 = 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,...
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...
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...
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...
Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue. Note that this does not mean simply displaying the contents of the underlying array. You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. Be careful that one item and no items display properly, no matter where front...
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 (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...
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() ///...
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...