class Stack: def __init__(self): self.list = Linked_list() self.size = 0 def push(self,data): self.list.add_node_head(data) self.size += 1 def pop(self): if self.isEmpty(): return None else : data = self.top() self.list.delete_node(data) self.size -= 1 return data def top(self): if self.isEmpty(): return None else: return self.list.get_head().data def isEmpty(self): if self.size == 0: return True else: return False
def test_delete_first_item(sef): ll = Linked_list(array) item = 1 if item == ll.head.data: ll.head = ll.head.next assert (ll.head.data == 2)
def test_replace_implementation(self): ll = Linked_list(array) assert ll.replace(8, 1) == 8 #replace first element assert ll.replace(9, 3) == 9 #replace item in the middle assert ll.replace(10, 6) == 10 #replace last item assert ll.replace(0, 45) == None #replace item that doesn't exist assert ll.size == 6 #size doesn't change
def test_prepend_implementation(self): ll = Linked_list() assert ll.prepend(1) == 1 assert ll.prepend(2) == 2 assert ll.head.data == 2 assert ll.tail.data == 1 assert ll.size == 2
def test_find(self): ll = Linked_list(array) #pdb.set_trace() assert ll.find(lambda item: item == 1) == 1 assert ll.find(lambda item: item < 2) == 1 assert ll.find(lambda item: item > 3) == 4 assert ll.find(lambda item: item == 7) is None
def test_delete_implementation(self): ll = Linked_list(array) assert ll.delete(1) == 1 assert (ll.delete(4)) == 4 assert (ll.delete(6)) == 6 assert (ll.delete(9)) == None assert (ll.size == 3)
def test_delete_last_node(self): ll = Linked_list(array) item = 6 current_node = ll.head previous_node = current_node while current_node is not None: if current_node is not None and current_node.next is None: ll.tail = previous_node previous_node = current_node current_node = current_node.next assert (ll.tail.data == 5)
def test_delete_node_in_middle(self): ll = Linked_list(array) item = 4 current_node = ll.head previous_node = current_node while current_node is not None: if current_node.data == item: current_node = current_node.next previous_node.next = current_node previous_node = current_node current_node = current_node.next assert ll.find(lambda item: item == 4) is None
def split(linked_list): if linked_list == None or linked_list.head == None: left_half = linked_list right_half = None return left_half,right_half else: size = linked_list.size() mid = size // 2 mid_node = linked_list.node_at_index(mid-1) left_half = linked_list right_half = Linked_list() right_half.head = mid_node.next_Node mid_node.next_Node = None return left_half,right_half
class Queue: def __init__(self): self.list = Linked_list() self.size = 0 def poll(self): if self.isEmpty(): return None else: data = self.peek() # linked list should store last element and delete node in O(1) self.list.delete_node(data) self.size -= 1 return data def peek(self): if self.isEmpty(): return None else: return self.list.get_head().data def add(self,data): self.list.add_node(data) self.size += 1 def isEmpty(self): if self.size == 0: return True else: return False def print_queue(self): self.list.list_print()
def merge(left,right): merge = Linked_list() merge.add(0) current = merge.head left_head = left.head right_head = right.head while left_head or right_head: if left_head is None: current.next_Node = right_head right_head = right_head.next_Node elif right_head is None: current.next_Node = left_head left_head = left_head.next_Node else: left_data = left_head.data right_data = right_head.data if left_data < right_data : current.next_Node = left_head left_head = left_head.next_Node else: current.next_Node = right_head right_head = right_head.next_Node current = current.next_Node head = merge.head.next_Node merge.head = head return merge
def test_replace(self): ll = Linked_list(array) newItem = Node(8) replaceItem = 6 current = ll.head previous = current while current: if current.data == replaceItem: current = current.next previous.next = newItem newItem.next = current break previous = current current = current.next assert (ll.find(lambda item: item == 8)) == 8 assert (ll.find(lambda item: item == 6)) == None
def test_delete_none_existing_item(sef): ll = Linked_list(array) current_node = ll.head previous_node = current_node item = 9 find = False while current_node is not None: if current_node.data == item: current_node = current_node.next previous_node.next = current_node find = True previous_node = current_node current_node = current_node.next assert (find == False)
def test_init_array(self): ll = Linked_list(array) assert (ll.head.data == 1) assert (ll.head.next.data == 2) assert (ll.tail.data == 6)
def push(self, value): Linked_list.add_to_tail(self, value)
def pop(self): if self.size == 0: return None return_value = Linked_list.remove_from_head(self) return return_value
def pop_min (self): return Linked_list.remove_from_head(self)
def pop_max (self): return Linked_list.remove_from_tail(self)
def __init__(self): self.list = Linked_list() self.size = 0
a_ref = a_curr.next if b_curr: b_ref = b_curr.next if a_curr: a_curr.next = b_curr if b_curr: b_curr.next = a_ref a_curr = a_ref b_curr = b_ref return a_curr if __name__ == "__main__": lst1 = Linked_list() lst2 = Linked_list() lst3 = Linked_list() lst1.insert(1) lst1.append(2) lst1.append(3) lst1.append(2) lst1.append(1) lst2.insert(2) lst2.append(4) lst2.append(6) lst2.append(8) lst2.append(10) lst3.insert(420) lst3.append(69) print(lst1.to_string())
def __init__(self): Linked_list.__init__(self)
from linked_list import Node, Linked_list def is_cycle(node): if not node or not node.next: return False slow, fast = node while slow: if fast.next or fast.next.next: return False if slow == fast: return True if __name__ == "__main__": linked_list = Linked_list() print(linked_list)
from linked_list import Linked_list linked_list_1 = Linked_list() linked_list_2 = Linked_list() linked_list_3 = Linked_list() linked_list_4 = Linked_list() elements_1 = [3, 2, 4, 35, 6, 65, 6, 4, 3, 21] elements_2 = [6, 32, 4, 9, 6, 1, 11, 21, 1] for i in elements_1: linked_list_1.append(i) for i in elements_2: linked_list_2.append(i) print(linked_list_1.union(linked_list_2)) print(linked_list_1.intersection(linked_list_2)) elements_3 = [3, 2, 4, 35, 6, 65, 6, 4, 3, 23] elements_4 = [1, 7, 8, 9, 11, 21, 1] for i in elements_3: linked_list_3.append(i) for i in elements_4: linked_list_4.append(i) print(linked_list_3.union(linked_list_4)) print(linked_list_3.intersection(linked_list_4))
def linked_list(): return Linked_list()
from linked_list import Linked_list link_list1 = Linked_list() link_list1.append(0) link_list1.append(1) link_list1.append(2) link_list1.append(3) link_list1.append(4) link_list1.append(5) item = link_list1[3] print(item) print(link_list1) link_list1.delete(3) print(link_list1) link_list1.delete(0) print(link_list1) link_list1.append(6) print(link_list1) link_list1.prepend(0) print(link_list1)
def __init__(self, init_size=8): """Initialize this hash table with the given initial size.""" # Create a new list (used as fixed-size array) of empty linked lists self.buckets = [Linked_list() for _ in range(init_size)]
Search.verify(resulte) print(f"time of monitor: {(lastTime-StartTime)}") #--------------------// chack if the List is sorted or not \\-------------------- print("\n--------// chack if the List is sorted or not \\\\-------") # print(alist) # print(Sort.verify_sort(alist)) StartTime = time.perf_counter_ns() sorted_list = Sort.merge_sort(alist) EndTime = time.perf_counter_ns() print(f"sorted_list {sorted_list} \ntime of monitor: {(EndTime-StartTime)}ns ") # print(Sort.verify_sort(sorted_list)) #------------------// sort lkinked list \\------------------------------------- print("\n--------// sort lkinked list \\\\-------") l = Linked_list() l.add(20) l.add(30) l.add(10) l.add(5) l.add(25) l.add(40) print(l) sorted_link_list = Linked_list_sortition.sort_linked_list(l) print(sorted_link_list) #------------------// bogo sort \\------------------------------------- print("\n-----------//bogo sort \\\\ ----------------") alist1 = load_numbers("Data_Structures/Files/numers.txt") StartTime = time.perf_counter_ns() sorted_bogo = Sort.bogo_sort(alist1)