[Python] Print the contents of a Doubly Linked List

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to print the contents of a Doubly Linked list using the str method by converting the content into string. The problem I face is that it returns the memory location of the values, not the values themselves.

    This is my node class. class Node:

    def __init__(self, initial_data, prev = None, next = None):
    self._initial_data = initial_data
    self._prev = prev
    self._next = next


    Here is my str function:

    def __str__(self):
    s = "["
    node = self._head
    while node is not None:
    s += str(node._initial_data)
    if node._next:
    s += "->"
    node = node._next
    s += "]"
    return s

    Continue reading...

Compartilhe esta Página