Write a program to input group of values into the queue and move the maximum value to front so it will be removed first

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Write a program to input group of values into the queue and move the maximum value to front so it will be removed first

Post by answerhappygod »

Write a program to input group of values into the queue and move the maximum value to front so it will be removed first one .You can use STL queue or the following one programmed in the class.
Write a program to input group of values into the queue and move the maximum value to front so it will be removed first one .
You can use STL queue or the following one programmed in the class.
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
node(int d,node *n=0)
{ data=d; next=n; }
};
class queue
{
node *front;
node *rear;
public:
queue();
bool empty();
void append(int el);
bool serve();
int retrieve();
//....
};
queue::queue()
{
front=rear=0;
}
bool queue::empty()
{
return front==0;
}
void queue::append(int el)
{
if(empty())
front=rear=new node(el);
else
rear=rear->next=new node(el);
}
int queue::retrieve()
{
if(front!=0)
return front->data;
}
bool queue::serve()
{
if(empty())
return false;
if(front==rear)
{
delete front;
front=rear=0;
}
else
{
node *t=front;
front=front->next;
delete t;
}
return true;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply