def __init__(self): self.queue = LinkedList()
from time import time from singly_linked_list import LinkedList """ Create an array with 100,000 items Time how long it takes to remove all of them from the front of a list and a linked list respectively. """ n = 1000000 # a list containing n elements l = [i for i in range(n)] my_list = LinkedList() # add n elements to the linked list for i in range(n): my_list.add_to_tail(i) start_time = time() # remove n elements from the front of our linked list for _ in range(n): my_list.remove_head() end_time = time() ll_time = end_time - start_time print(f'It took {ll_time} seconds to remove {n} items from our linked list.')
odd.append(curr.value) curr = curr.next curr = odd.head if curr is None: return even while curr.next is not None: curr = curr.next curr.next = even.head return odd if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6] solution = [1, 3, 5, 2, 4, 6] llist = LinkedList().from_list(arr) res = even_after_odd(llist).to_list() for i, num in enumerate(solution): assert num == res[i] arr = [1, 3, 5, 7] solution = [1, 3, 5, 7] llist = LinkedList().from_list(arr) res = even_after_odd(llist).to_list() for i, num in enumerate(solution): assert num == res[i]
import time from singly_linked_list import LinkedList n = 100000 l = [i for i in range(n)] ll = LinkedList() for i in range(n): ll.add_to_tail(i) start_time = time.time() for i in range(n): ll.remove_head() end_time = time.time() print(f"Linked List remove from head took {float(end_time - start_time)} seconds") start_time = time.time() for i in range(n):
def __init__(self): self.size = 0 # 1. use array as storage # self.storage = [] # 2. use LinkedList as storage self.storage = LinkedList()
start_time = time.time() f = open('names_1.txt', 'r') names_1 = f.read().split("\n") # List containing 10000 names f.close() f = open('names_2.txt', 'r') names_2 = f.read().split("\n") # List containing 10000 names f.close() names = names_1 + names_2 names.sort() #duplicates = [] # Return the list of duplicates in this data structure duplicates = LinkedList() #Replace the nested for loops below with your improvements # for name_1 in names_1: # for name_2 in names_2: # if name_1 == name_2: # duplicates.add_to_head(name_1) new_list = LinkedList() for n in names: new_list.add_to_head(n) node = new_list.head while node.next_node is not None: if node.value == node.next_node.value: duplicates.add_to_head(node.value) node = node.next_node
def __init__(self): self.size = 0 # keeping track of size # 2. use LinkedList as storage # To initialize a new stack,set the attr to be an empty # linked list. empty link list consist of head & tail = none self.storage = LinkedList()
def setUp(self): self.l = LinkedList() for i in 'ABCDE': self.l.append(i)
def __init__(self): self.size = 0 # self.storage = np.array([]) self.storage = LinkedList(None)
def __init__(self): self.__storage = LinkedList()
def __init__(self, size=0, storage=LinkedList()): self.size = size self.storage = storage
temp = reverse_linked_list_recursion(head.next) # Assume all nodes after the current node's next node are all # reversed, now we need to reverse the next node and the # current node.For example, when 3 is returned, temp will be 3, # while head will be 2, and 2.next.next is now assigned to 2 # which means 3.next = 2, so the node is reversed. head.next.next = head # To avoid a list loop, we need to set head.next to None, in # this case, 2.next is set to None for now. head.next = None # Return the last node or the first node in the reversed list. # Here it's 3. return temp if __name__ == "__main__": new_list = LinkedList([1,2,3,4,5,6]) print(f"Linked List: {new_list}") new_head = reverse_linked_list(new_list.head) reversed_list = LinkedList() reversed_list.head = new_head print(f"Linked List: {reversed_list}") new_list = LinkedList([1,2,3,4,5,6]) print(f"Linked List: {new_list}") reversed_head = reverse_linked_list_recursion(new_list.head) new_reversed_list = LinkedList() new_reversed_list.head = reversed_head print(f"Linked List: {new_reversed_list}")
def __init__(self, init_node=None): if init_node: self.size = 1 else: self.size = 0 self.storage = LinkedList(init_node)
def __init__(self): # Initialize this queue to the empty queue self.size = 0 # Use the LinkedList class as storage self.storage = LinkedList()
def test_forward_sum(self): out = LinkedList() out.first = forward_sum(self.x.first, self.y.first) self.assertEqual(str(out), str(self.forward))
def test_insert(lst): lnkd_lst = LinkedList() for i in lst: lnkd_lst.insert(i, 0) for i in range(len(lst) - 1, 0, -1): assert lst[::-1][i] == lnkd_lst.get(i).value
sum_list.append(remainder) else: carry = 0 sum_list.append(s) if p: p = p.next if q: q = q.next if carry: sum_list.append(carry) return sum_list l1 = LinkedList() l1.append(2) l1.append(1) l1.append(5) l1.print_list() # 5 -> 1 -> 2 -> l2 = LinkedList() l2.append(8) l2.append(3) l2.append(6) l2.print_list() # 8 -> 3 -> 6 -> sum_l = sum_two_lists(l1, l2) sum_l.print_list() # 0 -> 5 -> 1 -> 1 ->
def test_get(a): lnkd_lst = LinkedList() lnkd_lst.append(a) assert lnkd_lst.get(0).value == a
def __init__(self): self.size = 0 #Keep track of the length. You could do this in the linked list class self.storage = LinkedList()
def test_append(lst): lnkd_lst = LinkedList() for i in lst: lnkd_lst.append(i) for i in range(len(lst) - 1, 0, -1): assert lst[i] == lnkd_lst.get(i).value
def __init__( self ): #initialize the Queue class that will manage the values of the head (first item in the queue) tail (newest or latest item added to the queue) self.size = 0 self.storage = LinkedList()
def test_pop(a): lnkd_lst = LinkedList() for i in a: lnkd_lst.append(i) assert lnkd_lst.get(0).value == lnkd_lst.pop(0)
def __init__(self): self.size = 0 self.items = LinkedList()
def __init__(self): # initialize constructor method self.size = 0 # set size to 0 self.storage = LinkedList() # set storage to LinkedList class
def __init__(self): self.size = 0 # self.storage = [] #array structure self.storage = LinkedList() #linkedlist structure
def __init__(self): self.size = 0 # self.storage = ? self.data = LinkedList()
def setUp(self): self.list = LinkedList()
def test_reverse_sum(self): out = LinkedList() out.first = reverse_sum(self.x.first, self.y.first, 0) self.assertEqual(str(out), str(self.reverse))
def __init__(self): self.size = 0 self.storage = LinkedList()
def __init__(self): self.size: int = 0 self.storage: LinkedList = LinkedList()