Linked-list add_before method

when I try add before(use method:-> add_before() <-) element he give me a error
this error :slight_smile:

go to last code I add some comment :point_down:

#todo:  create Linked List:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

    def __repr__(self):
        return self.data


class LinkedList:
    def __init__(self, nodes=None):
        self.head = None
        #-: make linked list much faster
        if nodes is not None:
            node = Node(data=nodes.pop(0))
            self.head = node
            for elem in nodes:
                node.next = Node(data=elem)
                node = node.next

    #-: Return a string
    def __repr__(self):
        node = self.head
        nodes = []
        while node is not None:
            nodes.append(node.data)
            node = node.next
        nodes.append('None')
        return ' ->'.join(nodes)

    #-: to loop to all list if you add all list at one
    def __iter__(self):
        node = self.head
        while node is not None:
            yield node
            node = node.next

    #-: add new node to the head
    def add_first(self, node):
        node.next = self.head
        self.head = node

    #-: add the new node to the last
    def add_last(self, node):
        if self.head is None:
            self.head = node
            return
        for current_node in self:
            pass
        current_node = node

    #-: adds a node afer an existing node with specific data value
    def add_after(self, target_node_data, new_node):
        if self.head is None:
            raise Exception('List is empty')

        for node in self:
            if node.data == target_node_data:
                new_node.next = node.next
                node.next = new_node
                return
        raise Exception("Node with data '%s' not found "% target_node_data)

    #-: add before existing node
    def add_before(self, target_node_data, new_node):
        if self.head is None:
            raise Exception('List is empty')

        #: add node before the head
        if self.head.data == target_node_data:
            return self.add_first(new_node)
      # in example -> prev_node = b
     #prev_node.next = c
      #target_node_data = G
      
        prev_node = self.head
        for node in self:
            if node.data == target_node_data:
                # here I make prev_node.next = G
                prev_node.next = new_node
                #here is the error , I want make (G) to point to (c)
                new_node.next = node
                return
            prev_node = node

        raise Exception("Node With data '%s' not found"% target_node_data)

The code seems fine, I guess the method call is wrong.
Do your debug printing to find out what’s going on. The error suggests that new_node is a string when it blows up…

you are correct I was call method like this llists.add_before(β€˜c’, β€˜G’) ← this wrong
I fix it β†’ llists.add_before(β€˜c’, Node(β€˜G’))
I forget create node! :grinning_face_with_smiling_eyes:
どうも

A free service run by Zed A. Shaw for learncodethehardway.org.