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 Stack: ''' This is a list based implementation of a stack which will keep newest data and drop anything oldest that there is not room for ''' __slots__ = "RB","capacity1", "top1" def __init__(self, capacity): """ initlizes the Stack class :param own: slef object :param capacity: Capacity of the Stack """ # 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.top1 = None def __str__(self): """ Convert Stack in a string format :param own: slef object :return: returns stack in the String format """ if self.capacity1==0 or self.capacity1<0: print("Capacity of Stack is 0 or less than 1. Can't print Stack") return self.RB.print() def push(self, val): """ push operation of the stack. :param own: slef object :return: returns the pushed element """ if self.capacity1==0 or self.capacity1<0: print("Capacity of Stack is 0 or less than 1. Can't use this Stack") return if self.RB.sizeOf() == self.capacity1: print("As Stack is full, Removing top ", self.top1) self.top1=self.RB.insert_keep_new(val) return self.top1 def pop(self): """ pop operation of the stack. :param own: slef object :return: returns the poped element """ if self.capacity1==0 or self.capacity1<0: print("Capacity of Stack is 0 or less than 1. Can't use this Stack") return if self.size()==0 : print("Stack is empty") return self.top1= self.RB.remove_newest() def peek(self): """ peek operation of the stack. :param own: self object :return: returns the element at the top """ if self.capacity1==0 or self.capacity1<0: print("Capacity of Stack is 0 or less than 1. Can't use this Stack") return if self.size()==0 : print("Stack is empty") return return self.top1 def size(self): """ check size of the stack :param own: self object :return: returns the size of the stack """ return self.RB.sizeOf() def capacity(self): """ check capacity of the stack :param own: self object :return: returns the capacity of the stack """ return self.capacity1
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)") list1.insert_keep_new("1")