use a singly linked list create a stack 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 
'''
  
class Node: 
      
    # Class to create nodes of linked list 
    # constructor initializes node automatically 
    def __init__(self,data): 
        self.data = data 
        self.next = None
      
class Stack: 
      
    # head is default NULL 
    def __init__(self): 
        self.head = None
      
    # Checks if stack is empty 
    def isempty(self): 
        if self.head == None: 
            return True
        else: 
            return False
      
    # Method to add data to the stack 
    # adds to the start of the stack 
    def push(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) 
    def pop(self): 
          
        if self.isempty(): 
            return None
              
        else: 
            # Removes the head node and makes  
            #the preceeding one the new head 
            poppednode = self.head 
            self.head = self.head.next
            poppednode.next = None
            return poppednode.data 
      
    # Returns the head node data 
    def peek(self): 
          
        if self.isempty(): 
            return None
              
        else: 
            return self.head.data 
      
    # Prints out the stack      
    def display(self): 
          
        iternode = self.head 
        if self.isempty(): 
            print("Stack Underflow") 
          
        else: 
              
            while(iternode != None): 
                  
                print(iternode.data,"->",end = " ") 
                iternode = iternode.next
            return
          
# 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 

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source