-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
33 lines (29 loc) · 798 Bytes
/
stack.py
File metadata and controls
33 lines (29 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Node:
def __init__(self, data = None, next = None):
self.data = data
self.next = next
class Stack:
def __init__(self):
self.head = None
self.size = 0
def is_empty(self):
return not bool(self.size)
def push(self, data):
new_node = Node(data, self.head)
self.head = new_node
self.size += 1
return data
def pop(self):
data = self.head.data
self.head = self.head.next
self.size -= 1
return data
def __str__(self):
if self.is_empty():
return 'Stack is empty!'
itr = self.head
output = ''
while itr:
output += str(itr.data) + ' '
itr = itr.next
return output