1

I am learning linked list and I'm creating doubly linked list. My print backward is not working as intended. For better Information I will highlight the print backward function.


Print Backward

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


class LinkedList:
    def __init__(self):
        self.head = None

    def get_last_node(self):
        itr = self.head
        while itr.next:
            itr = itr.next
        return itr

        def print_backward(self):
        if self.head is None:
            print("Linked List is empty")
            return
        last = self.get_last_node()
        itr = last
        llstr = ''
        while itr:
            llstr += itr.data + '-->'
            itr = itr.prev
        print(llstr)

I hope that my code can be resolved...

1

2 Answers 2

0

Your linked list seems to be working as intended when you fix the indentation issue and make sure node.data is converted to string before printing. Otherwise, I think there's probably an issue with initializing the list

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


class LinkedList:
    def __init__(self):
        self.head = None

    def get_last_node(self):
        itr = self.head
        while itr.next:
            itr = itr.next
        return itr

    def print_backward(self):
        if self.head is None:
            print("Linked List is empty")
            return
        last = self.get_last_node()
        itr = last
        llstr = ''
        while itr:
            llstr += str(itr.data) + '-->'
            itr = itr.prev
        print(llstr)


# Sample test
lst = LinkedList()

nodes = [Node(data=x) for x in range(10)]

nodes[0].next = nodes[1]

for i in range(1, 9):
    nodes[i].prev = nodes[i - 1]
    nodes[i].next = nodes[i + 1]

nodes[-1].prev = nodes[-2]

lst.head = nodes[0]

lst.print_backward()
# Output: 9-->8-->7-->6-->5-->4-->3-->2-->1-->0-->
Sign up to request clarification or add additional context in comments.

Comments

0

With a doubly linked list you typically need facilities to both prepend and append.

Override __str__ in the Node class for easier printing.

Implement / override __reversed__ and __iter__ for easy traversal of the linked list

Something like this:

class Node:
    def __init__(self, _data):
        self._data = _data
        self._next = self._prev = None

    def __str__(self):
        return f"{self._data}"


class LinkedList:
    def __init__(self):
        self._head = None
        self._tail = None

    def __reversed__(self):
        temp = self._tail
        while temp:
            yield temp
            temp = temp._prev

    def __iter__(self):
        temp = self._head
        while temp:
            yield temp
            temp = temp._next

    def append(self, _node):
        _node._prev = self._tail
        _node._next = None
        if self._tail:
            self._tail._next = _node
        else:
            self._head = _node
        self._tail = _node

    def prepend(self, _node):
        _node._next = self._head
        _node._prev = None
        if self._head:
            self._head._prev = _node
        else:
            self._tail = _node
        self._head = _node

LL = LinkedList()

LL.append(Node(3))
LL.append(Node(4))
LL.append(Node(5))
LL.prepend(Node(2))
LL.prepend(Node(1))

print('Reversed:')
print(*reversed(LL), sep='-->')
print('Normal:')
print(*LL, sep='-->')

Output:

Reversed:
5-->4-->3-->2-->1
Normal:
1-->2-->3-->4-->5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.