def test_join(): list = SingleLinkedList() list2 = SingleLinkedList() list.join(list2) assert list.isEmpty() list.prepend(1).prepend(2).prepend(3) list2.prepend(4).prepend(5).prepend(6) list.join(list2) assert list.head() == 6 assert list.size() == 6
def test_remove_last(): list = SingleLinkedList() list.removeLast() assert list.isEmpty() list.append(1).removeLast() assert list.isEmpty() list.prepend(3).prepend(2).prepend(1) assert list.size() == 3 list.removeLast() assert list.head() == 1 assert list.size() == 2 list.removeLast().removeLast().removeLast() assert list.size() == 0
class Stack: def __init__(self): self.__list = SingleLinkedList() def isEmpty(self): return self.__list.isEmpty() def size(self): return self.__list.size() def top(self): return self.__list.head() def push(self, element): self.__list.prepend(element) return self def pop(self): element = self.top() self.__list = self.__list.tail() return element
def test_prepend_list(): list = SingleLinkedList() assert list.head() is None list.prepend(1).prepend(2).prepend(3) assert list.head() == 3
def test_append(): list = SingleLinkedList() list.append(1) assert list.head() == 1 list.append(2).append(3) assert list.head() == 1