Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a Java program that manages a linked queue where the items in the queue ha

ID: 3760527 • Letter: W

Question

write a Java program that manages a linked queue where the items in the queue have a priority. The head of the queue points to the first entry on the queue which is the entry with the highest priority. The first entry points to the 2nd node (or is null if the list is empty) which is the entry with the 2nd highest priority, etc. The last entry will point to null. Each node on the queue will have two parts, the pointer to the next entry on the queue (or null) and a pointer to the data item associated to that node entry (which should never be null). The data items are very simple and will consist of two parts, a priority which is a number from 0 to 100 inclusive with 100 being the highest and 0 being the lowest priority and a string.

You will need to create the following methods:

enqueue(newEntry): boolean Adds an entry to the proper place in the queue given its priority. Returns false if cannot addan entry and true if the addition was successful.

dequeue(): T Removes and returns the entry with the highest priority (the 1st entry on the queue).

isEmpty(): boolean Detects whether the queue is empty (true if yes, otherwise false).

clear(): void Removes all entries from the queue.

length(): int Returns the length of the queue.

returnArray(): array T Returns an array of all the entries on the queue in priority order (not nodes, just the data).

main(): void This will probably be the hardest method. It needs to have code that demonstrates that all of the other methods work.

Notes:

Do not use the Java built in list methods, create your own.

Assume that all initialization has been done.

Assume that a call to dequeue will always find something in the queue.

No restriction is placed on the length of the queue.

Explanation / Answer

I try to implement C++.


#include <iostream>
#include <list>
using namespace std;
int main ()
{
list<int> list;
int sum (0);

for (int i=1;i<=10;++i) list.push_back(i);

while (!list.empty())
{
     sum += list.front();
     list.pop_front();
}

cout << "total: " << sum << ' ';

return 0;
}