class LLQueue: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): return self.size def enqueue(self, value): self.size += 1 self.storage.add_to_end(value) def dequeue(self): self.size -= 1 return self.storage.remove_from_head()
class Stack: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): return self.size def push(self, value): self.storage.add_to_end(value) self.size += 1 def pop(self): if not self.storage.head: return None else: self.size -= 1 return self.storage.remove_at_end()
class Queue: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): return self.size def enqueue(self, value): self.storage.add_to_end(value) self.size += 1 def dequeue(self): if not self.storage.head: return None else: self.size -= 1 return self.storage.remove_from_head()
class Queue: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): return self.size def enqueue(self, value): # add a value to be the last node on ll self.storage.add_to_end(value) # update the size attr self.size = self.size + 1 def dequeue(self): # if list is empty then return nothing if not self.storage.head: return None else: # remove first element from ll value = self.storage.remove_from_head() # update the size attr self.size = self.size - 1 return value
bob = Person("Bob", 25, ("burping", "biking", "breaking")) rob = Person("Rob", 35, ("riding", "reading", "reviewing")) tob = Person("Tob", 45, ("training", "teaching", "talking")) # Create LinkedList and fill it with a list containing each person list_of_people = LinkedList([bob, rob, tob]) # Loop through LinkedList for person in list_of_people: print(person.name + " is " + str(person.age) + " years old.") # Add more people to our list pob = Person("Pob", 65, ("playing", "praying", "paying")) lob = Person("Lob", 15, ("laughing", "lauding", "laying")) nob = Person("Nob", 55, ("naying", "naming", "nailing")) list_of_people.add_to_end(pob) list_of_people.add_to_beginning(lob) list_of_people.insert_after(tob, nob) # Print our list to the console list_of_people.print_list() # Reverse our list list_of_people.reverse() list_of_people.print_list() # Manipulate our list list_of_people.remove(lob) # Removes lob from list print("List contains lob: " + str(list_of_people.contains(lob))) # Check if lob is gone also_bob = list_of_people.poll_first(
list1 = LinkedList() list2 = LinkedList((1, 2, 3)) list3 = LinkedList(list2) # Test add methods list3.insert_after(2, 2) list3.insert_after(3, (4, 5)) list3.print_list() print("Expected: 1 2 2 3 4 5") print("-----") list3.add_to_beginning(0) list3.add_to_beginning((0, 0)) list3.print_list() print("Expected: 0 0 0 1 2 2 3 4 5") print("-----") list3.add_to_end(6) list3.add_to_end((7, 8)) list3.print_list() print("Expected: 0 0 0 1 2 2 3 4 5 6 7 8") print("-----") # Test contains(), size(), remove(), clear_list() print(list3.size()) print("Expected: 12") print("-----") list3.clear_list() list3.print_list() print((list3.size() == 0)) print("Expected: True") print("-----") print(list3.contains(1))
tkam = Book("To Kill a Mockingbird", "Harper Lee", 1960) podg = Book("The Picture of Dorian Gray", "Oscar Wilde", 1890) # Create LinkedList and fill it with a list containing each book list_of_books = LinkedList([thg, tkam, podg]) # Loop through LinkedList for book in list_of_books: print(book.title + " is " + str(datetime.now().year - book.year_published) + " years old.") print() # Add more books to our list tpp = Book("The Pilgrim's Progress", "John Bunyan", 1678) tsl = Book("The Screwtape Letters", "C. S. Lewis", 1942) list_of_books.add_to_end(tpp) list_of_books.insert_after(tkam, tsl) # Print our list to the console list_of_books.print_list() # Reverse our list list_of_books.reverse() list_of_books.print_list() # Manipulate our list list_of_books.remove(thg) # Removes thg from list print("List still contains 'The Hunger Games': " + str(list_of_books.contains(thg))) # Check if thg is indeed gone also_tpp = list_of_books.poll_first() # poll() methods remove and return a list element