예제 #1
0
class Queue:
    '''
    This is a list based implementation of a Queue
    '''

    __slots__ = "RB", "capacity1", "front", "back"

    def __init__(self, capacity):
        """
               initlizes the Queue class
               :param own: slef object
               :param capacity: Capacity of the Queue
         """
        # create list of size capacity
        #self.list_stack = [None] * capacity
        self.RB = RingBuffer(capacity)
        # store as instance variable
        self.capacity1 = capacity
        # set other instance variable defaults
        self.front = None
        self.back = None
        #self.sizeIs = 0

    def __str__(self):
        """
        Convert Queue in a string format
        :param own: slef object
        :return: returns Queue in the String format
        """
        if self.capacity1 == 0 or self.capacity1 < 0:
            print(
                "Capacity of Queue is 0 or less than 1. Can't print this Queue"
            )
            return
        if self.size() == 0:
            print("Queue is empty")
            return
        self.RB.print()

    def is_empty(self):
        """
        check if empty
        :param own: slef object
        :return: returns the true if Queue is empty or false if not
        """
        return self.front == None

    def enqueue(self, val):
        """
        Addes the element to the Queue
        :param own: slef object
        :param val: Element to be added
        """
        if self.capacity1 == 0 or self.capacity1 < 0:
            print(
                "Capacity of Queue is 0 or less than 1. Can't use this Queue")
            return
        #self.RB.insert_keep_new(val)
        if self.is_empty() == True:
            self.front = self.RB.insert_keep_old(val)
            self.back = self.front
            return
        elif self.RB.sizeOf() == self.capacity1:
            print("Queue is full. Can't add any value")
            return
        else:
            self.back = self.RB.insert_keep_old(val)

    def dequeue(self):
        """
        Removes the element from the Queue
        :param own: slef object
        """
        if self.capacity1 == 0 or self.capacity1 < 0:
            print(
                "Capacity of Queue is 0 or less than 1. Can't use this Queue")
            return
        #print("Is empty in deque", self.is_empty())
        if self.is_empty() == True:
            print("Queue is empty")
            return
        elif self.RB.sizeOf() == 1:
            self.front = None
            self.back = None
            self.RB.clear()
            return
        self.front = self.RB.remove_oldest()

    def peek(self):
        """
        Peek operation for the Queue
        :param own: slef object
        :Return : the first element
        """
        if self.is_empty() == True:
            print("Queue is empty")
            return
        return self.front

    def capacity(self):
        """
        Checks the capacity of the Queue
        :param own: slef object
        :Return : the capacity of the Queue
        """
        return self.capacity1

    def size(self):
        """
        Checks the size of the Queue
        :param own: slef object
        :Return : the size of the Queue
        """
        return self.RB.sizeOf()