Blossom Project

I am working on the Blossom Project found here:

Blossom Project Link

To practice my skills with building node and linked lists, I am building everything from scratch within VS Code.

I am running into some errors and they are probably from the differences in how I coded my linked_list library vs what the codecademy version has.

image

How can I get access to the linked_list library that is imported so I can compare to my own work? It would be a huge help.

1 Like

Has a previous lesson covered linked lists? Your imported Node and Linked_List classes would be almost identical to those you have used in those lessons. If not they’d be found on the computer science path and possibly some smaller lessons too (e.g. data structures) though I’m afraid I don’t know which.

1 Like

Hello, @joselvelez,

To get access to the files that are provided, click the folder icon in the upper left corner of the editor pane. Then you will see a list of files. Click the name of a file to open it in the editor, so that you can see its contents.

   
Screen Shot 2020-09-18 at 5.56.45 AM

1 Like

Oh wow! I feel dumb not noticing that until you mentioned it!

Thanks! This is exactly what I needed.

Since it is related to this project and this library, I’ll just ask here if that is ok.

I am looking at the insert method for the LinkedList class below and am trying to make sure I understand what is happening here. I’ve inserted comments below.

class LinkedList:
  def __init__(self, head_node=None):
    self.head_node = head_node
  
  def insert(self, new_node):
# Set the variable current_node to the head node.
    current_node = self.head_node

# If the list is empty and there is no head node,
# insert new_node as first node and set as head. Exit the method.
    if not current_node:
      self.head_node = new_node

# If current_node is not empty, iterate through while current_node
# has a value other than None and do the following
    while(current_node):
# Set the variable next_node equal to the current node's next node
      next_node = current_node.get_next_node()
      if not next_node:
        current_node.set_next_node(new_node)
      current_node = next_node

I am stuck at the last 3 lines. I do not fully understand what is happening here. I interpret it as:

If next_node has no value or is None, then set the current_nodes’s next node to the new_node. That does not make sense. Shouldn’t the current node become the new_node’s next node?

Unless this method is actually inserting the new node at the END of the list. Is that what it is doing?

1 Like

Yes, it is inserting the new Node at the end of the LinkedList.

You can add some code that displays information about what is going on in order to verify this. Modify the insert method as follows:

  def insert(self, new_node):
    current_node = self.head_node

    if not current_node:
      self.head_node = new_node
      print("first if block executed")

    while(current_node):
      print("while loop iterated")
      next_node = current_node.get_next_node()
      if not next_node:
        current_node.set_next_node(new_node)
      current_node = next_node

Add a display method, as follows, that displays all values in the LinkedList, from the head to the tail:

  def display(self):
    for item in self:
      print(item)

The above works due to the __iter__ method.

Then add this at the end of the file:

ll = LinkedList()
ll.insert(Node("Alpha"))
ll.display()
ll.insert(Node("Beta"))
ll.insert(Node("Gamma"))
ll.display()

Output:

first if block executed
Alpha
while loop iterated
while loop iterated
while loop iterated
Alpha
Beta
Gamma

Note the order of the values in the LinkedList.

Thanks! I was so hung up on expecting the node to be inserted at the beginning that it was keeping me scratching my head. It all makes sense now!

1 Like