Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 "<info;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions