Question
C++ Queue Please fix the following code 1. exception handling (out of memory ) 2. build a destructor #include #include using namespace std; struct node
C++ Queue
Please fix the following code
1. exception handling (out of memory )
2. build a destructor
#include
#include
using namespace std;
struct node
{
int info;
struct node *next;
};
class Queue
{
private:
node *rear;
node *front;
public:
Queue();
void enqueue();
void dequeue();
void display();
};
Queue::Queue()
{
rear = NULL;
front = NULL;
}
void Queue::enqueue()
{
int data;
node *temp = new node;
try
{
node *temp = new node;
}
catch (bad_alloc)
{
cout << "Ran out of memory!" << endl;
}
cout<<"Enter the data to enqueue: ";
cin>>data;
temp->info = data;
temp->next = NULL;
if(front == NULL)
{
front = temp;
}
else
{
rear->next = temp;
}
rear = temp;
}
void Queue::dequeue()
{
node *temp = new node;
if(front == NULL)
{
cout<<" Queue is Emtpty ";
}
else
{
temp = front;
front = front->next;
cout<<"The data Dequeued is "<
delete temp;
}
}
int main()
{
Queue queue;
int choice;
while(true)
{
cout<<" 1. Enqueue 2. Dequeue 3. Quit";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: queue.enqueue();
break;
case 2: queue.dequeue();
break;
case 3: exit(0);
break;
default: cout<<" Invalid Input. Try again! ";
break;
}
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started