def insert(self, n): #complexity O(n) x = Unit() if not self.contains(n): self.__elements = Cons(n, self.__elements) self.__iter.copy( self.__elements) # copies the address of the object return x print("Element already exist!", n) return x
def push(self, el): #adds new element in to the list #complexity O(1) newS = Cons(el, self.__elements) self.__elements = newS self.__iter.copy(self.__elements) x = Unit() return x #returns Unit
class ListSet(Set): #IMM List def __init__(self): #complexity O(1) self.__elements = Nil() self.__iter = Imm_L_I() #iterator object for the listset def insert(self, n): #complexity O(n) x = Unit() if not self.contains(n): self.__elements = Cons(n, self.__elements) self.__iter.copy( self.__elements) # copies the address of the object return x print("Element already exist!", n) return x def contains(self, n): #complexity O(n) self.__iter.init() if self.__elements.emptyList(): #checks is the set empty or not return False while self.__iter.hasNext(): m = self.__iter.Next() if n is m: return True self.__iter.init() return False def delete(self, n): #complexity O(n) x = Option() while self.__iter.hasNext(): if n is self.__iter.iter.getHead(): self.__iter.iter.deletFirst( ) #delets the element from the list self.__iter.init() x.setEl(n) return x self.__iter.Next() print("Element is not in the set", n) return x def iterator(self): self.__iter.init() return self.__iter
def enqueue(self, el): #adds a new element into the list #complexity O(1) newQ = Cons(el, self.__fill) self.__fill = newQ x = Unit() return x #returns Unit object
def __init__(self): #complexity O(1) self.__elements = Nil() self.__iter = Imm_L_I() #iterator object for the listset