Ask Question

Write an abstract data type for a queue in Java whose elements are generic elements of type T that have an associated integer priority. This queue must have the following methods: enqueue, which takes an object of generic type T and an integer (priority) as parameters; dequeue, which returns the generic element from the queue that has the highest priority (and removes that element); and empty that tells if the queue is empty or not. The queue is not to be maintained in priority order of its elements, so the dequeue operation must always search the whole queue. Also, if there are multiple elements with the same priority, the oldest one should be dequeued first.

+3
Answers (2)
  1. 5 May, 15:57
    0
    See explaination

    Explanation:

    #include

    const int MAX=20;

    class dque

    {

    private:

    int arr[MAX];

    int front, rear;

    public:

    dque ();

    void addqatbeg (char item);

    void addqatend (char item);

    int delqatbeg ();

    int delqatend ();

    void display ();

    int count ();

    };

    dque::dque ()

    {

    front=rear=-1;

    for (int i=0; i
    arr[i]=0;

    }

    void dque::addqatbeg (int item)

    {

    if (front==0&&rear==MAX-1) {cout<<"/nDeque is full"<
    if (front==-1) {front=rear=0; arr[front]=item; return; }

    if (rear!=MAX-1)

    {int c=count (); int k=rear+1;

    for (int i=1; i<=c; i++) {arr[k]==arr[k-1]; k--; }

    arr[k]=item; front=k; rear++;

    }

    else{front--; arr[front]=item; }

    }

    void dque::addqatend (int item)

    {

    if (front==0&&rear==MAX-1) {cout<<"/nDeque is full"<
    if (front==-1) {front=rear=0; arr[rear]=item; return; }

    if (rear==MAX-1) {

    int k=front-1;

    for (int i=front-1; i
    if (k==MAX-1) arr[k]=0;

    else arr[k]=arr[i+1];

    }rear++;

    arr[rear]=item;

    }

    int dque::delqatbeg ()

    {

    if (front==-1) {cout<<"/nDeque is empty"<
    int item=arr[front];

    arr[front]=0;

    if (front==rear) front=rear=-1;

    else

    front++;

    return item;

    }

    int dque::delqatend ()

    {

    if (front==-1) {cout<<"/nDeque is empty"<
    int item=arr[rear];

    arr[rear]=0;

    rear--;

    if (rear==-1) front=-1; return item;

    }

    void dque::display ()

    {

    cout<
    for (int i=0; i
    }

    int dque::count ()

    {

    int c=0;

    for (int i=0; i
    return c; }

    }
  2. 5 May, 16:00
    0
    Check the explanation

    Explanation:

    Abstract Data type (ADT) can be refereed to as a class for objects whose behavior is influenced inline with a set of value and a set of operations.

    #include

    const int MAX=20;

    class dque

    {

    private:

    int arr[MAX];

    int front, rear;

    public:

    dque ();

    void addqatbeg (char item);

    void addqatend (char item);

    int delqatbeg ();

    int delqatend ();

    void display ();

    int count ();

    };

    dque::dque ()

    {

    front=rear=-1;

    for (int i=0; i
    arr[i]=0;

    }

    void dque::addqatbeg (int item)

    {

    if (front==0&&rear==MAX-1) {cout<<"/nDeque is full"<
    if (front==-1) {front=rear=0; arr[front]=item; return; }

    if (rear!=MAX-1)

    {int c=count (); int k=rear+1;

    for (int i=1; i<=c; i++) {arr[k]==arr[k-1]; k--; }

    arr[k]=item; front=k; rear++;

    }

    else{front--; arr[front]=item; }

    }

    void dque::addqatend (int item)

    {

    if (front==0&&rear==MAX-1) {cout<<"/nDeque is full"<
    if (front==-1) {front=rear=0; arr[rear]=item; return; }

    if (rear==MAX-1) {

    int k=front-1;

    for (int i=front-1; i
    if (k==MAX-1) arr[k]=0;

    else arr[k]=arr[i+1];

    }rear++;

    arr[rear]=item;

    }

    int dque::delqatbeg ()

    {

    if (front==-1) {cout<<"/nDeque is empty"<
    int item=arr[front];

    arr[front]=0;

    if (front==rear) front=rear=-1;

    else

    front++;

    return item;

    }

    int dque::delqatend ()

    {

    if (front==-1) {cout<<"/nDeque is empty"<
    int item=arr[rear];

    arr[rear]=0;

    rear--;

    if (rear==-1) front=-1; return item;

    }

    void dque::display ()

    {

    cout<
    for (int i=0; i
    }

    int dque::count ()

    {

    int c=0;

    for (int i=0; i
    return c; }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write an abstract data type for a queue in Java whose elements are generic elements of type T that have an associated integer priority. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers