classElement(object): def__init__(self, value): self.value = value self.next = None
classLinkedList(object): def__init__(self, head=None): self.head = head
defappend(self, new_element): current = self.head if self.head: while current.next: current = current.next current.next = new_element else: self.head = new_element
definsert_first(self, new_element): "Insert new element as the head of the LinkedList" current = self.head self.head = new_element new_element.next = current
defdelete_first(self): "Delete the first (head) element in the LinkedList as return it" current = self.head.next self.head = current
defpush(self, new_element): "Push (add) a new element onto the top of the stack" self.ll.insert_first(new_element)
defpop(self): "Pop (remove) the first element off the top of the stack and return it" if self.ll.head == None: returnNone current = self.ll.head self.ll.delete_first() return current
defmain(): # Test cases # Set up some Elements e1 = Element(1) e2 = Element(2) e3 = Element(3) e4 = Element(4)