Saha
Saha

Reputation: 9

My self-defined queue not printing properly

I'm currently doing a class assignment to create a Class Queue using linked list. My code is as follows:

class Node:
def __init__(self, data):
    self.data = data
    self.next = None
    self.prev = None
class Queue:
def __init__(self):
    self.front = None
    self.rear = None
def enqueue(self, value):
    newNode = Node(value)
    if self.rear is None:
        self.front = newNode
        self.rear = self.front
    else:
        self.rear.next = newNode
        self.rear.next.prev = self.rear
        self.rear = self.rear.next
def dequeue(self):
    if self.front is None:
        return None
    else:
        to_remove = self.front.data
        self.front = self.front.next
        self.front.prev = None
        return to_remove

def printQueue(self):
    print('The elements in the queue are ')
    element = self.front
    while element is not None:
        print(element, end = "")
        element = element.next
myqueue = Queue()
for i in range(21):
   if i % 3 == 0:
      myqueue.enqueue(i)
   elif i % 5 == 0:
      myqueue.dequeue()
myqueue.printQueue()

But when I tried printing, it comes up like this:

<__main__.Node object at 0x000001EB465D8048><__main__.Node object at 0x000001EB465D8128><__main__.Node object at 0x000001EB465D8080><__main__.Node object at 0x000001EB465D80F0>

I searched on the internet and tried to change my code but it still shows up the same. I don't understand why though.

Upvotes: 0

Views: 87

Answers (3)

afterburner
afterburner

Reputation: 2791

First of all, that is not an error. Your printQueue function iterates the elements and prints each element. The type of each element is Node. For example, your output contains the representation of the nodes: The object below is a single node, defined in the __main__ function. Its type is Node Object and its memory address is 0x000001EB465D8048

<__main__.Node object at 0x000001EB465D8048>

To print a user friendly version of each node, the node class needs to override the __str__ method.

For example, if you would modify your Node class to have the following structure, then you could just use print(element, end = "\n") inside your printQueue method.

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

    def __str__(self):
        return str(self.data)

The output then would be

The elements in the queue are 
9
12
15
18

Furthermore, while not directly related to your question, it's tangential enough that you might find it helpful. Python objects have both a __str__ and a __repr__method. Simply put, the __str__ is used to represent the readable form of your object, and the __repr__ the unambiguous representation.

For more information on those 2 methods, this stackoverflow post does a good job at explaining them.

Upvotes: 1

Viswa
Viswa

Reputation: 326

When you print out element you print the location of the object's memory.

<__main__.Node object at 0x000001EB465D8048><__main__.Node object at 0x000001EB465D8128><__main__.Node object at 0x000001EB465D8080><__main__.Node object at 0x000001EB465D80F0>

The hexadecimal values are addresses.

In order to access the value present at these locations use element.data instead of element.

Upvotes: 1

Andre
Andre

Reputation: 47

As the comment says, and I modified your questions itself, too. You have to print the value of the objects, not the objects themselves. It's not an error, just a little confusion on your part.

Upvotes: 2

Related Questions