Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me complete the java methods in the picture. Here is my code so far: class DynamicQueue{ private DynamicNode front, rear; public DynamicQueue(){ front=rear=null;

Please help me complete the java methods in the picture.

Here is my code so far:

class DynamicQueue{ private DynamicNode front, rear; public DynamicQueue(){ front=rear=null; } public boolean empty(){ return (front==null); } }

public void insert(Object x){ DynamicNode p=new DynamicNode(x,null); if(empty()) front=rear=p; else rear.setNext(p); rear=p; } public Object remove(){ if(empty()){ System.out.println(Queue underflow); System.exit(1); } DynamicNode p=front; Object temp=p.getInfo(); front=p.getNext(); if(front==null) rear=null; return temp; }

Here is the image of what needs to be done please:

image text in transcribedimage text in transcribedimage text in transcribed

following this will cause a zero grade for the operation Description: In this assignment, you will implement N dynamic priority queues. Let us first look at a single queue. Your program is given a sequence of objects, for example: a, b, w, a, x, s, . The queue maintains numElements such objects, so that the first (front) is the oldest in the sequence, and the last (rear) is the most recent in the sequence. When the queue is full, the oldest (front) is deleted, so that the new object can be inserted (in rear). If an object that is already in the queue is seen again, it becomes the most recent (it is moved to the rear). This way, the queue is maintained ordered by the object's place in the sequence (last node - most recently referenced first node least recently referenced) The queue wil be implemented using a single-linked list (DynamicNode, rear and front pointers so it should be similar to DynamicQueue from slides "Lecture 5-Queues"). Elements are inserted at the rear, deletion is explained in the following paragraphs. To implement the queue, you CANNOT use: array, or any available classes such as JDK's Queue, LinkedList, or PriorityQueue The N queues are maintained in a 1-D array. Assuming your queue class is DynamicQueue, below is an example for the array of queues DynamicQueuel] queues = new DynamicQueue[n]; Your program must set (hardcode) these: the number of queues (N=length of the array), and the max number of elements in each queue (QUEUESIZE) Your program reads a file that contains a sequence of , for example (see example file "data.txt" on Canvas): b 3 b 0 Page 1 of3 t 3 s 3 c 3 e 0 s 3 When a pair is read, the program must insert the key to the corresponding queue, queues [index]. In the example above, reading the first line a 1> will insert 'a, into gueues[1]. For esample, to insert key' Into a specific queue,queues: 1. Check if the key ('a') already exists in the queue 2. If 'a' does NOT exist in the queue: If the queue is NOT full insert new node with key 'a' in rear of queue. Else //queue is full - that is, it contains QUEUESIZE elements remove front element of the queue; then, insert new node with key 'a' in rear o o 3. Else I'a' already exists in the queue o Move the node with 'a' to the rear (so rear is again the most recently referenced key). o Note: moving the node can be implemented by deleting the node that contains 'a', ther insert new node with key 'a' in rear. Or it can be implemented by modifying the appropriate pointers so that the node with 'a' becomes the rear node. Implementing this part by modifying pointers receives FULL credit, implementing this by using_deletetinsert (or similar) will receive POINTS OFF. For the full credit, you must not swap info on the nodes, but appropriately modify list pointers Output: Your program must print messages for each queue operation, as well as the final contents of the queues please follow the example output below EXACTLY (very small format changes are fine) Example The output shown below is for the file on Canvas data.txt", with number of queues N-4, and queue size (max number of elements in a queue) QUEUESIZE4 Read key a for queue e. Inserting a in rear. Q0: a Read key f for queue 1. Inserting f in rear. Q1: f Read key b for queue 3. Inserting b in rear. Q3: b Read key c for queue 3. Inserting c in rear. Q3: b->c Read key x for queue 1. Inserting x in rear. Q1: f->x Read key b for queue e. Inserting b in rear. Q0 a->b Read key w for queue e. Inserting w in rear. Q0: a->b->W Read key t for queue 3. Inserting t in rear. Q3: b->c->t Read key s for queue 3. Inserting s in rear. Q3: b->c->t->s Read key a for queue 3. Q is full, removing front . Inserting a in rear. Q3 : c->t-s-a Read key z for queue . Inserting z in rear. Q0 : a->b->w->z Read key c for queue 3. Moving c to rear. Q3: t->s->a->c Read key c for queue 3. c is already rear. Q3: t->s->a->c Read key e for queue . Q is full, removing front . Inserting e in rear. Q0 : b->w-z-e Read key s for queue 3. Moving s to rear. Q3: t->a->c->s Final Queues.. Q1: f->x 02: 03: Empty t->a->c->s

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

Students also viewed these Databases questions

Question

=+What is your personal mission statement?

Answered: 1 week ago

Question

16 Evaluate JIJ ew dydxdz

Answered: 1 week ago

Question

Proficiency with Microsoft Word, Excel, PowerPoint

Answered: 1 week ago

Question

Experience with SharePoint and/or Microsoft Project desirable

Answered: 1 week ago