示例#1
0
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__()
示例#2
0
class Stack:
    def __init__(self):
        self.my_stack = MyList()

    def length(self):
        return self.my_stack.__len__()

    def isEmpty(self):
        if self.length() == 0:
            return True
        return False

    def pop(self, data):
        if not self.isEmpty():
            data = self.my_stack.head.get_data()
            self.my_stack.remove(self.my_stack.head.get_data())
            return self.my_stack.head.get_data()

        print("The stack contains no items!")
        return ""

    def push(self, item):
        self.my_stack.add(item)

    def peek(self):
        if not self.isEmpty():
            return self.my_stack.head.get_data()

        print("The stack contains no items!")
        return ""
示例#3
0
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
示例#4
0
class Stack:
	def __init__(self):
		self.stack = MyList()
		
	def length(self):
		return self.stack.__len__()
		
	def isEmpty(self):
		if self.length() == 0:
			return True
		return False
		
	def pop(self):
		if not self.isEmpty():
			data = self.stack.get_last_elem()
			self.stack.remove(data)
			return data
				
		print("Stack is empty")
		return ""
			
		
	def push(self, item):
		elem = ListNode(item)
		self.stack.add(elem)
		
	def peek(self):
		if not self.isEmpty():
			return self.stack.get_last_elem()
				
		print("Stack is empty")
		return ""
示例#5
0
class Queue:
	def __init__(self):
		self.my_queue = MyList()
		
	def length(self):
		return self.my_queue.__len__()
		
	def isEmpty(self):
		if self.length() == 0:
			return True
		return False
		
	def dequeue(self):
示例#6
0
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__()