Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to solve a task of using the queue , using this classes Task : A normal queue is a FIFO data structure; elements

I want to solve a task of using the queue , using this classes

Task :

A normal queue is a FIFO data structure; elements are dequeued from the front of the queue and enqueued at the end of the queue. A priority queueis a queue in which elements are also dequeued from the front of the queue; but they are enqueued according to their associated priorities. A higher priority item is always enqueued before a lower priority element. An element that has the same priority as one or more elements in the queue is enqueued after all the elements with that priority.

(A) Patients in a hospital are assigned priorities to see a doctor. Write a test program, TestPriorityQueuein the CSE202.lab4package so that:

1. For each patient: It prompts for and reads the patient ID and the priority of the patient (an integer between 1 and 5) to see a doctor.

2. Initializes a priority queue with the data that is read. Each patient object is an Associationobject that contains the priority as the key, and the ID as the associated value.

3. Displays the contents of the priority queue using the PrintingVisitorpackage.

Sample input/ output:

postfix expression

value

5 9 + 2 * 6 5 * +

58

6 2 + 5 8 4 / - *

24

6 2 + 5 * 8 4 / -

38

...........................................

public interface Queue extends Container {

public abstract Object getHead();

public abstract void enqueue(Object obj);

public abstract Object dequeue();

}

.....................................

import java.util.NoSuchElementException;

public class QueueAsLinkedList extends AbstractContainer implements Queue { protected MyLinkedList list; public QueueAsLinkedList() { list = new MyLinkedList(); }

public void purge() { list.purge(); count = 0; }

public Object getHead() { if(count == 0) throw new ContainerEmptyException(); else return list.getFirst(); }

public void enqueue(Object obj) { list.append(obj); count++; }

public Object dequeue() { if(count == 0) throw new ContainerEmptyException(); else { Object obj = list.getFirst(); list.extractFirst(); count--; return obj; } }

public Iterator iterator() { return new Iterator() { MyLinkedList.Element position = list.getHead();

public boolean hasNext() { return position != null; }

public Object next() { if(position == null) throw new NoSuchElementException(); else { Object obj = position.getData(); position = position.getNext(); return obj; } } }; } }

...........................................

import java.util.NoSuchElementException; import CSE202.*;

public class PriorityQueueAsLinkedList extends QueueAsLinkedList { Object ob; boolean status=true; int b; public void enqueue(Object obj) { if(list.isEmpty()) { list.prepend(obj); } else{ MyLinkedList.Element x=list.getHead(); while ((x!=null) && (x.getData()> obj)){ //Check it please I have error here x=x.getNext(); } if(x==null){ list.append(obj); } x.insertBefore(obj); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions