Question

In: Computer Science

Chapter 4 programing project 4.4 The priority queue shown in Listing 4.6 features fast removal of...

Chapter 4 programing project 4.4 The priority queue shown in Listing 4.6 features fast removal of the high-priority item but slow insertion of new items. Write a program with a revised PriorityQ class that has fast O(1) insertion time but slower removal of the high priority item.
Please submit one MS WOrd file with java code and screenshots showing your testing.

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

class PriorityQ {
  private int maxSize;

  private long[] queArray;

  private int nItems;

  public PriorityQ(int s) { // constructor
    maxSize = s;

    queArray = new long[maxSize];

    nItems = 0;
  }

  public void insert(long item) {
    if (nItems < queArray.length) {
      queArray[nItems] = item;

      nItems++;
    }
  }

  public long remove() {
    int indexMin = 0;

    for (int i = 1; i < nItems; i++) {
      if (queArray[i] < queArray[indexMin]) {
        indexMin = i;
      }
    }

    long elem = queArray[indexMin];

    for (int i = indexMin; i < nItems - 1; i++) {
      queArray[i] = queArray[i + 1];
    }

    nItems--;

    return elem;
  }

  public long peekMin() {
    int indexMin = 0;

    for (int i = 1; i < nItems; i++) {
      if (queArray[i] < queArray[indexMin]) {
        indexMin = i;
      }
    }

    return queArray[indexMin];
  }

  public boolean isEmpty() {
    return (nItems == 0);
  }

  public boolean isFull() {
    return (nItems == maxSize);
  }

  public void display() {
    System.out.print("Queue contents: ");
    for (int i = 0; i < nItems; i++) {
      System.out.print(queArray[i]);

      if (i != nItems - 1) {
        System.out.print(", ");
      }
    }

    System.out.println();
  }
}

class PriorityQApp {

  public static void main(String[] args) {
    PriorityQ thePQ = new PriorityQ(5);

    thePQ.insert(30);

    thePQ.insert(50);

    thePQ.insert(10);

    thePQ.insert(40);

    thePQ.insert(20);

    //displaying the queue

    thePQ.display();

    while (!thePQ.isEmpty()) {
      long item = thePQ.remove();

      //displaying the removed element

      System.out.println(item + " is removed");

      //displaying current queue contents

      thePQ.display();
    } // end while
  } // end main()
  // -------------------------------------------------------------

} // end class PriorityQApp

Related Solutions

In Group Project C of Chapter 4, it was shown that the simple pendulum equation: y''(t)...
In Group Project C of Chapter 4, it was shown that the simple pendulum equation: y''(t) + siny(t) = 0 has periodic solutions when the initial displacement and velocity are small. Show that the period of the solution may depend on the initial conditions by using the vectorized Runge–Kutta algorithm with h 0.02 to approximate the solutions to the simple pendulum problem on for the initial conditions: a) y(t) = 0.1 y'(t) = 0 b) y(t) = 0.5 y'(t) =...
Rank the substituents shown below in order of Cahn-Ingold-Prelog priorities (1 = highest priority group, 4...
Rank the substituents shown below in order of Cahn-Ingold-Prelog priorities (1 = highest priority group, 4 = lowest priority group). (a) -CCH (b) -CH=CH2 (c) -CH2CH3 (d) -CH3
Could make a answer fast you can please! Thank you! Learning Objectives: CHAPTER 4 Describe fraud...
Could make a answer fast you can please! Thank you! Learning Objectives: CHAPTER 4 Describe fraud and its impact Explain the objectives and components of internal control Design and use a bank reconciliation Evaluate internal controls over cash receipts and cash payments Construct and use a cash budget Report cash on the balance sheet EXAMPLE OF WHAT I'M LOOKING FOR: One thing I found challenging was the credits and debits concept from chapter two and matching them up, (common stock...
Discuss hypothetical situation described based on information from Chapter 4 and 5. There are different kinds of listing agreements brokers may offer clients.
Discuss hypothetical situation described based on information from Chapter 4 and 5. There are different kinds of listing agreements brokers may offer clients. Subagency is a way of having a cooperating broker assist a client. MLS is a service which allows listed property to be marketed by any participating licensee to maximize exposure of listed property.     1. Chris wants to list his house for sale and wants to use an open listing. What would the advantages be to Chris of...
Chapter 4 of the PMI Practice Standard for Project Configuration Management (2007) describes a basic process...
Chapter 4 of the PMI Practice Standard for Project Configuration Management (2007) describes a basic process for managing the “fundamental project constraints of scope, time, cost, and quality” (p.15). Comment on the similarities and differences between this basic process and the project change management process used in your organization. Is project change management effective in your organization? How can it be improved?
The project is adapted from the Chapter 4 Case Study dealing with North–South Airline In January...
The project is adapted from the Chapter 4 Case Study dealing with North–South Airline In January 2012, Northern Airlines merged with Southeast Airlines to create the fourth largest U.S. carrier. The new North–South Airline inherited both an aging fleet of Boeing 727-300 aircraft and Stephen Ruth. Stephen was a tough former Secretary of the Navy who stepped in as new president and chairman of the board. Stephen’s first concern in creating a financially solid company was maintenance costs. It was...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT