Tag: element hid else lam bool so generator def directly
Reference Blog: https://www.cnblogs.com/stacklike/p/8284550.html
A simple list-based implementation
#Advanced post-out#a simple stack implemented as a listclassSimplestack:#special attributes to restrict the properties that class can add __slots__= ('__items',) def __init__(self): self.__items= [] defIs_empty (self):returnSelf.__items== [] defPeek (self):returnSelf.__items[Len (self.__items)-1] defsize (self):returnLen (self.__items) defpush (self, item): Self.__items. Append (item)defpop (self): self.__items. Pop ()
View CodeImplementing stacks as a single-linked list
classStackfullexception (Exception):#the exception to throw when full stack PassclassStackemptyexception (Exception):#exception to throw when empty stack PassclassNode:def __init__(Self, Val=none, nxt=None): Self.value= Val#Information FieldSelf.next = NXT#pointer Field def __str__(self):returnStr (self.value)classStack:#Initialize an empty stack def __init__(Self, max=0): Self._top= None#the top element of the stackSelf._max = 0#maximum height of stackSelf.max = max#Maximum stack height that the user will set@propertydefLength (self):ifSelf._top isNone:return0 Node=self._top Count= 1#as long as it is not empty, there is at least one node, so starting from 1 #The next element is determined by a pointer within the node, as long as the current node jumps to the next node and the count is added 1 whileNode.next:node=Node.next Count+ = 1returnCount @propertydefIs_empty (self):returnSelf._top isNone @propertydefIs_full (self):#the condition of full stack is that the maximum height of the stack is not infinite (when setting the maximum, the negative number is converted to 0,0, which represents an infinite size) . #and the current stack is higher than the maximum allowable stack height returnBOOL (Self._max andSelf.length = =Self._max) @propertydefMax (self):returnSelf._max @max. SetterdefMax (self, m): M= Int (m)#possible incoming value is str or float ifM < self.length:#sets whether the value is less than the height of the current stack, or throws an exception RaiseException ('Stack Resize failed, please pop some elements first.') Self._max= 0ifM < 0ElseM#whether the input value is negative or 0, is set to 0, as an infinite size #build a stack from an empty stack by pressing into the incoming iterable defInit (self, iterable=()): if notIterable:#to pass in an iterator object returnSelf._top= Node (Iterable[0])#set its starting element to the top of the stack forIteminchIterable[1::]:#The subsequent elements are also pressed into the stack, each pressed into the stack will be replacednode = Self._top#The top element of the original stack is stored first.Self._top = Node (item)#sets the current element to the top of the stackSelf._top.next = node#Point A set of stacked pointers to the original top of the stack """ | 5 | | 4 | | 3 | | 2 | | 1 | Displayed template""" defShow (self):#the child functions are defined to traverse the stack, where the generator is used def_traversal (Self): node=Self._top whileNode andNode.next:yieldnode Node=Node.next#if there is no yield, the element at the bottom of the stack cannot be traversed, because the last element does not satisfy the condition of the while loop and aborts the iteration yieldnode#<>^ Left/right/center alignment #The generator is also iterative, where the string format method is mapped to each element with a higher-order function Print('\ n'. Join (Map (LambdaX:'| {: ^7}|'. Format (str (x)), _traversal (self)) +'\ n'+ 7 *'-') defpush (self, item):#throws an exception if the stack is full ifSelf.is_full:RaiseStackfullexception ('error:trying to push an item into a full stack.') #if the stack is empty, set the item directly to the top of the stack and return it, because you do not need to set the pointer if notSelf._top:self._top=Node (item)returnnode= Self._top#first fetch to the top of the original stackSelf._top = Node (item)#set item to top of stackSelf._top.next = node#point Pointer to the top of the stack at the top of the stack defpop (self):ifSelf.is_empty:RaiseStackemptyexception ('error:trying to POPs from an empty stack.') Node= Self._top#first fetch to the top of the original stackSelf._top = Self._top.next#set the top of the stack to the next element at the top of the stack returnNode.value#returns the value of the top of the original stack defTop (self):returnSelf._top.valueifSelf._topElseNonedefClear (self):#constructing a new method on a constructed method whileSelf._top:self.pop () s=Stack () s.init ([1, 2, 3, 4, 5]) s.show ()
View Code
A single-linked list implementation of the Python stack