我已经厌倦了正常优先级的编码。现在我想在我的代码中添加一个或两个以上的条件。
这是我当前的普通优先级代码:
void queue::addToQueueList(int newPriority, double newFare, int custID)
{
node* newnode= new node;
newnode->priority= newPriority;
newnode->fare = newFare;
newnode->cusID = custID;
newnode->next= NULL;
if (front == NULL || newnode->priority < front->priority)
{
newnode->next = front;
front = newnode;
}
else
{
node* q = front;
node* p;
while (q->next != NULL && q->next->priority <= newnode->priority)
{
q=q->next;
}
newnode->next = q->next;
q->next = newnode;
}
}
如果发现优先级相同,我如何添加条件示例,比较票价。最高票价将是优先考虑。
由于
答案 0 :(得分:0)
如果我已正确理解,您需要以下
#include <utility>
//...
void queue::addToQueueList(int newPriority, double newFare, int custID)
{
node* newnode= new node;
newnode->priority= newPriority;
newnode->fare = newFare;
newnode->cusID = custID;
newnode->next= NULL;
auto p = std::make_pair( newPriority, newFare );
if (front == NULL || p < std::make_pair( front->priority, front->fare ) )
{
newnode->next = front;
front = newnode;
}
else
{
node* q = front;
while (q->next != NULL &&
std::make_pair( q->next->priority, q->next->fare ) <= p)
{
q=q->next;
}
newnode->next = q->next;
q->next = newnode;
}
}
也似乎是声明
node* p;
可能会从该功能中删除。
答案 1 :(得分:0)
这就是我所做的:
void queue::addToQueueList(int newPriority, double newFare, int custID)
{
node* newnode= new node;
newnode->priority= newPriority;
newnode->fare = newFare;
newnode->cusID = custID;
newnode->next= NULL;
if (front == NULL || newnode->priority < front->priority)
{
newnode->next = front;
front = newnode;
}
else
{
node* q = front;
node* p;
while (q->next != NULL && q->next->priority <= newnode->priority)
{
p=q;
q=q->next;
}
bool chk = compare(q,newnode);
if(chk)
{
p->next = newnode;
newnode->next = q;
}
else
{
newnode->next = q->next;
q->next = newnode;
}
}
}
bool queue::compare(node* const& n1, node* const& n2)
{
bool check = false;
if(n1->priority == n2->priority)
{
if(n2->fare > n1->fare)
{
check = true;
}
}
return check;
}