Write a class to implement queues in python.


In [80]:
class Queue(object):
    '''Class to implement queue data structure statically'''
    def __init__(self,size):
        self.q = [0 for i in xrange(size)]
        self.front = -1
        self.rear = -1
        self.size = size
    def enqueue(self,elem):
        if self.rear<self.size-1:
            self.rear+=1
            self.q[self.rear] = elem
        else:
            print 'Overflow'
        if self.front == -1:
            self.front+=1
    def dequeue(self):
        if self.front>-1:
            self.front+=1
            t = self.q[self.front-1]
            if self.front>self.rear:
                self.front = -1
                self.rear = -1
            return t
        else:
            print 'Underflow'        
    def empty(self):
        return not bool(self.rear - self.front)

In [ ]: