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
This code testes RingBuffer, Stack, Queue, ListStack, ListQueue """ from ring_buffer import RingBuffer from queue import Queue from stack import Stack from ListStack import ListStack from ListQueue import ListQueue print("Test case of RingBuffer") print( "Assumed RingBuffer capacity is 5 for testing. All the test cases below are according to size 5" ) sizeOfRB = int(input("Enter size of RingBuffer")) list1 = RingBuffer(sizeOfRB) print("Adding 1") list1.insert_keep_new("1") print("Adding 2") list1.insert_keep_new("2") print("Adding 3") list1.insert_keep_old("3") print("Adding 4") list1.insert_keep_old("4") 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()
class RingStack: ''' A ring buffer based stack implementation. Keeps the new data and drops the old to insert new data ''' def __init__(self, capacity): # store as instance variable self._capacity = capacity # create list of size capacity self.RingBuffer = RingBuffer(self._capacity) # set other instance variable defaults self.top = -1 self.size = 0 def __str__(self): ''' A toString equivalent of java, which returns the string representation of the class ''' # pretty print result = 'RingStack' result += str(self.RingBuffer) return result def insert(self, val): ''' Method to insert data into stack ''' self.top += 1 if self.top == self._capacity: self.top = 0 self.RingBuffer.insert_keep_new(val) # update pointers during insert to keep only newest data if self.size < self._capacity: self.size += 1 def remove(self): ''' Method to remove data from stack ''' # no op if empty if self.size is 0: return # update pointers self.RingBuffer.element[self.top].element = None self.top -= 1 if self.top is -1: self.top = self._capacity - 1 self.size -= 1 def peek(self): ''' Returns top element of the buffer ''' if self.RingBuffer.element[self.top].element is None and self.size > 0: self.top = self._capacity - self.size return self.RingBuffer.element[self.top].element def capacity(self): ''' Returns buffer size ''' return self._capacity