Esempio n. 1
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 ""
Esempio n. 2
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
Esempio n. 3
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 ""
Esempio n. 4
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__()
Esempio n. 5
0
class Set:
    def __init__(self):
        self.linkedList = MyList()

    def length(self):
        return abs(self.linkedList.__len__)

    def contains(self):
        return self.linkedList.__contains__

    def add(self, item):
        newItem = ListNode(item)
        self.linkedList.add(newItem)

    def remove(self, target):
        self.linkedList.remove(target)

    def isSubsetOf(self, inputSet):
        for element in self.linkedList:
            if element not in inputSet.linkedList:
                return False
        return True

    def equals(self, inputSet):
        if len(self.linkedList) != len(inputSet.linkedList):
            return False
        else:
            return self.isSubsetOf(inputSet)

    def intersection(self, setB):
        newSet = Set()
        for element in setB.linkedList:
            if self.linkedList.__contains__(element):
                newSet.add(element)
        return newSet

    def __repr__(self):
        output = "Set: "
        newList = list()
        for i in self.linkedList:
            newList.append(i)

        output += str(newList)

        return output
Esempio n. 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__()
Esempio n. 7
0
from linked_list import MyList, ListNode
lst = MyList()
nd = ListNode(1)
lst.add(nd)
nd = ListNode(2)
lst.add(nd)
nd = ListNode(3)
lst.add(nd)
lst.remove(2)

for data in lst:
    print(data)