Please add a count and De-queue which will returns a number from the front of th
ID: 3749427 • Letter: P
Question
Please add a count and De-queue which will returns a number from the front of the queue
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
};
class Queue{
private:
node * rear;
node * front;
public:
Queue ();
void EnQueue ();
void dequeue ();
void Show ();
};
Queue::Queue ()
{
rear = 0;
front = 0;
}
void
Queue::EnQueue ()
{
int data;
node * temp = new node;
cout << "Enter datas to EnQueue: ";
cin >> data;
temp->info = data;
temp->next = 0;
if (front == 0)
{
front = temp;
}
else{
rear->next = temp;
}
rear = temp;
}
void
Queue::dequeue (){
node * temp = new node;
if (front == 0){
cout << endl<<endl<<"!!!!!!!!!!!!!!!!!!!!!"<<"its empty"<<endl;
}
else{
temp = front;
front = front->next;
cout <<endl<< "***********"<<endl <<"data De-queued is: " << temp->info<<endl<<"*****************"<<endl;
delete temp;
}
}
void
Queue::Show (){
node * p = new node;
p = front;
if (front == 0)
{
cout << "Npthing to show "<<endl;
}
else{
while (p != 0)
{
cout << endl << p->info;
p = p->next;
}
}
}
int
main ()
{
Queue queue;
int choice;
while (true)
{
cout << " 1.EnQueue 2. Dequeue 3. Show 4.Quit";
cout << " enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
queue.EnQueue ();
break;
case 2:
queue.dequeue ();
break;
case 3:
queue.Show ();
break;
case 4:
exit (0);
break;
default:
cout << " Invalid, type again ";
break;
}
}
return 0;
}
Explanation / Answer
//You can find my changes with keyword CHEGGEA
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
};
class Queue {
private:
node * rear;
node * front;
public:
Queue();
void EnQueue();
void dequeue();
void Show();
//adding count function which returns number of items in queue , CHEGGEA
int count();
int De_queue();
};
//adding count function which returns number of items in queue , CHEGGEA
int Queue::count()
{
node *cur = front;
int cnt = 0;
if (front == NULL)
{
cout << "Queue is empty ";
return -1;
}
while (cur != NULL)
{
cnt++;
cur = cur->next;
}
return cnt;
}
int Queue::De_queue()
{
//return number from front of the queue
if (front == NULL)
{
cout << "Queue is empty ";
return -1;
}
return front->info;
}
//end of definition, CHEGGEA
Queue::Queue()
{
rear = 0;
front = 0;
}
void Queue::EnQueue()
{
int data;
node * temp = new node;
cout << "Enter datas to EnQueue: ";
cin >> data;
temp->info = data;
temp->next = 0;
if (front == 0)
{
front = temp;
}
else {
rear->next = temp;
}
rear = temp;
}
void Queue::dequeue() {
node * temp = new node;
if (front == 0) {
cout << endl << endl << "!!!!!!!!!!!!!!!!!!!!!" << "its empty" << endl;
}
else {
temp = front;
front = front->next;
cout << endl << "***********" << endl << "data De-queued is: " << temp->info << endl << "*****************" << endl;
delete temp;
}
}
void Queue::Show() {
node * p = new node;
p = front;
if (front == 0)
{
cout << "Npthing to show " << endl;
}
else {
while (p != 0)
{
cout << endl << p->info;
p = p->next;
}
}
}
int
main()
{
Queue queue;
int choice;
while (true)
{
//adding count ,front of the queue options to menu , CHEGG EA
cout << " 1.EnQueue 2. Dequeue 3. Show 4. Count_nodes 5. Front_of_the_queue 6.Quit";
cout << " enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
queue.EnQueue();
break;
case 2:
queue.dequeue();
break;
case 3:
queue.Show();
break;
//case added by CHEGGEA
case 4:
if(queue.count()!=-1)
cout << "Count of all nodes in queue: " << queue.count() << endl;
break;
//case added by CHEGGEA
case 5:
cout << "Front of the queue (-1 in the case of empty queue) is : " << queue.De_queue() << endl;
break;
case 6:
exit(0);
break;
default:
cout << " Invalid, type again ";
break;
}
}
return 0;
}
/*output
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 3
Npthing to show
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 4
Queue is empty
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 5
Queue is empty
Front of the queue (-1 in the case of empty queue) is : -1
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 1
Enter datas to EnQueue: 10
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 1
Enter datas to EnQueue: 4
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 1
Enter datas to EnQueue: 15
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 1
Enter datas to EnQueue: 20
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 1
Enter datas to EnQueue: 22
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 3
10
4
15
20
22
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 4
Count of all nodes in queue: 5
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 5
Front of the queue (-1 in the case of empty queue) is : 10
1.EnQueue
2. Dequeue
3. Show
4. Count_nodes
5. Front_of_the_queue
6.Quit
enter choice: 6
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.