2

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

Pavan Nath
  • 1,494
  • 1
  • 14
  • 23
  • 1
    If you want to answer your own question, you should probably [add it as an answer](http://stackoverflow.com/help/self-answer) and not edit your question to include the answer. – NightShadeQueen Jul 24 '15 at 14:52
  • possible duplicate of [How to create a custom string representation for a class object?](http://stackoverflow.com/questions/4932438/how-to-create-a-custom-string-representation-for-a-class-object) – Łukasz Rogalski Jul 24 '15 at 14:54

2 Answers2

8

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.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
0

Change line 13 from

print(a)

to

print(a.data)
trincot
  • 317,000
  • 35
  • 244
  • 286