forked from Avery246813579/Computer-Science-Theory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.py
40 lines (28 loc) · 825 Bytes
/
Stack.py
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
34
35
36
37
38
39
40
class Node:
def __init__(self, data, next_node=None):
self.data = data
self.next = next_node
def __repr__(self):
return '[Node: ' + str(self.data) + ']'
class Stack:
def __init__(self, iterator=None):
if iterator is not None:
for item in iterator:
self.push(item)
return
self.head = None
def push(self, data):
new_node = Node(data, self.head)
self.head = new_node
def pop(self):
if self.head is None:
return
old_head = self.head
self.head = self.head.next
return old_head.data
def peak(self):
if self.head is None:
return None
return self.head.data
def __str__(self):
return '[Stack: ' + str(self.head) + ']'