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()
def test_init(self): rb = RingBuffer(4) rs = RingStack(4) rq = RingQueue(4) ls = ListStack(4) lq = ListQueue(4) self.assertIsNotNone(rb.head) self.assertEqual(rb.size(), 0) self.assertEqual(rs.top, -1) self.assertEqual(rs.size, 0) self.assertEqual(ls.top, -1) self.assertEqual(ls.size, 0) self.assertEqual(rq.front, -1) self.assertEqual(rq.back, -1) self.assertEqual(rq.size, 0) self.assertEqual(lq.front, -1) self.assertEqual(lq.back, -1) self.assertEqual(lq.size, 0)
list1.enqueue(5) print("Printing Queue") list1.__str__() print("Adding 6 but it should not be added as Queue is full") list1.enqueue(6) print("Now doing dqueue and printing the queue") list1.dequeue() list1.__str__() print("Adding 6 now") list1.enqueue(6) print("Printing Queue") list1.__str__() print("Checking Queue's size") print(list1.size()) print("Checking Queue's capacity") print(list1.capacity1) print("Peek on Queue returns") print(list1.peek()) print("Reseting the Queue with size 0") list1 = Queue(0) print("Now trying to add 1") list1.enqueue(1) print("Now trying to print Queue") list1.__str__() print("Now trying to dequeqe") list1.dequeue()