class Stack: __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 push(self, data): if (data == None): print ("Data has to be sent!!") return -1 else: self.ring_buffer.insert_keep_new(data) def pop(self): self.ring_buffer.remove_newest() def peek(self): return self.ring_buffer.head def capacity(self): self.ring_buffer.capacity()
class Stack: __slots__ = "ring_buffer" def __init__(self, capacity): self.ring_buffer = RingBuffer(capacity) def __str__(self): return self.ring_buffer.__str__() def push(self, value): self.ring_buffer.insert_keep_new(value) def pop(self): self.ring_buffer.remove_newest() def capacity(self): self.ring_buffer.capacity() def peek(self): return self.ring_buffer.head
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()
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()
print("Orignal") list1.print() print("After insert_keep_new(5)") list1.insert_keep_new("5") list1.print() 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)")