0

I am doing an assignment and it is not going very well. It is my code which is not working super good:

from dataclasses import dataclass

@dataclass
class Node:
    value: int = None
    nxt: Node = None  

@dataclass
class Deque:
    head: Node = None      # First node in queue
    tail: Node = None      # Last node in queue
    size: int = 0

    def add_first(self, n):
        new = Node(n, None)
        if self.head is None:
            self.head = new
            self.tail = new
        self.size += 1


        s = "{ "
        node = self.head
        while node is not None:
            s += str(node.value) + " "
            node = node.nxt
        s += "}"
        return s

    
    def add_last(self, n):
        new = Node(n, None)
        if self.head is None:
            self.head = new
            self.tail = new
        else:
            self.tail.nxt = new
            self.tail = new
        self.size += 1

    def get_last(self):
        if self.tail is None:
            print("Get can't be applied on an empty list")
            return None
        else:
            return self.tail.value       

    def get_first(self):
        if self.head is None:
            print("Get can't be applied on an empty list")
            return None
        else:
            #node = self.head
            return self.head.value

    def remove_first(self):
        if self.head is None:
            print("Remove can't be applied on an empty list")
        elif self.head == self.tail:
            s = self.head.value
            self.head = None
            self.tail = None
            self.size -= 1
            return s
        elif self.size == 1:
            node = self.head
            self.head = self.head.nxt
            self.size -= 1
            return node.value

Output: { 1 2 3 4 5 6 7 8 9 10 } Size: 10 { 20 19 18 17 16 15 14 13 12 11 1 2 3 4 5 6 7 8 9 10 } Size: 20

Update: I found an answer to my question. It was issues regarding def add_first and def remove last as well as def remove first.

  • One thing that jumps out at me is that your `remove_first()` and `remove_last()` methods only handle the cases of the deque being empty, or only containing one item. They completely ignore the usual case of two or more items. – jasonharper Nov 03 '21 at 17:07
  • FYI you can refer to a class within its own definition by using a string, like `'Node'`, rather than with `Any`. See: https://www.python.org/dev/peps/pep-0484/#forward-references and https://stackoverflow.com/a/33533514/6273251 – Random Davis Nov 03 '21 at 17:10

1 Answers1

0

Because this is homework I'm not going to give away the fixed code, but I will point out what needs fixing. I believe I figured out all the required fixes, but I could be wrong since you didn't share the code used to test your Deque:

  1. Your add_first method is unnecessarily stepping through all the Nodes, then setting the last Node (the tail)'s nxt value to the new Node, meaning the new Node will appear at the end of the Deque. Instead, all you need to do is set the nxt of the new Node to the current head, and set head to the new Node.
  2. Your remove_first doesn't account for any case except if there's one Node in the Deque. You have to set the Deque's head to its old head's nxt Node in all other cases.
  3. Your remove_last also doesn't account for any case except if there's one Node in the Deque. In all other cases, you have to loop through the Deque's Nodes, starting with head, until you find a Node whose nxt value is the Deque's tail, then set its nxt to None, and set the Deque's tail to that Node.
    Example of finding the Node before the tail Node:
    node = self.head
    while node.nxt != self.tail:
        node = node.nxt
    node.nxt = None
    self.tail = node
    
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • I changed add_first: `def add_first(self, n):` `new = Node(n, None)` `if self.head is None:` `self.head = new` `self.tail = new` `else:` `new.nxt = self.head` `self.head = new` `self.size += 1` – user17020522 Nov 03 '21 at 18:31
  • @user17020522 it's hard to read code in a comment, better to put it at the end of your question, but regardless, that code's logic looks identical to the version I wrote, so it looks good to me. – Random Davis Nov 03 '21 at 18:42
  • Okay, i did a few updates but still do not understand regarding loop in both removes. 1 minute. i change my post – user17020522 Nov 03 '21 at 19:38
  • "you have to loop through the Deque's Nodes, starting with head, until you find a Node whose nxt value is the Deque's tail, then set its nxt to None, and set the Deque's tail to that node." `remove_last` while self.head is not None: if node.nxt == self.tail: node == None – user17020522 Nov 03 '21 at 19:55
  • @user17020522 I'll add some example code for that. – Random Davis Nov 03 '21 at 20:35
  • i found an answer – user17020522 Nov 03 '21 at 22:51
  • thank you for your kindness – user17020522 Nov 03 '21 at 23:07