def partition(ll, ref_node): res = LinkedList() for node in ll: if node.value < ref_node.value: res.add_to_beginning(node.value) else: res.add(node.value) return res
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( ) # poll() methods remove and return a list element
also_tpp = list_of_books.poll_first() # poll() methods remove and return a list element also_tkam = list_of_books.poll(1) # Can poll() from an index too also_podg = list_of_books.poll_last() list_of_removed_books = [also_tpp, also_tkam, also_podg, thg] for book in list_of_removed_books: # Make sure our books left the list. if (list_of_books.contains(book)): print("Our list contains something it shouldn't") print("Checking list: ") # Take a look at the list now. list_of_books.print_list() for i in list_of_removed_books: # Now add our books back into the list sorted by year. for j in range(0, list_of_books.size()): if (i.year_published <= list_of_books.peek_first().year_published): list_of_books.add_to_beginning(i) continue if (i.year_published >= list_of_books.peek(j).year_published): if (list_of_books.size() > j + 1): if (i.year_published <= list_of_books.peek(j + 1).year_published): list_of_books.insert_after(list_of_books.peek(j), i) else: list_of_books.add_to_end(i) print("Checking list again: ") # Now lets look at the list again. list_of_books.print_list() # Lastly, clear our list list_of_books.clear_list() # Note: there are other methods in linked_list.py that are not used here.
# This file tests methods from linked_list.py from linked_list import LinkedList # Create new LinkedLists 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()
sweepstakes.add_contestant("Nate", "Johnson") sweepstakes.add_contestant("Paige", "Johnson") sweepstakes.add_contestant("Mason", "Johnson") sweepstakes.get_winner() family = Family() family.add_member("Nate", "Johnson", "Me") family.add_member("Paige", "Johnson", "Wife") print(family.members) linked_list = LinkedList() linked_list.append_node(5) linked_list.append_node(30) linked_list.append_node(65) linked_list.add_to_beginning(1) linked_list.add_to_beginning(3) print(linked_list.contains_node(linked_list.head, 65)) print(linked_list.contains_node(linked_list.head, 64)) new_tree = Binary_Tree(50) new_tree.add_node(75, new_tree) new_tree.add_node(25, new_tree) new_tree.add_node(15, new_tree) new_tree.add_node(17, new_tree) new_tree.add_node(80, new_tree) new_tree.add_node(79, new_tree) print(new_tree.search_for_node(80, new_tree)) print(new_tree.search_for_node(23, new_tree)) new_tree.in_order(new_tree)