Te damos el arreglo a esta impedimento, al menos eso deseamos. Si tienes alguna interrogante dínoslo, para nosotros será un gusto ayudarte
Ejemplo: use una lista enlazada individualmente cree una pila de python
'''Python supports automatic garbage collection so deallocation of memory
is done implicitly. However to force it to deallocate each node after use,
add the following code:
import gc #added at the start of program
gc.collect() #to be added wherever memory is to be deallocated
'''classNode:# Class to create nodes of linked list # constructor initializes node automatically def__init__(self,data):
self.data = data
self.next=NoneclassStack:# head is default NULL def__init__(self):
self.head =None# Checks if stack is empty defisempty(self):if self.head ==None:returnTrueelse:returnFalse# Method to add data to the stack # adds to the start of the stack defpush(self,data):if self.head ==None:
self.head=Node(data)else:
newnode = Node(data)
newnode.next= self.head
self.head = newnode
# Remove element that is the current head (start of the stack) defpop(self):if self.isempty():returnNoneelse:# Removes the head node and makes #the preceeding one the new head
poppednode = self.head
self.head = self.head.next
poppednode.next=Nonereturn poppednode.data
# Returns the head node data defpeek(self):if self.isempty():returnNoneelse:return self.head.data
# Prints out the stack defdisplay(self):
iternode = self.head
if self.isempty():print("Stack Underflow")else:while(iternode !=None):print(iternode.data,"->",end =" ")
iternode = iternode.nextreturn# Driver code
MyStack = Stack()
MyStack.push(11)
MyStack.push(22)
MyStack.push(33)
MyStack.push(44)# Display stack elements
MyStack.display()# Print top element of stack print("nTop element is ",MyStack.peek())# Delete top elements of stack
MyStack.pop()
MyStack.pop()# Display stack elements
MyStack.display()# Print top element of stack print("nTop element is ", MyStack.peek())# This code is contributed by Mathew George
Sección de Reseñas y Valoraciones
Si aceptas, eres capaz de dejar una noticia acerca de qué te ha gustado de esta división.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)