0

I can't solve this task on a project I'm working on could you take a look? I have to only complete the .insert(), without altering any of the already provided code. if you could also then fully explain to me the logic of the .insert() function it would be great!

Below are Node and DoublyLinkedList class implementations used to create doubly linked lists. Currently, the DoublyLinkedList class has an unfinished .insert() method used to add new_node at location pos.

Complete the .insert() method so that new_node is inserted before current_node. Ensure that your doubly linked list can be traversed forward and backward.

For example, given a DoublyLinkedList instance my_list, calling my_list.insert(1, 'b') would add a Node at position 1 of my_list with a value of b.

Do not alter the existing class attributes and methods, but feel free to use additional code to test your work. some code has already been written that will help you test your code.

class DoublyLinkedList:
  def __init__(self):
    self.head_node = None
    self.tail_node = None
  
  def insert(self, pos, new_value):
    if pos == 0:
      self.add_to_head(new_value)
    else:
      current_node = self.head_node
      for i in range(pos):
        if current_node.get_next_node() is None:
          self.add_to_tail(new_value)
          return
        current_node = current_node.get_next_node()
      
      new_node = Node(new_value)

      # Add code below
      

  def add_to_head(self, new_value):
    new_head = Node(new_value)
    current_head = self.head_node

    if current_head != None:
      current_head.set_prev_node(new_head)
      new_head.set_next_node(current_head)

    self.head_node = new_head

    if self.tail_node == None:
      self.tail_node = new_head

  def add_to_tail(self, new_value):
    new_tail = Node(new_value)
    current_tail = self.tail_node

    if current_tail != None:
      current_tail.set_next_node(new_tail)
      new_tail.set_prev_node(current_tail)

    self.tail_node = new_tail

    if self.head_node == None:
      self.head_node = new_tail

  def print(self):
    current_node = self.head_node
    while current_node is not None:
      print(f"{current_node.get_value()}", end=" ")
      current_node = current_node.get_next_node()
    print()

class Node:
  def __init__(self, value, next_node=None, prev_node=None):
    self.value = value
    self.next_node = next_node
    self.prev_node = prev_node
    
  def set_next_node(self, next_node):
    self.next_node = next_node
    
  def get_next_node(self):
    return self.next_node

  def set_prev_node(self, prev_node):
    self.prev_node = prev_node
    
  def get_prev_node(self):
    return self.prev_node
  
  def get_value(self):
    return self.value
  
dll = DoublyLinkedList()
dll.add_to_head('a')
dll.add_to_tail('c')
dll.print()
dll.insert(1, 'b')
dll.print()

1
  • What exactly is unclear to you about what the method needs to do? Commented May 6, 2023 at 20:42

1 Answer 1

0

Your code just needs to set the four links to/from the new node:

  • The prev/next links from new_node to its neighbors
  • The links from those neighbors back to new_node

So add these statements:

prev_node = current_node.get_prev_node()
# Set forward/backward link with the new node's successor:
new_node.set_next_node(current_node)
current_node.set_prev_node(new_node)
# Set forward/backward link with the new node's predecessor:
new_node.set_prev_node(prev_node)
prev_node.set_next_node(new_node)
Sign up to request clarification or add additional context in comments.

3 Comments

this works but the task ask that we do not replace any existing statement. should we not be using the pos argument to find the correct place to insert the node?
The code has already used the pos argument. All that is left is to set the four links. It is a pity you are not allowed to use the power of the Node constructor, but of course you can set the links after the node has been constructed. See update of answer.
great this answer is perfect thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.