use linkedlist class to implement a stack class elementobject def initself value self value value self next none class linkedlistobject def initself headnone self head head def appendself newelement current self head if self head while current next current current next current next newelement else self head newelement def insertfirstself newelement insert new element as the head of the linkedlist fetch the current head current self head newelement next current self head newelement def deletefirstself delete the first head element in the linkedlist and return it current self head if current if current next self head current next else self head none return current else return none class stackobject def initself topnone self ll linkedlisttop def pushself newelement push add a new element onto the top of the stack self ll insertfirstnewelement def popself pop remove the first element off the top of the stack and return it return self ll deletefirst test cases set up some elements e1 element1 e2 element2 e3 element3 e4 element4 start setting up a stack stack stacke1 test stack functionality stack pushe2 stack pushe3 printstack pop value printstack pop value printstack pop value printstack pop stack pushe4 printstack pop value fetch the current head test cases set up some elements start setting up a stack test stack functionality
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"
        
        current = self.head
        new_element.next = current
        self.head = new_element

    def delete_first(self):
        "Delete the first (head) element in the LinkedList and return it"
        current = self.head
        if current:
            if current.next:
                self.head = current.next
            else:
                self.head = None
            return current
        else:
            return None

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

    def push(self, new_element):
        "Push (add) a new element onto the top of the stack"
        self.ll.insert_first(new_element)

    def pop(self):
        "Pop (remove) the first element off the top of the stack and return it"
        return self.ll.delete_first()
    


e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)


stack = Stack(e1)


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)