abbarnes icon

Queue Function [codeskulptor]

abbarnes | PRO | 12/13/17 01:29:37 AM UTC | 0 ⭐ | 242 👁️ | Never ⏰ | []
Python |

1.03 KB

|

None

|

0 👍

/

0 👎

"""
Queue class
"""
 
class Queue:
    """
    A simple implementation of a FIFO queue.
    """
 
    def __init__(self):
        """ 
        Initialize the queue.
        """
        self._items = []
 
    def __len__(self):
        """
        Return the number of items in the queue.
        """
        return len(self._items)
    
    def __iter__(self):
        """
        Create an iterator for the queue.
        """
        for item in self._items:
            yield item
 
    def __str__(self):
        """
        Return a string representation of the queue.
        """
        return str(self._items)
 
    def enqueue(self, item):
        """
        Add item to the queue.
        """        
        self._items.append(item)
 
    def dequeue(self):
        """
        Remove and return the least recently inserted item.
        """
        return self._items.pop(0)
 
    def clear(self):
        """
        Remove all items from the queue.
        """
        self._items = []

Comments