Home > Enterprise >  code should not return any node object(Element)
code should not return any node object(Element)

Time:07-29

https://gist.github.com/manaidu-2002/3f7eb60b8521201eba6548ca23cec053

Code returning Node Object(Element), please check the test cases and help me with this

"""Add a couple methods to our LinkedList class, and use that to implement a Stack. You have 4 functions below to fill in: insert_first, delete_first, push, and pop. Think about this while you're implementing: why is it easier to add an "insert_first" function than just use "append"?"""

class Element(object):
    def __init__(self, value):
        self.value = value
        self.next = None
        
class LinkedList(object):
    def __init__(self, head=None):
        self.head = head
        
    def append(self, new_element):
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = new_element
        else:
            self.head = new_element

    def insert_first(self, new_element):
        "Insert new element as the head of the LinkedList"
        new_element.next = self.head
        self.head = e_insert
        

    def delete_first(self):
        "Delete the first (head) element in the LinkedList as return it"
        temp = self.head
        if temp == None:
            return None
        s= temp
        self.head = temp.next
        return s

class Stack(object):
    def __init__(self,head=None):
        self.ll = LinkedList(head)

    def push(self, new_element):
        "Push (add) a new element onto the top of the stack"
        temp = self.ll.head
        while temp.next :
            temp = temp.next
        temp.next = new_element
            

    def pop(self):
        "Pop (remove) the first element off the top of the stack and return it"
        
        
         if self.ll.head.next == None :
            temp = self.ll.head
            e= temp
            temp = None
            return e
        elif self.ll.head.next:
            temp = self.ll.head
            while temp.next.next:
                   temp = temp.next
            e= temp.next
            temp.next = None
            return e
        return None
    
# Test cases
# Set up some Elements
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)

# Start setting up a Stack
stack = Stack(e1)

# Test stack functionality
stack.push(e2)
stack.push(e3)
print(stack.pop().value)
print(stack.pop().value)
print(stack.pop().value)
print(stack.pop())
stack.push(e4)
print(stack.pop().value)

CodePudding user response:

First of all, there is an unused name reference in your code: e_insert. This should read new_element.

The main issue is that your Stack class is not re-using the code you already have in your LinkedList class. In the new code you have written there are several mistakes in how you deal with next, but taking a step back, you are making a critical mistake in thinking that the top of the stack should be at the tail of the linked list, but that is very inefficient. The top of the stack should be at the head of the linked list. It is at that side that you can easily remove and insert elements without having to iterate the list.

So take these points into consideration:

  • Reuse the code you already have for LinkedList. In other words, call the methods defined on the LinkedList class.

  • The top of the stack is at the head of the linked list.

That means the Stack class can be as simple as this:

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

    def push(self, new_element):
        self.ll.insert_first(new_element)
            
    def pop(self):
        return self.ll.delete_first()
  • Related