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

In C++ please!!! \"A queue is a data structure where items can only be added to

ID: 3737189 • Letter: I

Question

In C++ please!!!

"A queue is a data structure where items can only be added to the end (enqueue) and items can only be removed from the front (dequeue).

A queue can easily be implemented as a linked list.

Create a node structure that contains an integer variable and a pointer to the next node.

Write a Enqueue function:

The parameters will be an integer value to add to the list and a Node pointer that points to the head of the list.

Add the new value to the end of the list.

Write a Dequeue function:

It will only have a Node pointer parameter.

It should return the data element that is in the head.

It then needs to delete the head node and update the new head to the next node.

Write a driver program to test the functions.

Every time you Enqueue and Dequeue print out the queue to make sure its working correctly.

Build a Queue that has 5 items.

Demonstrate your Enqueue and Dequeue functions.

You can build your list by using Enqueue.

Bonus (15 pts): Print the queue out each time by using a recursive print function.

Turn in one netid.cpp file. "

Explanation / Answer

//Queue.h
struct Node
{
int data;
Node *next;
};


class Queue{
  
private:
Node *front,*rear;
  
public:
Queue();
void Enqueue(int data);
void Dequeue();
void displayQueue();
~Queue();
};

//Queue.cpp
#include "Queue.h"
#include <cstddef>
#include <iostream>
using namespace std;

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

void Queue::Enqueue(int data){
  
Node *temp = new Node;
temp->data=data;
temp->next=NULL;

//checking the condition for front
if(front==NULL){
  
front=rear=temp;
}
else{
  
rear->next=temp;
rear=temp;
}
}
//Recursive function
void display(Node *node)
{
if(node)
{
cout<<node->data<<" ";
display(node->next);
}
else
{
cout<<endl;
}
}

void Queue::displayQueue()
{
if(front==NULL){
  
cout<<"Underflow condition."<<endl;
return;
}
  
Node *temp=front;
display(temp);
}

void Queue::Dequeue()
{
if (front==NULL)
{
cout<<"Underflow condition"<<endl;
return;
}

//taking the first node
Node *temp=front;
  
if(front==rear)
front=rear=NULL;
else
front=front->next;
//deleting this node
delete temp;   
}

Queue ::~Queue()
{
while(front!=NULL)
{
Node *temp=front;
front=front->next;
delete temp;
}
}

//main.cpp
/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include "Queue.h"
using namespace std;

int main()
{
Queue Q;
Q.Enqueue(50);
Q.displayQueue();
  
Q.Enqueue(70);
Q.displayQueue();
  
Q.Enqueue(90);
Q.displayQueue();
  
Q.Enqueue(120);
Q.displayQueue();
  
Q.Enqueue(30);
Q.displayQueue();

Q.Dequeue();
Q.displayQueue();
  
Q.Dequeue();
Q.displayQueue();
  
Q.Dequeue();
Q.displayQueue();
  
Q.Dequeue();
Q.displayQueue();
  
Q.Dequeue();
Q.displayQueue();

return 0;
}

//PLEASE PROVIDE FEEDBACK THUMBS UP