abbarnes

Queue Function [codeskulptor]

Dec 12th, 2017
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. """
  2. Queue class
  3. """
  4.  
  5. class Queue:
  6.     """
  7.    A simple implementation of a FIFO queue.
  8.    """
  9.  
  10.     def __init__(self):
  11.         """
  12.        Initialize the queue.
  13.        """
  14.         self._items = []
  15.  
  16.     def __len__(self):
  17.         """
  18.        Return the number of items in the queue.
  19.        """
  20.         return len(self._items)
  21.    
  22.     def __iter__(self):
  23.         """
  24.        Create an iterator for the queue.
  25.        """
  26.         for item in self._items:
  27.             yield item
  28.  
  29.     def __str__(self):
  30.         """
  31.        Return a string representation of the queue.
  32.        """
  33.         return str(self._items)
  34.  
  35.     def enqueue(self, item):
  36.         """
  37.        Add item to the queue.
  38.        """        
  39.         self._items.append(item)
  40.  
  41.     def dequeue(self):
  42.         """
  43.        Remove and return the least recently inserted item.
  44.        """
  45.         return self._items.pop(0)
  46.  
  47.     def clear(self):
  48.         """
  49.        Remove all items from the queue.
  50.        """
  51.         self._items = []
Advertisement
Add Comment
Please, Sign In to add comment