| 4 | self.next = None |
| 5 | |
| 6 | class Stack: |
| 7 | def __init__(self): |
| 8 | self.top = None |
| 9 | |
| 10 | def push(self, data): |
| 11 | print(f"Adding {data} to the top of the stack") |
| 12 | |
| 13 | # If there is no data, we add the value in the top element and return |
| 14 | if self.top == None: |
| 15 | self.top = Node(data) |
| 16 | return |
| 17 | new_node = Node(data) |
| 18 | new_node.next = self.top |
| 19 | self.top = new_node |
| 20 | |
| 21 | def pop(self): |
| 22 | # If there is no data in the top node, we return |
| 23 | if self.top == None: |
| 24 | print ("There is no item on the stack to unstack") |
| 25 | return |
| 26 | |
| 27 | print(f"Unstack {self.top.data}") |
| 28 | self.top = self.top.next |
| 29 | |
| 30 | def printData(self): |
| 31 | print("Printing stack: ") |
| 32 | |
| 33 | # Step through the stack and print values |
| 34 | tmp_node = self.top |
| 35 | |
| 36 | while tmp_node != None: |
| 37 | print (f"[{tmp_node.data}]", end = "") |
| 38 | tmp_node = tmp_node.next |
| 39 | |
| 40 | print("") |
| 41 | |
| 42 | stack = Stack() |
| 43 | stack.push('a') |