Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to implement priority queue class that extends singleLink class using java language the node class package cp213; public final class Node { //

I want to implement priority queue class that extends singleLink class using java language

the node class

package cp213; public final class Node { // The T data. private T data = null; // Link to the next Node. private Node next = null; /** * Creates a new node with data and link to next node. Not copy safe as it * accepts a reference to the data rather than a copy of the data. * * @param data * the data to store in the node. * @param next * the next node to link to. */ public Node(final T data, final Node next) { this.data = data; this.next = next; } /** * Returns the node data. Not copy safe as it returns a reference to the * data, not a copy of the data. * * @return The data portion of the node. */ public final T getData() { return this.data; } /** * Returns the next node in the linked structure. * * @return The node that follows this node. */ public final Node getNext() { return this.next; } /** * Links this node to the next node. * * @param next * The new node to link to. */ public final void setNext(final Node next) { this.next = next; } } 

the SingleLinked class

package cp213; import java.lang.reflect.Array; import java.util.Iterator; import java.util.NoSuchElementException; /** * The abstract base class for singly-linked data structures. Provides * attributes and implementations for getSize, peek, isEmpty, toArray, and * iterator methods. The head attribute is the first node in any * singly-linked list. * * @author David Brown * @version 2017-10-23 * @param  * data type for base data structure. */ public abstract class SingleLink implements Iterable { /** * Creates an Iterator for the outer class. An iterator allows a program to * walk through the data in a data structure by using the hasNext and next * methods. Typical code: * * 
 Iterator iter = dataStructureObject.iterator(); while(iter.hasNext()){ T data = iter.next(); ... } * 
* * It also allows the user of the enhanced for loop: * *
 for(T data : dataStructureObject){ ... } * 
* * (Replace T with a concrete class such as String or Integer.) */ private class SingleLinkIterator implements Iterator { // current is initialized to beginning of linked list. private Node current = SingleLink.this.head; /* * (non-Javadoc) * * @see java.util.Iterator#hasNext() */ @Override public boolean hasNext() { return this.current != null; } /* * (non-Javadoc) * * @see java.util.Iterator#next() */ @Override public T next() { T result = null; if (this.current == null) { throw new NoSuchElementException(); } else { result = this.current.getData(); this.current = this.current.getNext(); } return result; } } // First node of linked list. protected Node head = null; // Number of elements currently stored in linked list. protected int size = 0; /** * Returns the current number of elements in the linked structure. * * @return the value of size. */ public final int getSize() { return this.size; } /** * Determines whether the linked data structure is empty or not. * * @return true if data structure is empty, false otherwise. */ public final boolean isEmpty() { return this.head == null; } /* * (non-Javadoc) * * @see java.lang.Iterable#iterator() */ @Override public final Iterator iterator() { return new SingleLinkIterator(); } /** * Returns a reference to the first data of the linked structure without * removing that data from the structure. * * @return The data in the head of the structure. */ public final T peek() { return this.head.getData(); } /** * Returns an array of data from a linked data structure. Not thread safe as * it assumes contents of data structure are not changed by an external * thread during the copy loop. If data elements are added or removed by an * external thread while the data is being copied to the array, then the * declared array size may no longer be valid. * * @return an array of data of type T. Returns null if the data structure is * empty. */ @SuppressWarnings("unchecked") public final T[] toArray() { T[] a = null; if (this.head != null) { // Create an array of data based upon the class of the head data. a = (T[]) Array.newInstance(this.head.getData().getClass(), this.size); final Iterator iter = this.iterator(); for (int i = 0; i < this.size; i++) { a[i] = iter.next(); } } return a; } }
Priority queue class package cp213; /** * A simple linked priority queue structure of Node T objects. * These data objects must be Comparable - i.e. they must provide the compareTo * method. Only the T data contained in the priority queue is * visible through the standard stack methods. Extends the * SingleLink class, which already defines the head node, size, * peek, isEmpty, and iterator. * @version 2017-10-06 * @param * data type for base data structure. */ public class PriorityQueue> extends SingleLink { /** * Adds data to the PriorityQueue. Data is stored in priority order, with * highest priority data at the front of the PriorityQueue, and lowest at * the rear. * * @param data */ public void insert(final T data) { // your code here } /** * Returns the highest priority data in the PriorityQueue. Decrements the * count. * * @return the highest priority data currently in the PriorityQueue. */ public T remove() { // your code here } } 

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 Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago

Question

7. What decisions would you make as the city manager?

Answered: 1 week ago

Question

8. How would you explain your decisions to the city council?

Answered: 1 week ago