Question: I need help sorting my linked list from smallest number to largest in this priorty queue. C++ Please help PQUEUE::PQUEUE() { front = NULL; back
I need help sorting my linked list from smallest number to largest in this priorty queue. C++ Please help PQUEUE::PQUEUE() { front = NULL; back = NULL; }
void PQUEUE::enq(int num) { if (front == NULL) { front = new node; front->val = num; front->next = NULL; } else { node* tmp = new node; tmp->val = num; tmp->next = front; front = tmp; } }
bool PQUEUE::deq() { node* tmp = front;
if(front == NULL) { return false; } else { front = front->next; delete(tmp); } }
bool PQUEUE::isEmpty() { if(front == NULL) return true; else return false; }
node* PQUEUE::getFront() { return front; }
void PQUEUE::printq() { node* tmp = front; cout << "FRONT: "; while(tmp!=NULL) { cout << tmp->val << "->"; tmp = tmp->next; } cout << "BACK" << endl; }
Output:
FRONT: 9 -> BACK Front of the queue contains 9 Added 3 to the queue FRONT: 3 -> 9 -> BACK Front of the queue contains 3 Added 5 to the queue FRONT: 3 -> 5 -> 9 -> BACK Front of the queue contains 3 Added 1 to the queue FRONT: 1 -> 3 -> 5 -> 9 -> BACK Front of the queue contains 1 Added 4 to the queue FRONT: 1 -> 3 -> 4 -> 5 -> 9 -> BACK Front of the queue contains 1 Removed front of queue FRONT: 3 -> 4 -> 5 -> 9 -> BACK Front of the queue contains 3 Removed front of queue FRONT: 4 -> 5 -> 9 -> BACK Front of the queue contains 4 Removed front of queue FRONT: 5 -> 9 -> BACK Front of the queue contains 5 Queue has data Removed front of queue FRONT: 9 -> BACK Removed front of queue FRONT: BACK Nothing to remove from the queue FRONT: BACK Queue is empty FRONT: BACK
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
