0

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)
1
  • What is e_insert? It is nowhere defined? Commented Jul 28, 2022 at 18:25

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

6 Comments

You have told about mistakes dealing with next can you elaborate it
Can you elaborate about my mistakes with using next ?
There are several. I stopped looking further, because the approach to iterate the list was a bad decision anyway. But for instance, if self.ll.head.next == None is wrong. It should be if self.ll.head == None. But that is just the tip of the iceberg.
if self.ll.head.next == None Then only I can use while loop and be able to remove the last element as I need to stop before the second last element and change its reference
The point is that self.ll.head can be None and self.ll.head.next will lead to an exception then.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.