示例#1
0
class Queue:

    __slots__ = 'ring_buffer', 'max_capacity'
    """constructor with intial max capacity of 10"""
    def __init__(self, capacity=10):
        self.ring_buffer = RingBuffer(capacity)
        self.max_capacity = capacity

    """to string function uses the internal to string of ring buffer but 
       points to the end"""

    def __str__(self):
        return str(self.ring_buffer) + '<-- end '

    """ enqueue function which inserts values in the queue """

    def enqueue(self, value):
        if value is None:
            raise Exception('cannot enqueue None element in the queue')
        else:
            self.ring_buffer.insert_keep_old(value)

    """ dequeue function which removes values from the queue using FIFO 
    method
        raises an exception if dequeue is done on the empty queue"""

    def dequeue(self):
        if self.ring_buffer.size() is 0:
            raise Exception('dequeue from a empty queue is not allowed')
        else:
            return self.ring_buffer.remove_oldest()

    """ returns the current size of the stack"""

    def size(self):
        return self.ring_buffer.size()

    """ :return the max capacity of the stack """

    def capacity(self):
        return self.ring_buffer.capacity()

    """ only returns the value of the first element in the queue does not 
       removed the element from the queue
       """

    def peek(self):
        if self.size() < 1:
            raise Exception('peek into a empty queue is not allowed')
        else:
            return self.ring_buffer.start_element()