Pavan Nath
Pavan Nath

Reputation: 1494

Error: <__main__.Node object at 0x03A5F990> while using linked-lists

The following is a linked list implementation using Python:

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

class List:
    head=None
    tail=None
    def printlist(self):
        print("list")
        a=self.head
        while a is not None:
                print(a)
                a=a.next

    def append(self, data):
        node = Node(data, None)
        if self.head is None:
            self.head = self.tail = node
        else:
            self.tail.next = node
        self.tail = node

p=List()
p.append(15)
p.append(25)
p.printlist()

Output:

list
<__main__.Node object at 0x03A9F970>
<__main__.Node object at 0x03A9F990>

To check your answer you need to edit this inbuilt method def __repr__ and rewriting it.

You can also do this by adding __str__ method

Upvotes: 2

Views: 15837

Answers (2)

Anand Kothari
Anand Kothari

Reputation: 21

Change line 13 from

print(a)

to

print(a.data)

Upvotes: 0

Mason Wheeler
Mason Wheeler

Reputation: 84550

This is not an error. You're seeing exactly the output you're asking for: two Node objects.

The problem is that you haven't defined __repr__ or __str__ on your Node class, and so there's no intuitive way to print out the value of the node objects. All it can do is punt and give you the default, which is rather unhelpful.

Upvotes: 8

Related Questions