0
class Doubly_Linked_List:
    #O(1)
    def __init__(self,value=None):
        if value==None:
            self.head = {'value':None,'prev':None,'next':None}
            self.tail = None
            self.length = 0
        else:
            self.head = {'value':value,'prev':None,'next':None}
            self.tail = self.head
            self.length = 1
    #O(1)
    def __repr__(self):
        return f'head:{self.head}\ntail:{self.tail}\nlength:{self.length}'
    def _navigate_to_pointer(self,index):
        pointer_before_insertion = self.head
        for i in range(index-1):
            pointer_before_insertion = pointer_before_insertion['next']
        return pointer_before_insertion
    #O(1)
    def _make_node(self,value):
        return {'value':value,'prev':None,'next':None}
    def append(self,value):
        if self.length==0:
            self.head = self._make_node(value)
            self.tail = self.head
            self.length+=1
            return None
        new_last_node = self._make_node(value)
        new_last_node['prev'] = self.tail
        self.tail['next'] = new_last_node
        self.tail = new_last_node
        self.length+=1

#test
a=Doubly_Linked_List()
for i in range(2):
    a.append(i)
print(a)
print(a.tail)
print(a.head)

I am trying to implement doubly linked list using dictionary data type in python 3.8. I am trying to test my code but I can't see the full output.

screenshot of current output

martineau
  • 119,623
  • 25
  • 170
  • 301
Algela
  • 1
  • 1
  • 2
    Although [this](https://stackoverflow.com/questions/13851581/python-printed-a-list-three-dots-appeared-inside-sublists) refers to lists, I wonder if your three dots mean that your dictionaries have a references to themselves which is then probably a bug in your construction of the linked list. – DarrylG May 16 '20 at 00:14
  • No it's not a bug of my implementation of Doubly Linked List. According to the answer of this question python was avoiding the output. And I tested the answer. My implementation is correct so far. – Algela May 17 '20 at 22:43

1 Answers1

0

This is actually not a bug. I'm including some test code and output to clarify. What Python is doing is not print out any object that was already printed in order to avoid an infinite print out.

dlist = Doubly_Linked_List()
dlist.append(1)
dlist.append(2)
print(dlist)

Outputs the following.

head:{'value': 1, 'prev': None, 'next': {'value': 2, 'prev': {...}, 'next': None}}
tail:{'value': 2, 'prev': {'value': 1, 'prev': None, 'next': {...}}, 'next': None}
length:2
  • Notice that when printing the head first the ... is in place of the prev of the last item, because it was already printed.
  • Notice that when printing from tail (going backwards) then the next of the eventually reached first item is replaced with ... because it was already printed out (the item with value 2).

The code, formatted, and also I gave you an __iter__ method that would help you print out every item.

class Doubly_Linked_List:
    #O(1)
    def __init__(self,value=None):
        if value==None:
            self.head = {'value':None,'prev':None,'next':None}
            self.tail = None
            self.length = 0
        else:
            self.head = {'value':value,'prev':None,'next':None}
            self.tail = self.head
            self.length = 1
    #O(1)
    def __repr__(self):
        return f'head:{self.head}\ntail:{self.tail}\nlength:{self.length}'
    def _navigate_to_pointer(self,index):
        pointer_before_insertion = self.head
        for i in range(index-1):
            pointer_before_insertion = pointer_before_insertion['next']
        return pointer_before_insertion
    #O(1)
    def _make_node(self,value):
        return {'value':value,'prev':None,'next':None}
    def append(self,value):
        if self.length==0:
            self.head = self._make_node(value)
            self.tail = self.head
            self.length+=1
            return None
        new_last_node = self._make_node(value)
        new_last_node['prev'] = self.tail
        self.tail['next'] = new_last_node
        self.tail = new_last_node
        self.length+=1
    def __iter__(self):
        pointer = self.head
        while pointer:
            yield pointer["value"]
            pointer = pointer["next"]

dlist = Doubly_Linked_List()
dlist.append(1)
dlist.append(2)
#dlist.append(3)
print(dlist)

for item in dlist:
    print(item)
ubershmekel
  • 11,864
  • 10
  • 72
  • 89