class Queue: __slots__ = "ring_buffer" def __init__(self, capacity): if capacity < 0: print("Cannot have a negative list") exit() elif isinstance(capacity, int): self.ring_buffer = RingBuffer(capacity) else: print("Incorrect format of Capacity") def __str__(self): return self.ring_buffer.__str__() def enqueue(self, data): if (data == None): print("Data has to be sent!!") return -1 else: self.ring_buffer.insert_keep_old(data) def dequeue(self): self.ring_buffer.remove_oldest() def peek(self): return self.ring_buffer.head def capacity(self): return self.ring_buffer.capacity()
class Queue: __slots__ = "ring_buffer", "link" def __init__(self, capacity): self.ring_buffer = RingBuffer(capacity) def __str__(self): return self.ring_buffer.__str__() def enqueue(self, value): self.ring_buffer.insert_keep_old(value) def dequeue(self): self.ring_buffer.remove_oldest() def peek(self): return self.ring_buffer.head def capacity(self): return self.ring_buffer.capacity()
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()
print("After insert_keep_new(6)") list1.insert_keep_new("6") list1.print() print("After insert_keep_old(7)") list1.insert_keep_old("7") list1.print() print("After insert_keep_old(8)") list1.insert_keep_old("8") list1.print() print("Capacity is ", list1.capacity()) print("Testing remove_newest()") list1.remove_newest() list1.print() print("Testing remove_oldest()") list1.remove_oldest() list1.print() print("Size of RingBuffer", list1.sizeOf()) print("Capacity of RingBuffer", list1.capacity()) print("Testing find. Searching for 3") list1.Find("3") print("Reseting the RingBuffer to capacity 0") list1 = RingBuffer(0) print("Trying to insert_keep_new(1)") list1.insert_keep_new("1") print("-----------------------") print("Test Case of Stack") print( "Assumed Stack capacity is 5 for testing. All the test cases below are according to size 5"
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()