Question

Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends...

Can someone implement this using singly linked list. Please and thank you

public interface PriorityQueueInterface<T extends Comparable<T>>
{
/** * Adds a new entry to this priority queue * @param newEntry An object to be added. */
void add(T newEntry);
/** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */
T remove();
/** Retrieves the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty, null. */
T peek();
/** Detects whether this priority queue is empty. * @return True if the priority queue is empty, or false otherwise. */
boolean isEmpty();
/** Gets the size of this priority queue * @return The number of entries currently in the priority queue. */
int getSize();
/** Removes all entries from this priority queue. */
void clear(); }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Structure

typedef struct node {

    int data;
    int priority; // Low values means higher priority

    struct node* next;

  } Node;

  

Add

Node* newNode(int d, int p)

{

    Node* t= (Node*)malloc(sizeof(Node));

    t->data = d;

    t->priority = p;

    t->next = NULL;

    return t;

}

  void push(Node** head, int d, int p)

{

    Node* start = (*head);

    Node* temp = newNode(d, p);

  

    if ((*head)->priority > p) {     // if head has less priority than new node  

        temp->next = *head;

        // Insert New Node before head

        (*head) = temp;

    }

    else {

        // Traverse to find position to insert new node

        while (start->next != NULL &&

               start->next->priority < p) {

            start = start->next;

        }

        temp->next = start->next;

        start->next = temp;

    }

}

Remove

// Remove the highest priority and return

Node* remove(Node** head)

{

    Node* temp = *head;

    (*head) = (*head)->next;

    return temp;

}

PEEK

int peek(Node** head)

{

    return (*head)->data; }

isEmpty

boolean isEmpty(Node** head){

if (head) return 1;

else return 0;

}

GETSIZE  

int getSize(Node** head){

Node* start = (*head); int c=1;

while (start->next != NULL ) {

            start = start->next; c++;

        } return c;

}

CLEAR

//clear all values

void clear(Node** head){

Node* start = (*head);

while (start->next != NULL ) {

Node *t=start;

            start = start->next; free(t);

        } *head=NULL;

}

Add a comment
Know the answer?
Add Answer to:
Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone implement this using singly linked list. Please and thank you public interface Priori...

    Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends Comparable<T>> { /** * Adds a new entry to this priority queue * @param newEntry An object to be added. */ void add(T newEntry); /** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */ T remove(); /** Retrieves the entry having the...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • public class ArrayHeadTailList<T> implements HeadTailListInterface<T> You are given an interface for a type of list. The...

    public class ArrayHeadTailList<T> implements HeadTailListInterface<T> You are given an interface for a type of list. The list works like this: entries can only be added to and removed from the beginning or end of the list entries can be accessed in any position entries begin at index 0 Write a class that implements this interface. The class uses arrays to implement the list. Your class header and instance data variables will be: public class ArrayHeadTailList<T> implements HeadTailListInterface<T> private T[] listArray;...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ListofQueuesPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ListofQueues in order to fulfill the needs of the interface. You will need to make sub-classes within...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ArrayListPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ArrayList in order to fulfill the needs of the interface. For each constructor and method in your...

  • In this lab, we will implement the Linked Bag. The bag will contain a sequence of...

    In this lab, we will implement the Linked Bag. The bag will contain a sequence of strings. First, you need to design the Node class (Node.java). It will contain an integer data and a reference to thenext Node. The constructor of Node class receives an integer and assigns it to the data field. It also has a default constructor. Data Next Node Then we will design another class named LinkedBag (LinkedBag.java). LinkedBag class has an instance variable called firstNode of...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

  • Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main...

    Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main program named "Project1.java" testing the methods defined for the LinkedBag object. The interface file: BagInterface.java. /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.0*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /**...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT