Home > Back-end >  why am not able to display data if inserting a new node in a empty circular lined list?
why am not able to display data if inserting a new node in a empty circular lined list?

Time:09-28

Below is the code snippet to add node in two scenario

  • if the circular linked list is empty
  • Add node at the end of the circular linked list
    class Node:
        def __init__(self, data):
            self.data = data
            self.start = None
    
    class cLL:
        def __init__(self):
            self.start = Node(None)
            self.last = Node(None)
            self.start.nxt = self.last
            self.last.nxt = self.start
            
        def addEmpty(self, val):
            nu = Node(val)
            if self.start.data == None:
                self.start = nu
                self.last = nu
                nu.nxt = self.start
            return nu
                
        def addEnd(self, val):
            nu = Node(val)
            if self.start == None:
                print("no nodes at the beginning")
            else:
                self.last.nxt = nu
                self.last = nu
                nu.nxt = self.start
                
        def display(self):
            tmp = self.start
            while tmp.nxt != self.last.nxt:
                tmp = tmp.nxt
                print(tmp.data)
                
    rew = cLL()
    rew.addEmpty(23)
    rew.addEnd(30)
    rew.addEnd(90)
    rew.addEnd(900)
    rew.display()

But the function "addEmpty()" is not working. The display function is not able to show it. Please suggest

CodePudding user response:

Several issues:

  • A circular list should not be initiated with two nodes (having None as data), but with no nodes.
  • When adding a node to an empty list, you should not assign that node reference to both start and last, as that gives you a duplicate node for no good reason. If then later you mutate start, you'll be mutating the node that also last references, and vice versa. This will almost always have undesired effects.
  • A Node instance should not get a start attribute in its constructor, but should instead get an initialised nxt attribute.
  • The display method is printing not printing the data of the start node, but it is printing the data of the last node. This is confusing. That function should display all node's data.

For a circular list you actually don't need both a start and last reference. Either the list is empty, or else the start node will always be the one referenced by last.nxt (if there are no errors). So that means you can do with only the last attribute and drop the start attribute. This will also save you some code.

It is not clear why you would have a different method for adding a node to an empty list, than for adding a node to a non-empty list. Just use one method and make the distinction with an if, which anyway you already have for checking the list is empty or not.

Here is a corrected version:

class Node:
    def __init__(self, data):
        self.data = data
        # A node has a nxt attribute, not start.
        # As a circular list NEVER has None-references, make this a self refence
        self.nxt = self

class cLL:
    def __init__(self):
        self.last = None  # Don't create a node here! No need for `start`
        
    def addEnd(self, val):  # One method, not two
        nu = Node(val)
        if self.last is None:
            self.last = nu
        else:
            nu.nxt = self.last.nxt
            self.last.nxt = nu
            self.last = nu
            
    def display(self):
        if not self.last:
            return  # nothing to print
        tmp = self.last.nxt
        print(tmp.data)
        while tmp != self.last:
            tmp = tmp.nxt
            print(tmp.data)
        
rew = cLL()
rew.addEnd(23)
rew.addEnd(30)
rew.addEnd(90)
rew.addEnd(900)
rew.display()

CodePudding user response:

Your display function is incorrect, you are never printing the start value as you go to the next one before printing. An example answer would be to add a print statement before the while loop starts. Hope this helps:

def display(self):
    tmp = self.start
    print(tmp.data)
    while tmp.nxt != self.last.nxt:
        tmp = tmp.nxt
        print(tmp.data)
  • Related