use a singly linked list create a stack python

Solutions on MaxInterview for use a singly linked list create a stack python by the best coders in the world

showing results for - "use a singly linked list create a stack python"
Blossom
02 Oct 2019
1'''Python supports automatic garbage collection so deallocation of memory 
2is done implicitly. However to force it to deallocate each node after use, 
3add the following code: 
4  
5    import gc         #added at the start of program 
6    gc.collect()     #to be added wherever memory is to be deallocated 
7'''
8  
9class Node: 
10      
11    # Class to create nodes of linked list 
12    # constructor initializes node automatically 
13    def __init__(self,data): 
14        self.data = data 
15        self.next = None
16      
17class Stack: 
18      
19    # head is default NULL 
20    def __init__(self): 
21        self.head = None
22      
23    # Checks if stack is empty 
24    def isempty(self): 
25        if self.head == None: 
26            return True
27        else: 
28            return False
29      
30    # Method to add data to the stack 
31    # adds to the start of the stack 
32    def push(self,data): 
33          
34        if self.head == None: 
35            self.head=Node(data) 
36              
37        else: 
38            newnode = Node(data) 
39            newnode.next = self.head 
40            self.head = newnode 
41      
42    # Remove element that is the current head (start of the stack) 
43    def pop(self): 
44          
45        if self.isempty(): 
46            return None
47              
48        else: 
49            # Removes the head node and makes  
50            #the preceeding one the new head 
51            poppednode = self.head 
52            self.head = self.head.next
53            poppednode.next = None
54            return poppednode.data 
55      
56    # Returns the head node data 
57    def peek(self): 
58          
59        if self.isempty(): 
60            return None
61              
62        else: 
63            return self.head.data 
64      
65    # Prints out the stack      
66    def display(self): 
67          
68        iternode = self.head 
69        if self.isempty(): 
70            print("Stack Underflow") 
71          
72        else: 
73              
74            while(iternode != None): 
75                  
76                print(iternode.data,"->",end = " ") 
77                iternode = iternode.next
78            return
79          
80# Driver code 
81MyStack = Stack() 
82  
83MyStack.push(11)  
84MyStack.push(22) 
85MyStack.push(33) 
86MyStack.push(44) 
87  
88# Display stack elements  
89MyStack.display() 
90  
91# Print top element of stack  
92print("\nTop element is ",MyStack.peek()) 
93  
94# Delete top elements of stack  
95MyStack.pop() 
96MyStack.pop() 
97  
98# Display stack elements 
99MyStack.display() 
100  
101# Print top element of stack  
102print("\nTop element is ", MyStack.peek())  
103  
104# This code is contributed by Mathew George