-2

I got this school assignment, here is my code:

class Doubly_linked_node():
    def __init__(self, val):
        self.value = val
        self.next = None
        self.prev = None

    def __repr__(self):
        return str(self.value)

class Deque():
    def __init__(self):
        self.header = Doubly_linked_node(None)
        self.tailer = self.header
        self.length = 0

    def __repr__(self):
        string = str(self.header.value)
        index = self.header
        while not (index.next is None):
            string+=" " + str(index.next.value)
            index = index.next
        return string


    def head_insert(self, item):
        new = Doubly_linked_node(item)

        new.next=self.header
        self.header.prev=new

        self.header=new
        self.length+=1

        if self.tailer.value==None:
            self.tailer = self.header

    def tail_insert(self, item):
        new = Doubly_linked_node(item)

        new.prev=self.tailer
        self.tailer.next=new

        self.tailer=new
        self.length+=1

        if self.header.value==None:
            self.header = self.tailer

it builds a stack, allowing you to add and remove items from the head or tail (I didn't include all the code only the important stuff).

When I initiate an object, if I return self.next it prints None, but if I return self.prev, it prints nothing, just skips, I don't understand why since they are both defined exactly the same as you see, and if I insert only head several times for example for i in range(1,5): D.head_insert(i) and then I print D it prints 5 4 3 2 1 None but if I do tail insert for example for i in range(1,5): D.tail_insert(i) and print D it prints 1 2 3 4 5"as it should without the None. Why is that?

I have included an image:

image

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tallb
  • 21
  • 6
  • 1
    as a rule of thumb, use "is None" instead of == None: http://stackoverflow.com/questions/3257919/is-none-vs-none – dfranca May 06 '16 at 18:47
  • 1
    Please see http://stackoverflow.com/help/mcve -- questions about code should contain the smallest possible amount of code needed to reproduce the same issue. – Charles Duffy May 06 '16 at 18:47
  • There is no `head` and `tail` method in the `Deque` class – Günther Jena May 06 '16 at 18:50
  • Thank you for your fast response, I will mind it in future questions. However i have tried both is and == and either way it prints None while it shouldn't. Also note this is not the Deque python class its a made up class. – Tallb May 06 '16 at 18:51
  • I have copied such a large segment of code because it is all imperative to the understanding of the problem in my eyes. – Tallb May 06 '16 at 18:57
  • It would be a lot easier for you to debug this if you weren't defining `__repr__` in such a misleading way. – user2357112 May 06 '16 at 19:17
  • That is how we are asked to define the class in the home assignment. – Tallb May 06 '16 at 19:26

3 Answers3

1
  1. Keep in mind that you create a Deque which is not empty. You're initializing it with a Node with value None

  2. You're interchanging the value and the Node object. When you're checking if self.tailer.value==None: it's probably not what you're meaning

  3. Following to point 2 is a special handling for the empty Deque, where header and tailer is None

Here is what I have in mind, if I would implement the Deque. I'm slightly changed the return value of __repr__.

class Deque():
    def __init__(self):
        self.header = None
        self.tailer = None
        self.length = 0

    def __repr__(self):
        if self.header is None:
          return 'Deque<>'
        string = str(self.header.value)
        index = self.header.next
        while index!=None:
            string+=" " + str(index.value)
            index = index.next
        return 'Deque<'+string+'>'

    def head_insert(self, item):
        new = Doubly_linked_node(item)

        new.next=self.header
        if self.length==0:
          self.tailer=new
        else:
          self.header.prev=new

        self.header=new
        self.length+=1

    def tail_insert(self, item):
        new = Doubly_linked_node(item)

        new.prev=self.tailer
        if self.length==0:
          self.header=new
        else:
          self.tailer.next=new

        self.tailer=new
        self.length+=1
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
  • the `class Doubly_linked_node():` is given as part of the exercise that is why I must insert a certain value into it. how would you suggest i'd initiate a `Deque` it if not by inserting a `None`? – Tallb May 06 '16 at 19:08
  • Thank you Gunther that is a fine solution, by not initiating the `Deque` as a `Doubly_linked_node` and avoiding the node reference to the None; I'll point out that the reason I did not use the length for all the =='s is that it was a latter addition for another portion of the assignment (initially I only had header and tailer.) – Tallb May 06 '16 at 19:35
  • I still think i'd stick to the `while not (str(index.next) == "None"):` because its quicker fix...(although a very ugly one) – Tallb May 06 '16 at 19:37
1

Following Günthers advice, I have modified the __repr__ to this:

def __repr__(self):
    string = str(self.header.value)
    index = self.header
    while not (str(index.next) == "None"):
        string += (" " + str(index.next.value))
        index = index.next
    return string

that did solve the problem, but it is the ugliest solution I have ever seen.

does anyone know a better way?

Günther Jena
  • 3,706
  • 3
  • 34
  • 49
Tallb
  • 21
  • 6
0

Following to the question of a better __repr__ method here my proposal. Extend the Deque class with an __iter__ method. So you can iterate over the Deque which is nice to have, e.g.:

for item in D:
  print item

Based on that the __repr__ method is easy. Here is the whole change:

def __repr__(self):
  return 'Deque<'+' '.join([str(item.value) for item in self])+'>'

def __iter__(self):
  index=self.header
  while index is not None:
    yield index.value
    index=index.next
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
  • The functions as pre defined, i am not allowed to add any, just to fill em up with content. – Tallb May 06 '16 at 19:59