Linked List Methods #
This lab is for practicing implementing Linked List methods in context.
Syllabus Topics [HL] #
- B4.1.2 Evaluate linked lists. (HL only)
- B4.1.3 Construct and apply linked lists: singly, doubly and circular. (HL only)
[0] Set up #
๐ป Clone your repo. This will copy it onto your computer.
Be sure to replace yourgithubusername with your actual username.
cd ~/desktop/dpcs/unit03_oop
git clone https://github.com/isf-dp-cs/lab_linked_list_methods_yourgithubusername
cd lab_linked_list_methods_yourgithubusername
๐ป Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.
poetry shell
๐พ ๐ฌ Exiting the poetry shellWhen you want to exit the shell, you can type
exitor^D
[1] Whiny Babies #
Create a whiny babies game that will allow each player to add complaints to the front of the linked list.
You will have to complete the implementation of a singly LinkedList in linked_list.py.
add_front() #
Each complaint should be smaller and stupider (more trivial) than the previous one. This should use linked_list.add_front().
๐ป Completeadd_front().
remove_front() #
The other player can reject the most recent addition if its not sufficiently whiny/trivial. This should use linked_list.delete_front()
๐ป Completedelete_front().
printing #
๐ป Finish the method __str__ which will allow you to print() the linked list to show the complaints.
Whiny Babies Game #
๐ป Create a the game in whiny_babies.py.
You can utilize the Complaints class too store your complaints, if desired, to increase the complexity.
The game should alternate turns between two players. If one players things the other’s complaint isn’t sufficiently whiny, they can reject it, and it should get removed from the front of the list, and the whole list should be printed.
[2] Collaborative Story #
The collaborative story is similar to whiny babies, but new additions get added onto the end to create a narrative.
It will also use the LinkedList class.
Required methods #
๐ป Create a method add_last() which adds a new Node to the end of the linked list.
๐ป Create a method delete_last() which removes the last Node from the end of the linked list.
Story Game #
๐ป Create a the game in whiny_babies.py.
You can create a new class to store the story part objects, if desired.
[3] Spinning Game #
This game will utilize multiple circular linked lists to simulate a spinning game.
Create a spinning game that uses multiple linked lists to store various emojis. The game should allow players to trigger a random spinning, that utilizes the nature of a circular linked list to simulate continuous spinning.
What methods will your circular linked lists nee
๐ป Create a method that will allow you to insert data into your circular linked list.
๐ป Create the spinning game.
[4] Hue #
This game is loosely based on the game I ๐ Hue
Pretty Print #
As you might recall from the string manipulation/Wordle lab at the beginning of the year, you can print text with background colors. If you add this pretty_print() method to your linked list class, it will print each number in your linked list with its corresponding background color.
๐ป Add a pretty_print() to your LinkedList class.
def pretty_print(self):
result = ""
reset = "\u001b[0m"
current = self.__head
while current is not None:
n = current.get_data()
color = f"\u001b[48;5;{n}m {n:3} {reset}"
result += color
current = current.get_next()
result += "\n"
print(result)
๐ป Create a new file hue.py, and test pretty_print to make sure it works as expected.
Insert In Order #
๐ป Create a method insert(data) in your singly linked list which inserts a new Node in the correct (in order) location in the linked list.
๐ป Then edit your hue.py code to use this new method.
Verify Order #
๐ป Create a method in_order() in your singly linked list which returns True or False depending on whether the numbers in the linked list are in order or not.
Test this out using your previous code, and see if it can correctly identify a linked list that is ordered.
Swap #
๐ป Create a method swap(x,y) in your singly linked list. It should locate the node containing the data x and y, and swap them in the linked list.
for example:
131 132 134 133 130
swap(132,134)
130 131 132 133 134
๐ป Edit your hue.py code to make a game that lets users continually swap the colors until it’s in order.
[5] Deliverables #
โกโจ Once you finish the lab, be sure to complete these two steps:๐ Update Syllabus Checklist: Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.
๐ป Push your work to Github
- git status
- git add -A
- git status
- git commit -m “describe your code here”
- git push
- remote