0

I'm a beginner to linked lists in python. I'm trying to write a simple program to count the number of nodes in the list, but I keep running into this error.

I've initialized the data and next pointer in the __init__ function, but the function count_nodes does not seem to be recognizing it.

The error I receive:

```Runtime Exception
Traceback (most recent call last):
File "file.py", line 72, in <modules>
print(count_nodes(biscuit_list))
File "file.py", line 59, in count_nodes
top=biscuit_list.__head
AttributeError: 'LinkedList'object has no attribute '__head'```

```#lex_auth_012742478130135040816

class Node:
    def __init__(self,data):
        self.__data=data
        self.__next=None
    
    def get_data(self):
        return self.__data
    
    def set_data(self,data):
        self.__data=data
    
    def get_next(self):
        return self.__next
    
    def set_next(self,next_node):
        self.__next=next_node
    
class LinkedList:
    def __init__(self):
        self.__head=None
        self.__tail=None
    
    def get_head(self):
        return self.__head
    
    def get_tail(self):
        return self.__tail
    
    def add(self,data):
        new_node=Node(data)
        if(self.__head is None):
            self.__head=self.__tail=new_node
        else:
            self.__tail.set_next(new_node)
            self.__tail=new_node
    
    def display(self):
        temp=self.__head
        while(temp is not None):
            print(temp.get_data())
            temp=temp.get_next()
                                              
    #to print the elements of the DS object while debugging
    def __str__(self):
        temp=self.__head
        msg=[]
        while(temp is not None):
           msg.append(str(temp.get_data()))
           temp=temp.get_next()
        msg=" ".join(msg)
        msg="Linkedlist data(Head to Tail): "+ msg
        return msg

def count_nodes(biscuit_list):
    count=0
    top=biscuit_list.__head
    while(top.get_next):
        count+=1
        top=top.get_next

    return count

biscuit_list=LinkedList()
biscuit_list.add("Goodday")
biscuit_list.add("Bourbon")
biscuit_list.add("Hide&Seek")
biscuit_list.add("Nutrichoice")

print(count_nodes(biscuit_list))
                                 ```
beetroot
  • 1
  • 2

1 Answers1

0

__head is private since it is starting with __. Check this answer Double underscore in python.

Instead please use the get_head method to get that value

Ritwik G
  • 406
  • 2
  • 8
  • Is this "new" with python3? I'm using classes really rarely in Python (say: never), but I think I remember that in Python2 there was no such thing as private variables, it was only used to tell the developer that it's meant to be private. Maybe I'm also wrong about that :) – Tobias Brösamle Mar 16 '21 at 13:43
  • I haven't used classes much in python 2. So I don't remember exactly. But in python3 this is the case. I have been using the private variables recently. – Ritwik G Mar 16 '21 at 16:56
  • I just tested. Looks like it is true for python 2 as well. But it is said not truly private since it is actually accessible as explained in the link I attached. So in your case you should be able to access `__head` by `biscuit_list._LinkedList__head`. But recommended way is to use the a getter method. One way you can know what are the accessible variables and methods are by using command `dir(biscuit_list)` – Ritwik G Mar 16 '21 at 17:02