Skip to main content

Posts

Showing posts from July, 2018

Queues implementation using Python

Queue : A queue is a collection of objects in which elements are added/deleted based on the principle of FIFO (First In First Out) Queue ADT :- Q.enqueue(e) : Add an element to the end Q.dequeue()  : Remove and return the first element, an error will be reported if empty Q._front()   : Returns the first element without removing it, an error will be reported if empty Q.isEmpty()  : Returns True if the Queue is empty Q.isFull()   : Returns True if the Queue is full Q.size()     : Returns the length of Queue Implementation : Queue will be implemented using Lists (circular arrays), where we assume that the Queue is of fixed size N Test Code

Stack Abstract Data Type with implementation in Python as Arrays

Stack is a collection of objects where the elements are inserted and removed based on the principle of LIFO (Last In First Out). Common applications for Stack ADT is in text editors for managing undo operation. Github Link : stacks.py & test_stacks.py Implementation using Python # File : stacks.py # Stack ADT implementation using Lists as underlying data structures # S.push(e) : Add element 'e' to the top of Stack S # S.pop() : Remove element from the top of Stack S and return the removed element, error if empty # S.top() : Return the reference of the top element of S without removing it, error if empty # S.isEmpty() : Return True if stack is empty # len(S) : Return number of elements in Stack S class EmptyStackException (Exception): pass class StacksList (object): def __init__(self): self._data = [] def push(self,e): self._data.append(e) def pop(self): if self.isEmpty():