def test_3(self): """ test that append(node) adds to end """ l = LinkedList() n1 = Node(5) n2 = Node(3) l.append(n1) l.append(n2) self.assertEqual(l.head._next._value, 3)
def test_5(self): """ test pop() """ l = LinkedList() n1 = Node(5) n2 = Node(3) l.append(n1) l.append(n2) n = l.pop() self.assertEqual(n._value, 5)
def test_where_k_is_not_a_positive_integer(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") actual = x.kth_from_end(-3) expected = "you must enter a positive value" assert actual == expected
def test_insert_multiple_node(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") actual = x.contains() expected = ["e", "d", "c", "b", "a"] assert actual == expected
def test_return_false_if_value_in_list(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") actual = x.includes("z") expected = False assert actual == expected
def test_kth_from_end_returns_correct_node_value(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") actual = x.kth_from_end(2) expected = "c" assert actual == expected
def test_where_k_and_the_length_of_the_list_are_the_same(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") actual = x.kth_from_end(5) expected = "k is greater than length of linked list" assert actual == expected
def test_LinkedList_reverse(self): linked_list = LinkedList() linked_list.push(1) linked_list.push(2) linked_list.push(3) print(repr(linked_list)) linked_list.reverse() print(repr(linked_list))
def test_head_node_fist_in_list(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") actual = x.head.val expected = "e" assert actual == expected
def test_7(self): """ test remove(node) """ l = LinkedList() n1 = Node(1) n2 = Node(2) n3 = Node(3) l.append(n1) l.append(n2) l.append(n3) l.remove(2) self.assertEqual(n1._next._value, 3)
def test_can_return_all_elements_in_list(): x = LinkedList() x.insert("c") x.insert("b") x.insert("a") actual = x.__str__() expected = "{ a } -> { b } -> { c } -> NULL" assert actual == expected
def test_6(self): """ test remove(node) for no matching node """ l = LinkedList() n1 = Node(1) n2 = Node(2) n3 = Node(3) l.append(n1) l.append(n2) l.append(n3) self.assertEqual(l.remove(7), "Error: Node does not exist!")
def test_4(self): """ test push(node) """ l = LinkedList() n1 = Node(5) n2 = Node(3) l.append(n1) l.push(n2) self.assertEqual(l.head._value, 3)
def test_8(self): """ test len(list) """ l = LinkedList() n1 = Node(1) n2 = Node(2) n3 = Node(3) l.append(n1) l.append(n2) l.append(n3) self.assertEqual(len(l), 3)
from linked import LinkedList from linked import Node if __name__ == '__main__': llist = LinkedList() llist.head = Node(1) second = Node(2) third = Node(3) llist.head.next = second second.next = third llist.printList() llist.push(4) llist.push(5) llist.printList() llist.append(10) llist.append(20) llist.printList() llist.delete(20) llist.printList() print(llist.getC()) llist.push(30) llist.printList() print(llist.getCentre()) llist.reverse() llist.printList() llist.rev() llist.printList()
def test_LinkedList(self): linked_list = LinkedList() self.assertEqual(linked_list.size(), 0) linked_list_num = LinkedList(Node(1)) self.assertEqual(linked_list_num.pop(), 1) self.assertEqual(linked_list_num.push(1), None) self.assertEqual(linked_list_num.head.data, 1) self.assertEqual(linked_list_num.head.next, None) self.assertEqual(linked_list_num.push(2), None) self.assertEqual(linked_list_num.pop(), 2) self.assertEqual(linked_list_num.size(), 1) self.assertEqual(linked_list_num.is_empty(), False)
def test_insert_single_node(): x = LinkedList() x.insert("a") actual = x.head.val expected = "a" assert actual == expected
def test_empty_list(): assert LinkedList()
def test_can_successfully_add_multiple_nodes_to_the_end_of_a_linked_list(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") x.append("test one") x.append("test two") actual = x.contains() expected = ["e","d","c","b","a", "test one", "test two"] assert actual == expected
class StackLinked: """ A stack data structure implemented with a singly linked list. Attributes: capacity(int): The size limiter of the stack. head(LinkedList): The LinkedList that stores our nodes. num_items(int): The number of items in the stack. """ def __init__(self, capacity): self.capacity = capacity self.items = LinkedList() self.num_items = 0 def __repr__(self): return "StackLinked({}, {}, {})".format(self.capacity, self.items, self.num_items) def __eq__(self, other): return (isinstance(self, StackLinked) == isinstance( other, StackLinked) and self.capacity == other.capacity and self.items == other.items and self.num_items == other.num_items) def is_empty(self): """ Checks if the stack is empty. Args: self(StackLinked): The current object referencing this method. Returns: boolean: True if the stack is empty, else false. """ return self.num_items == 0 def is_full(self): """ Checks if the stack is full. Args: self(StackLinked): The current object referencing this method. Returns: boolean: True if the stack is full, else false. """ return self.num_items == self.capacity def push(self, item): """ Add an item to the stack. If the stack is full return Error. Args: self(StackLinked): The current object referencing this method. item(Any): The data that is going to be pushed to the stack Returns: None: Does not return anything """ if self.is_full(): raise IndexError self.items.push(item) self.num_items += 1 def pop(self): """ Removes an item from the top of the stack. If the stack is empty return None. Args: self(StackLinked): The current object referencing this method. Returns: Any: The data that was removed from the top of the stack. """ if self.is_empty(): raise IndexError self.num_items -= 1 return self.items.pop() def peek(self): """ The item at the top of the Stack. Args: self(StackLinked): The current object referencing this method. Returns: *: The data at the top of the Stack. """ return self.items.head.get_data() def size(self): """ The size of the StackLinked. Args: self(StackLinked): The current object referencing this method. Returns: int: Number of items in the Stack. """ return self.items.num_items
def set(self, key, value): hash_number = self.hash(key) if not self.buckets[hash_number]: self.buckets[hash_number] = LinkedList() self.buckets[hash_number].add((key, value))
def test_can_successfully_add_a_node_to_the_end_of_the_linked_list(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") x.append("g") actual = x.contains() expected = ["e","d","c","b","a","g"] assert actual == expected
def test_can_successfully_insert_a_node_after_the_last_node_of_the_linked_list(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") x.insert_after("a", "test") actual = x.contains() expected = ["e","d","c","b","a","test"] assert actual == expected
def test_can_successfully_insert_a_node_before_the_first_node_of_a_linked_list(): x = LinkedList() x.insert("a") x.insert("b") x.insert("c") x.insert("d") x.insert("e") x.insert_before("e", "test") actual = x.contains() expected = ["test","e","d","c","b","a"] assert actual == expected
def __init__(self, capacity): self.capacity = capacity self.items = LinkedList() self.num_items = 0
@forwardable() class InsertCounter: def_delegators('linked_list', 'remove,clear,__iter__,__str__,__len__') def __init__(self, linked_list): self.linked_list = linked_list self._counter = 0 def insert(self, value): self.linked_list.insert(value) self._counter += 1 def insert_all(self, iterable): iter1, iter2 = tee(iterable) self.linked_list.insert_all(iter1) self._counter += sum(1 for _ in iter2) @property def counter(self): return self._counter lst = InsertCounter(LinkedList()) lst.insert(4) lst.insert_all([8, 15, 16, 23, 42]) lst.remove() print(lst) print(f'length = {len(lst)}') print(f'counter = {lst.counter}')
def test_dif_length_lists(): x = LinkedList() x.insert("4") x.insert("3") x.insert("2") x.insert("1") x.insert("a") x.insert("c") x.insert("e") y = LinkedList() y.insert("b") y.insert("d") y.insert("f") z = zip_lists(x, y) actual = z.contains() expected = ["f", "e", "d", "c", "b", "a", "1", "2", "3", "4"] assert actual == expected
def test_insert_multiple_node(): x = LinkedList() x.insert("a") x.insert("c") x.insert("e") y = LinkedList() y.insert("b") y.insert("d") y.insert("f") z = zip_lists(x, y) actual = z.contains() expected = ["f", "e", "d", "c", "b", "a"] assert actual == expected
def test_where_the_linked_list_is_of_a_size_1(): x = LinkedList() x.insert("a") actual = x.kth_from_end(0) expected = "a" assert actual == expected
def test_2(self): """ test that append adds a node """ l = LinkedList() n1 = Node(5) l.append(n1) self.assertEqual(l.head._value, 5)