简体   繁体   中英

how to add a node after a given node in circular linked list?

Below is the code snippet to add a node after a given node. however, it's not working. As I understand the node is added in the same reference the way we add node in singly linked list. So, with few changes I tried to implement, but no use.

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

class cirLL:
    def __init__(self):
        self.lst = None
        
    def addEnd(self, val):
        nu = Node(val)
        if self.lst is None:
            self.lst = nu
        else:
            nu.nxt = self.lst.nxt
            self.lst.nxt = nu
            self.lst = nu
    
  
    def addaftr(self, pos, val):
        tmp = self.lst.nxt
        while tmp != self.lst:
            if tmp.data == pos:
                break
            tmp = tmp.nxt
        if tmp is None:
            print(pos, "not present")
        else:
            nu = Node(val)
            nu.nxt = tmp.nxt
            tmp.nxt = nu
    
    def disply(self):
        if not self.lst:
            return  # nothing to print
        tmp = self.lst.nxt
        print(tmp.data)
        while tmp != self.lst:
            tmp = tmp.nxt
            print(tmp.data)
                
               
rew = cirLL()
rew.addaftr(30, 456)
rew.addEnd(23)
rew.addEnd(30)
rew.addEnd(90)
rew.disply()

I am getting below error:

AttributeError: 'NoneType' object has no attribute 'nxt'

Not sure what could have gone wrong. Please suggest.

The problem is quite clear:

    tmp = self.lst.nxt

The above statement assumes that self.lst is not None , but initially it is. Hence, you get the error you got.

Just like the addEnd method does, you should first test if self.lst is maybe None and act accordingly.

You can fix it as follows:

def addaftr(self, pos, val):
    if self.lst is None:
        print(pos, "not present")
        return
    # rest of your code

Other remarks:

  • The name pos is misleading. First I thought the method was intended to insert a node at a position in the list, but it turns out that pos is a list value and not a position .

  • It is a bit odd that you want to insert a node after value 30 , when there is no node in your list yet. Did you really intend that?

  • Another issue is that this condition will never be true:

     if tmp is None

    As your list is circular, tmp will just hit the lst node, but it will never become None . Moreover, your loop does not check whether maybe the last node in the list has the data you're looking for. That if should be replaced with:

     if tmp.data:= pos:
  • When the value to find happens to be at the last node, then after the insertion you need to adapt the lst reference to the newly inserted node. So at the end of the else block, add this:

     if tmp == self.lst: self.lst = nu

Here is the corrected code:

    def addaftr(self, findval, val):
        if self.lst is None:
            print(findval, "not present")
            return
        tmp = self.lst.nxt
        while tmp != self.lst:
            if tmp.data == findval:
                break
            tmp = tmp.nxt
        if tmp.data != findval:
            print(findval, "not present")
        else:
            nu = Node(val)
            nu.nxt = tmp.nxt
            tmp.nxt = nu
            if tmp == self.lst:
                self.lst = nu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM