When print() takes a Queue object as one of its arguments, it implicitly calls _str()_ to get the string representation of the Queue object. _str_(): Returns a string representation of the current Queue object.Queue class overloads the base dunder method _len()_ to allow users also have a length function work on queue objects. ![]() _len_(): Returns the number of items in the queue.get_capacity(): Returns an int representing the capacity of the Queue object.It returns the last element of the internal list if the list is not empty. tail(): Returns the back element of the queue.It returns the first element of the internal list if the list is not empty. head(): Returns the front element of the queue.Then, it returns a boolean based on that comparison. However, if it is a fixed capacity queue, it checks if the length of the list equals to the capacity of the queue. is_full() returns False if the queue is not a fixed capacity queue. is_full(): Returns a boolean value denoting if the queue is filled up.Then it returns a boolean based on that comparison. Since it uses a list to implement the queue, it checks if the length of the list equals to zero. is_empty(): Returns a boolean value denoting if the queue is empty or not.The Queue class has the following method definitions: If the value of capacity is equal to zero, the queue becomes an infinite length queue. _init()_ accepts a parameter, capacity, which tells the queue class the maximum number of items that should be stored by this new instance. When we create a queue object, the initializer, _init()_, initializes self._items to an empty list. The attribute self._items stores the items in the queue, while self._capacity holds the capacity of the queue, or the maximum number of items that can be held by the queue. Return (len(self._items) = self._capacity) ![]() Self._capacity = 0 if capacity < 0 else capacity The following code listing contains the class definition Since we are making use of a list, we will use list index 0 as the front item, and list index -1 as the back, or rear, item of the queue. ![]() We will use a list to implement a fixed capacity queue in python. In this tutorial, we will be creating our very own Queue class using a list to store the queue items. There are several implementations of queues in python, namely, the deque in the collections module, and the Queue and PriorityQueue classes in the queue module.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |