Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL).
The program should provide the following functionality:
Test your program using at least the following test cases (considering the queue includes integers):
CODE IN C++ :
#include<iostream>
using namespace std;
class Node { // Node class for linked list
public :
int data;
Node* next;
Node(int data) {
this -> data = data;
next = NULL;
}
};
class Queue // Queue class for queue using linked list
{
Node* head;
Node* tail;
int size; // size of the queue
public :
Queue() {
head = NULL;
tail = NULL;
size = 0;
}
void enqueue(int data) // enqueue data into
queue
{
Node * newNode = new Node(data);
if( head == NULL )
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
size++;
}
void getSize() { // print the number of elements in
the queue.
cout << size << endl;
}
bool isEmpty() { // check if queue is empty
return size==0;
}
int dequeue() { // dequeue data from the
queue
if(head != NULL)
{
int ans = head->data;
Node *temp = head;
delete temp;
head = head->next;
size--;
return ans;
}
}
void front() // print data at the front
{
if(head != NULL)
{
cout << head->data << endl;
}
}
void back() // print data at the back
{
if(tail != NULL)
{
cout << tail->data << endl;
}
}
};
// Driver's code to test the program
int main()
{
Queue q1;
cout << "Is the queue empty ? " << q1.isEmpty()
<< endl; // 1 implies yes, 0 implies no and it will give 1
because initially size is 0
q1.enqueue(7); // 7 pushed into the queue
q1.enqueue(4); // 4 pushed into the queue
cout << "Is the queue empty ? " << q1.isEmpty()
<< endl; // 1 implies yes, 0 implies no and it will give 0
because queue is no more empty
q1.dequeue(); // 7 removed from the queue
cout << "The number of elements in the queue : ";
q1.getSize(); // Answer should be 1 because only 4 is present
q1.enqueue(5); // 5 pushed into the queue
cout << "The number of elements in the queue : ";
q1.getSize(); // Answer should be 2 because 4 and 5 are
present
cout << "Is the queue empty ? " << q1.isEmpty()
<< endl; // 1 implies yes, 0 implies no and it will give 0
because 4 and 5 are present
cout << "Data at the front is : ";
q1.front();// it should print 4
cout << "Data at the back is : ";
q1.back(); // it should print 5;
cout << "The entire queue is : ";
while(!q1.isEmpty()) // perform while it is non-empty
{
cout << q1.dequeue() << " ";
}
}
OUTPUT SNIPPET :

Please give an upvote if you liked my solution.Thank you :)
Write a C++ program to implement a queue using linked lists. You can use the queue...
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...
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...
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...
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,...
C++ I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue. Example input file: enqueue 6 enqueue 8 dequeue enqueue 4...
Help me solve this in C++ 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. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...
C++ queue data sructure ising linked list.
Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to create a queue of user-specified capacity (c) enqueue (d) dequeue (e) is_full (f) is_empty display (h) destructor that deallocates all nodes (i) copy constructor (j) overloaded assignment operator Demonstrate using a main function.
(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...
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() ///...
In c++ language
Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.