Question

Priority Queue Using A List in Python I would like to pass in a list to...

Priority Queue Using A List in Python

I would like to pass in a list to this class and modify the list using these functions only. Use a list of size 10 and use each of the functions with that list.

class PQ_List(object):

def __init__(self, sampleList):
print ("creates an unsorted list from passed in list")
#
# Returns the list
  
def enQueue(self, item):
print ("adds an item to the PQ")

# Add an item to the PQ
  
def deQueue(self):
print ("removes the highest priority item from the PQ")

# Remove the highest priority item from the PQ
  
def sneakAPeek(self):
print ("returns the highest priority in the PQ, but does not remove it")
#
# Return the highest priority item from the PQ, but don't remove it

def isEmpty(self):
print ("returns T if PQ is empty, F if PQ has entries")
  
# Return a T if PQ is empty, F if PQ is not empty
#   
def size(self):
print ("returns number of items in queue")
# Return the number of items in the queue

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

class PQ_List(object):
   def __init__(self, sampleList):
       self.list = sampleList
       print ("creates an unsorted list from passed in list")
   # Returns the list

   def enQueue(self, item):
       self.list.append(item)
       print ("adds an item to the PQ")
       print (self.list)

   # Add an item to the PQ

   def deQueue(self):
       self.list = self.list[1:]
       print ("removes the highest priority item from the PQ")
       print(self.list)
       # Remove the highest priority item from the PQ

   def sneakAPeek(self):
       print ("returns the highest priority in the PQ, but does not remove it")
       return self.list[0]
       # Return the highest priority item from the PQ, but don't remove it

   def isEmpty(self):
       print ("returns T if PQ is empty, F if PQ has entries")
       if len(self.list) > 0:
           return 'F'
       else:
           return 'T'
       # Return a T if PQ is empty, F if PQ is not empty

   def size(self):
       print ("returns number of items in queue")
       return len(self.list)
       # Return the number of items in the queue

Add a comment
Know the answer?
Add Answer to:
Priority Queue Using A List in Python I would like to pass in a list to...
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
  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Study the "Queue as linked list simple example" posted in this module. Rewrite the code to...

    Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...

  • Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...

    Data Structure Java code Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to the order in which they were enqueued A typical priority queue supports following...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • Problem 3 (20 points) Implement a queue abstract data type using the UnorderedList class. Problems 2-4...

    Problem 3 (20 points) Implement a queue abstract data type using the UnorderedList class. Problems 2-4 ask you to implement three abstract data types using the UnorderedList class described in chapter 3 in your book instead of the book's implementation using the Python list. # Problem 3 class Queue: def _init_(self): raise NotImplementedError def isEmpty(self): raise NotImplementedError def enqueue(self, item): raise NotImplementedError def dequeue(self): raise NotImplementedError def size(self): raise NotImplementedError

  • 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...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • 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...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • Python Expert, I need your help: I need to write a function(rev_int_rec) that take an integet...

    Python Expert, I need your help: I need to write a function(rev_int_rec) that take an integet number(num = 1234567) and retrive the last digit using (lastDigit = num % 10) and add it to a queue. Remove each last digit from num using the % operator Add each last digit to the queue, with each round of recursion reduce num by dividing by 10. Base case is when num is less than 10 # Base case: If num < 10...

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