class Set: def __init__(self): self.my_set = MyList() def length(self): return self.my_set.__len__() def contains(self, item): return self.my_set.__contains__(item) def add(self, item): nd = ListNode(item) self.my_set.add(nd) def remove(self, item): self.my_set.remove(item) def iterator(self): return self.my_set.__iter__() def equals(self, other_set): if type(other_set) is not Set: print("Unsuitable object type!") return "" if self.length() != other_set.length(): return False set1 = other_set.iterator() for elem in set1: if not self.my_set.__contains__(elem): return False return True def difference(self, other_set): if type(other_set) is not Set: print("Unsuitable object type!") return "" result_set = Set() it = self.iterator() for item in it: if not other_set.contains(item): result_set.add(item) return result_set def issubsetof(self, other_set): if type(other_set) is not Set: print("Unsuitable object type!") return "" if self.length() > other_set.length(): return False for item in self.my_set: if not other_set.contains(item): return False return True
class MySet: def __init__(self): self.items = MyList() def length(self): return self.items.__len__() def contains(self, item): return self.items.__contains__(item) def isEmpty(self): return self.items.__len__() == 0 def add(self, item): i = ListNode(item) self.items.add(i) def remove(self, item): i = ListNode(item) return self.items.remove(i) def equals(self, setB): if self.length() != setB.length(): return False else: return self.isSubsetOf(setB) def isSubsetOf(self, setB): for element in self: if element not in setB: return False return True def union(self, setB): newSet = MySet() for element in self.items: newSet.add(element) for element in setB: if element not in self: newSet.add(element) return newSet def difference(self, setB): newSet = MySet() for element in self.items: newSet.add(element) for element in setB: if element in self: newSet.remove(element) return newSet def print_set(self): for i in self.items: print(i, end=', ') def __iter__(self): return self.items.__iter__()
class MyQueue: def __init__(self): self.items = MyList() def length(self): return self.items.__len__() def isEmpty(self): if self.length() == 0: return True return False def enqueue(self, item): i = ListNode(item) self.items.add(i) def dequeue(self): if not self.isEmpty(): data = self.items.get_item() return data def __iter__(self): return self.items.__iter__()