can you help me to solve an assignment in data structure using C++ ?
esraa waqfi 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
We won't know until you give more information.
esraa waqfi 0 Newbie Poster
this is the assignment i asked to help me for solving.. :)
Implement the linkedPriorityQueue class as defined below. Note the following:
Each node in linkedPriorityQueue represents a customer. A customer can be regular or
with a high priority. High-priority customers have their high_priority variable set to true
while regular customers have their high_priority variable set to false.
To implement the idea of a priority queue, class linkedPriorityQueue maintains two
queues: one for high-priority customers (implemented using the high_priority_front
and high_priority_rear pointers) and one for regular customers (implemented using the
queue_front and queue_rear pointers).
When adding a new customer, both the customer’s number and his/her priority level is
provided. The customer should be added to the appropriate queue based on his/her priority.
When serving (deleting) a customer, high-priority customers are considered first (i.e., a regular
customer cannot be served while a high-priority customer is waiting).
The front function returns the number of the high-priority customer who has been waiting the
most, while the back function returns the number of the regular customer who has been
waiting the least.
The print function prints the contents of the queue starting with high-priority customers (see
the example below).
The size function takes a boolean argument, high_priority, and returns the number of
high-priority customers in the queue if high_priority is true and the number of regular
customers otherwise.
Do not forget to include proper documentation for your code. 10% of your grade depends on
the documentation. Each function and each variable must be accompanied with a comment
describing its functionality. Do not forget to include your name, ID and section in each file you
submit.
The provided main function is for testing/grading purposes. The only modification allowed on
this function is to comment/uncomment parts of it for debugging purposes.
Your program should compile correctly. Submissions with compilation errors will not be
accepted.
Included below is the expected output of the main function.
#include <iostream>
#include <cassert>
using namespace std;
struct node {
int number;
bool high_priority;
node *link;
};
class linkedPriorityQueue {
private:
node *high_priority_front,
*high_priority_rear,
*queue_front,
*queue_rear;
public:
linkedPriorityQueue();
void addQueue( int number, bool high_priority );
void deleteQueue();
int front();
int back();
bool empty();
bool full();
void print();
int size( bool high_priority);
~linkedPriorityQueue();
};
int main() {
bool t;
int i, j;
linkedPriorityQueue q1;
for( i = 0; i < 9; i++ ) {
t = bool( rand() % 2 );
q1.addQueue( i, t );
cout<<"Customer "<<i<<( t ? " with high-priority " : " " )<<"aded to the queue."<<endl;
}
q1.addQueue( i, true );
cout<<"Customer "<<i<<" with high-priority aded to the queue."<<endl;
cout<<"\nThe number of high-priority customers is "<<q1.size( true )
<<",\nwhile the number of remaining customers is "<<q1.size( false )<<"."<<endl;
cout<<"\nCustomer number "<<q1.front()<<" is at the front of the queue,"
<<"\nwhile customer number "<<q1.back()<<" is at the end of the queue."<<endl;
cout<<endl;
q1.print();
j = q1.size( true ) + q1.size( false ) / 2;
for( i = 0; i < j; i++ )
q1.deleteQueue();
cout<<"\nAfter a few removals, the number of high-priority customers is "<<q1.size( true )
<<",\nwhile the number of remaining customers is "<<q1.size( false )<<"."<<endl;
cout<<"\nCustomer number "<<q1.front()<<" is at the front of the queue,"
<<"\nwhile customer number "<<q1.back()<<" is at the end of the queue."<<endl;
cout<<endl;
q1.print();
return 0;
}
Edited by Ezzaral because: Added code tags. Please use them to format any code that you post.
awie10007 -1 Newbie Poster
Please Copy all U need to study.
esraa waqfi 0 Newbie Poster
this is the assignment i asked to help me for solving..
Implement the linkedPriorityQueue class as defined below. Note the following:
Each node in linkedPriorityQueue represents a customer. A customer can be regular or
with a high priority. High-priority customers have their high_priority variable set to true
while regular customers have their high_priority variable set to false.
To implement the idea of a priority queue, class linkedPriorityQueue maintains two
queues: one for high-priority customers (implemented using the high_priority_front
and high_priority_rear pointers) and one for regular customers (implemented using the
queue_front and queue_rear pointers).
When adding a new customer, both the customer’s number and his/her priority level is
provided. The customer should be added to the appropriate queue based on his/her priority.
When serving (deleting) a customer, high-priority customers are considered first (i.e., a regular
customer cannot be served while a high-priority customer is waiting).
The front function returns the number of the high-priority customer who has been waiting the
most, while the back function returns the number of the regular customer who has been
waiting the least.
The print function prints the contents of the queue starting with high-priority customers (see
the example below).
The size function takes a boolean argument, high_priority, and returns the number of
high-priority customers in the queue if high_priority is true and the number of regular
customers otherwise.
Do not forget to include proper documentation for your code. 10% of your grade depends on
the documentation. Each function and each variable must be accompanied with a comment
describing its functionality. Do not forget to include your name, ID and section in each file you
submit.
The provided main function is for testing/grading purposes. The only modification allowed on
this function is to comment/uncomment parts of it for debugging purposes.
Your program should compile correctly. Submissions with compilation errors will not be
accepted.
Included below is the expected output of the main function.
C++ Syntax (Toggle Plain Text)
#include <iostream>
#include <cassert>
using namespace std;
struct node {
int number;
bool high_priority;
node *link;
};
class linkedPriorityQueue {
private:
node *high_priority_front,
*high_priority_rear,
*queue_front,
*queue_rear;
public:
linkedPriorityQueue();
void addQueue( int number, bool high_priority );
void deleteQueue();
int front();
int back();
bool empty();
bool full();
void print();
int size( bool high_priority);
~linkedPriorityQueue();
};
int main() {
bool t;
int i, j;
linkedPriorityQueue q1;
for( i = 0; i < 9; i++ ) {
t = bool( rand() % 2 );
q1.addQueue( i, t );
cout<<"Customer "<<i<<( t ? " with high-priority " : " " )<<"aded to the queue."<<endl;
}
q1.addQueue( i, true );
cout<<"Customer "<<i<<" with high-priority aded to the queue."<<endl;
cout<<"\nThe number of high-priority customers is "<<q1.size( true )
<<",\nwhile the number of remaining customers is "<<q1.size( false )<<"."<<endl;
cout<<"\nCustomer number "<<q1.front()<<" is at the front of the queue,"
<<"\nwhile customer number "<<q1.back()<<" is at the end of the queue."<<endl;
cout<<endl;
q1.print();
j = q1.size( true ) + q1.size( false ) / 2;
for( i = 0; i < j; i++ )
q1.deleteQueue();
cout<<"\nAfter a few removals, the number of high-priority customers is "<<q1.size( true )
<<",\nwhile the number of remaining customers is "<<q1.size( false )<<"."<<endl;
cout<<"\nCustomer number "<<q1.front()<<" is at the front of the queue,"
<<"\nwhile customer number "<<q1.back()<<" is at the end of the queue."<<endl;
cout<<endl;
q1.print();
return 0;
}
esraa waqfi 0 Newbie Poster
and this is the solution but it need the (full ,empty and the destructor)functions
#include <iostream>
#include <cassert>
using namespace std;
struct node
{
int number;
bool high_priority;
node *link;
};
class linkedPriorityQueue
{
private:
node *high_priority_front,
*high_priority_rear,
*queue_front,
*queue_rear;
public:
linkedPriorityQueue();
void addQueue( int number, bool high_priority );
void deleteQueue();
int front();
int back();
void print();
int size( bool high_priority);
~linkedPriorityQueue();
};
linkedPriorityQueue::linkedPriorityQueue()
{
queue_front=queue_rear=NULL;
high_priority_front=high_priority_rear=NULL;
}
void linkedPriorityQueue::addQueue( int number, bool high_priority )
{
node *p=new node;
p->number =number;
p->high_priority =high_priority;
p->link =NULL;
if(high_priority)
{
if (high_priority_front==NULL)
high_priority_front=high_priority_rear=p;
else
{
high_priority_rear->link=p;
high_priority_rear=p;
}
}
else // regular priority
{
if (queue_front==NULL)
queue_front=queue_rear=p;
else
{
queue_rear->link =p;
queue_rear=p;
}
}
high_priority_rear->link =queue_front;
}
void linkedPriorityQueue::deleteQueue()
{
node *L;
L=high_priority_front;
if (L!=NULL)
if (L==high_priority_rear)
{
high_priority_front =high_priority_rear=NULL;
delete L;
}
else
{
high_priority_front=high_priority_front->link;
delete L;
}
else
{
L=queue_front;
if (L!=NULL)
if (L==queue_rear )
{
queue_front =queue_rear=NULL;
delete L;
}
else
{
queue_front=queue_front->link;
delete L;
}
}
}
int linkedPriorityQueue::front()
{
if(high_priority_front!=NULL)
return high_priority_front->number ;
else
if(queue_front!=NULL)
return queue_front->number;
else
return -1;
}
int linkedPriorityQueue::back()
{
if(queue_rear !=NULL)
return queue_rear->number ;
else
if(high_priority_rear!=NULL)
return high_priority_rear->number;
else
return -1;
}
void linkedPriorityQueue::print()
{
node *ptr;
ptr=high_priority_front;
cout <<"High-Priority Customers in the Queue:"<<endl;
while(ptr!=NULL )
{
cout<<ptr->number <<" ";
ptr=ptr->link;
}
cout<<endl;
ptr=queue_front ;
cout<<"Reming Customers in the Queue:"<<endl;
while(ptr!=NULL)
{
cout<<ptr->number <<" ";
ptr=ptr->link;
}
cout<<endl;
}
int linkedPriorityQueue::size(bool high_priority)
{
node *ptr;
int c=0;
if (high_priority)
{
ptr=high_priority_front;
while(ptr!=NULL )
{
c++;
ptr=ptr->link;
}
}
else
{
ptr=queue_front ;
while(ptr!=NULL)
{
c++;
ptr=ptr->link;
}
}
return c;
}
linkedPriorityQueue::~linkedPriorityQueue()
{
}
Edited by Narue because: Added code tags
esraa waqfi 0 Newbie Poster
and i wanna know if there is another shorter way to solve it ..
Narue 5,707 Bad Cop Team Colleague
One way to shorten things is maintaining the size rather than calculating it. When a node is added or removed, update a data member in the linkedPriorityQueue object. This would also likely be more efficient.
But for such a simple design, you're already pushing the minimum size limit without removing functionality. Why do you think it's not short enough?
esraa waqfi 0 Newbie Poster
mmm ..i understand ..but for size function i forced to use it in this assignment..but do u think that this code is good enough and didnt need to minimization ?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.