Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to implement a queue using linked lists. The program should

ID: 3834595 • Letter: W

Question

Write a C++ program to implement a queue using linked lists. The program should provide the following functionality:

Enqueue data into queue

Dequeue data from queue

Print data at the front

Print data at the back

Print the entire queue

Check if the queue is empty

Print the number of elements in the queue

Test your program using at least the following test cases (considering the queue includes integers):

Check if the queue is empty: Option F.

Enqueue 7 into the queue: Option A.

Enqueue 4 into the queue: Option A.

Check if the queue is empty: Option F.

Dequeue data form the queue.

Print the number of elements in the queue: Option G.

Enqueue 5 into the queue: Option A.

Print the number of elements in the queue: Option G.

Check if the queue is empty: Option F.

Print data at the front: Option C.

Print data at the back: Option D.

Print entire queue: Option E.

Explanation / Answer

Hi,

Here is the program which does what you want :

#include<iostream>

#include<cstdlib>

using namespace std;

struct node{
int info;
struct node *next;
};

class Queue{
private:
node *rear;
node *front;
int count;
public:
Queue();
void enqueue();
void dequeue();
void display();
void isEmpty();
void dataBack();
void dataFront();
void noOfElements();

};

Queue::Queue(){
rear = NULL;
front = NULL;
count =0;
}

void Queue::dataFront()
{
if (front == NULL )
{
cout<<"No data in front as queue is empty ";
}
else
{
cout<<front->info<<" ";
}
}

void Queue::dataBack()
{
if (rear == NULL )
{
cout<<"No data in rear as queue is empty ";
}
else
{
cout<<rear->info<<" ";
}
}

void Queue::noOfElements()
{
if ( count > 0 )
{
cout<<"There are "<<count<<" elements in the queue"<<" ";
}
else
{
cout<<"There are no elements in the queue ";
}
}

void Queue::enqueue()
{
int data;
node *temp = new node;
cout<<"Enter the data to enqueue: ";
cin>>data;
temp->info = data;
temp->next = NULL;
if(front == NULL && rear == NULL){
front = rear = temp;
}
rear->next = temp;
rear = temp;
count++;
}

void Queue::dequeue(){
node *temp = new node;
if(front == NULL){
cout<<" Queue is Emtpty ";
}else{
cout<<"Data is "<< front->info<< " ";
if (front == rear) {
front = rear = NULL;
}
else
{
front = front->next;
}
  
}
count--;

}

void Queue::isEmpty() {
if(front == NULL){
cout<<" Queue is Emtpty ";
}else{
cout<<" Queue is not empty ";
}
}

void Queue::display(){
node *p = new node;
p = front;
if(front == NULL){
cout<<" Nothing to Display ";
}else{
while(p!=NULL){
cout<<endl<<p->info;
p = p->next;
}
}
}

int main(){
Queue queue;
char choice;
while(true){
cout<<" A.Enqueue B. Dequeue C. Print Front data D. Print Back data E. Print Entire queue F. Check if queue is empty G. No of elements in queue H. Quit;";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice){
case 'A':
queue.enqueue();
break;
case 'B':
queue.dequeue();
break;
case 'C':
queue.dataFront();;
break;
case 'D':
queue.dataBack();;
break;
case 'E':
queue.display();
break;
case 'F':
queue.isEmpty();
break;
case 'G':
queue.noOfElements();;
break;
case 'H':
exit(0);
break;
default:
cout<<" Invalid Input. Try again! ";
break;
}
}
return 0;
}

Thanks

Nikhil

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote