class Stack: def __init__(self): self.__l = SinglyLinkedList() self.top = -1 def push(self, element): self.__l.append(element) self.top += 1 def pop(self): if self.top == -1: raise Exception("Cannot pop from an empty stack") self.top -= 1 return self.__l.pop() @property def top_item(self): if self.top == -1: return return self.__l.tail def __iter__(self): for i in self.__l: yield i def __str__(self): return "[" + ", ".join([str(_) for _ in self]) + "]"
def test_push(): """ Can we properly push a data to linked list or not? new data must be in head """ names = SinglyLinkedList() names.push('John') expected = 'John' actual = names.head.data assert actual == expected
class MaxBinaryHeap: def __init__(self): self.values = SinglyLinkedList() def swap(self, node_1, node_2): node_1.data, node_2.data = node_2.data, node_1.data def __bubble_up(self): if len(self.values) == 1: return index = len(self.values) - 1 parent = (index - 1) // 2 while (self.values[index].data > self.values[parent].data) and index > 0: self.swap(self.values[index], self.values[parent]) index = parent parent = (index - 1) // 2 def insert(self, values): for value in values: self.values.append(value) self.__bubble_up() def __sink_down(self): if len(self.values) == 1: return index = 0 l_child, r_child = 2 * index + 1, 2 * index + 2 if len(self.values) == 2: if self.values[index].data > self.values[l_child].data: self.swap(self.values[index], self.values[l_child]) while (r_child < len(self.values) and l_child < len(self.values)) and ( (self.values[index].data < self.values[l_child].data) or (self.values[index].data < self.values[r_child].data)): if self.values[l_child].data > self.values[r_child].data: self.swap(self.values[index], self.values[l_child]) index = l_child else: self.swap(self.values[index], self.values[r_child]) index = r_child l_child, r_child = 2 * index + 1, 2 * index + 2 def extract_max(self): if self.values.is_empty: return max_element = self.values.head.data self.values.head.data = self.values.tail.data self.values.pop() self.__sink_down() return max_element def __str__(self): return str(self.values)
def test_peek(): """ If we can peek data from head of linked list? """ pets = SinglyLinkedList() pets.push('cat') pets.push('dog') expected = 'dog' actual = pets.peek() assert actual == expected
def breadth_first_search(self, node): if node is None: return queue = Queue() visited = SinglyLinkedList() queue.enqueue(node) while not queue.is_empty: node = queue.dequeue().data visited.append(node.data) if node.l_child: queue.enqueue(node.l_child) if node.r_child: queue.enqueue(node.r_child) return visited
def test_insert_at(): """ can we insert at a given index? """ nums = SinglyLinkedList() nums.push(1) nums.push(3) nums.push(54) expected = 3 actual = nums.get_at(1) assert actual == expected nums.insert_at(1, 666) expected = 666 actual = nums.get_at(1) assert actual == expected
def __init__(self, size, hash_type='sum'): if hash_type != 'sum' and != 'mult': raise ValueError("must have 'sum' or 'mult' value") self.hash_list = [] self.size = size self.hash_type = hash_type for i in range(size): self.hash_list.append(SinglyLinkedList())
def depth_first_search(self, node, mode): traversed = SinglyLinkedList() if node is None: return if mode is TreeTraverse.PRE_ORDER: self.__pre_order(node, traversed) elif mode is TreeTraverse.IN_ORDER: self.__in_order(node, traversed) elif mode is TreeTraverse.POST_ORDER: self.__post_order(node, traversed) return traversed
def test_append(): """ can we insert data at the end? """ nums = SinglyLinkedList() nums.push(1) nums.push(3) nums.append(55) expected = 55 actual = nums.get_at(2) assert actual == expected
def test_len(): """ can we get length of linked list """ nums = SinglyLinkedList() nums.push(1) nums.push(2) nums.push(55) expected = 3 actual = nums.len() assert actual == expected
def test_search(): """ can we search a value in linked list at first match and get its index """ nums = SinglyLinkedList() nums.push(1) nums.push(3) nums.push(5) expected = 1 actual = nums.search(3) assert actual == expected
class Queue: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__l = SinglyLinkedList() self.__front = -1 self.__rear = -1 @property def is_empty(self): return self.__front == self.__rear @property def front(self): return self.__front @property def rear(self): return self.__rear def enqueue(self, element): self.__l.append(element) self.__rear += 1 def dequeue(self): self.__front += 1 if self.is_empty: self.__front, self.__rear = -1, -1 return self.__l.shift() def peek(self): return self.__l.head def __iter__(self): for i in self.__l: yield i def __str__(self): return "[" + ", ".join([str(_) for _ in self]) + "]"
def test_remove_at(): """ can we remove node at certain index of list """ nums = SinglyLinkedList() nums.push(1) nums.push(3) nums.push(4) expected = 3 actual = nums.remove_at(1) assert expected == actual expected = 1 actual = nums.get_at(1) assert expected == actual
def test_get_at(): """ Can we get nth nodes data? """ pets = SinglyLinkedList() pets.push('cat') pets.push('dog') pets.push('fish') pets.push('bird') expected = 'dog' actual = pets.get_at(2) assert actual == expected
def test_pop(): """ can we remove data from head? pop will return the removed data """ nums = SinglyLinkedList() nums.push(1) nums.push(2) expected = 2 actual = nums.pop() assert expected == actual expected = 1 actual = nums.peek() assert expected == actual
def __init__(self): self.__l = SinglyLinkedList() self.top = -1
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__l = SinglyLinkedList() self.__front = -1 self.__rear = -1
def __init__(self): self.values = SinglyLinkedList()
curr = self.head while len(self) > 1: count = 1 while count != step: curr = curr.next count += 1 self.remove_node(curr) curr = curr.next def is_circular(self, another_list): curr = another_list.head while curr and curr.next: curr = curr.next if curr.next == another_list.head: return True return False clist = CircularLinkedList() clist.append(1) clist.append(2) clist.append(3) clist.append(4) llist = SinglyLinkedList() llist.append(1) llist.append(2) llist.append(3) llist.append(4) print(clist.is_circular(llist))
from linked_list.singly_linked_list import SinglyLinkedList def reversedLinkedList(LinkedList): previous = None current = LinkedList.head while (current != None): temp = current.next current.next = previous previous = current current = temp LinkedList.head = previous if __name__ == "__main__": myLinkedList = SinglyLinkedList() for i in range(10, 0, -1): myLinkedList.insertAtStart(i) print("Original: ", end=" ") myLinkedList.printLinkedList() print() print("Reversed: ", end=" ") reversedLinkedList(myLinkedList) myLinkedList.printLinkedList()
def test_instantiation(): """ checking if it is successfully instantiate an empty linked list? """ assert SinglyLinkedList()