def enqueue(self, value): if self.has_space(): add_item = Node(value) print("Adding " + str(add_item.get_value()) + " to the queue!") if self.is_empty(): self.front = add_item self.back = add_item else: self.back.set_link_node(add_item) self.back = add_item self.size += 1 else: print("Oops, no more room!")
class LinkedList: def __init__(self, value=None): self.head_node = Node(value) def get_head_node(self): return self.head_node def peek_head(self): return self.head_node.get_value() def insert_front_node(self, new_value): new_node = Node(new_value) new_node.set_link_node(self.head_node) self.head_node = new_node def length_of_list_iterative(self): count = 0 cur_node = self.head_node while cur_node: count += 1 cur_node = cur_node.get_link_node() return count def string_list(self): str_list = "" current_node = self.get_head_node() while current_node: if current_node.get_value() != None: str_list += str(current_node.get_value()) + "\n" current_node = current_node.get_link_node() return str_list def remove_node(self, value_to_remove): current_node = self.get_head_node() if current_node.get_value() == value_to_remove: self.head_node = current_node.get_link_node() else: while current_node: next_node = current_node.get_next_node() if next_node.get_value() == value_to_remove: current_node.set_link_node(next_node.get_link_node()) current_node = None else: current_node = next_node