How to implement the standard Queue ADT?

Hello.
I have to show how to implement the standard Queue ADT using only a Priority Queue and one additional integer instance variable. Can anyone provide a text description of the process?
secondcupAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
A queue is a special case of a priority queue, where the priority is basically the time of insertion.
0
 
Infinity08Commented:
Is this an assignment ? How far did you get ? What are you unsure about ?
0
 
secondcupAuthor Commented:
I have no idea how to implement the Queue ADT using only a Priority Queue and one additional integer instance variable.  I just know we can implement it by using List...but I do not get what it means by one additional integer..
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
SankoziConnect With a Mentor Commented:
I think this is good solution. You just need this additional integer to determine priority of next element that will be pushed.

class Queue
{
  PQueue pQueue = new PQueue(); 
  int nextPriority = Integer.MAX_VALUE;
 
  void push(T t)
  {
    pQueue.insertWithPriority(t, nextPriority --);
 
    if(nextPriority == Integer.MIN_VALUE)
    {
      reload(); //"restart" queue
    }
  }
 
  T pop()
  {
    p.getNext();
  }
 
  void reload()
  {
    oldPQ = pQueue;
    pQueue = new PQueue();
    nextPriority = Integer.MAX_VALUE;
 
    while(!oldPQ.empty())
    {
      push(oldPQ.pop());
    }
  }
}

Open in new window

0
 
Infinity08Commented:
Sankozi, be careful with posting full code solutions to what might be an assignment - that's not permitted on this site.
0
 
CCSINCOMETRUSTConnect With a Mentor Commented:
You may want to consider implementing a circular queue depending on what it will be used for and if you don't know how much it will hold ahead of time.

Hint: Google "Java Circular Queue" or "C++ Circular Queue" - you should have no problem finding code that you can use.
0
All Courses

From novice to tech pro — start learning today.