Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1319
  • Last Modified:

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?
0
secondcup
Asked:
secondcup
3 Solutions
 
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
 
Infinity08Commented:
A queue is a special case of a priority queue, where the priority is basically the time of insertion.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
SankoziCommented:
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
 
CCSINCOMETRUSTCommented:
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now