-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathStack.py
More file actions
63 lines (51 loc) · 1.19 KB
/
Stack.py
File metadata and controls
63 lines (51 loc) · 1.19 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Stack:
def __init__(self):
'''
Default constructor
'''
self.elements = []
def push(self, element):
'''
Push element to the stack
:param element:
'''
self.elements.insert(0, element)
def pop(self):
'''
Pop top element of the stack
:return: element
'''
if not self.isEmpty():
return self.elements.pop(0)
else:
return None
def peek(self):
'''
Peek element at the top of the stack
:return: element
'''
if not self.isEmpty():
return self.elements[0]
else:
return None
def isEmpty(self):
'''
Check if stack is empty
:return: true if empty, false otherwise
'''
if len(self.elements) == 0:
return True
else:
return False
def size(self):
'''
Size of the stack
:return: number of elements in the stack
'''
return len(self.elements)
def tolist(self):
'''
Returns stack as a list
:return: list
'''
return self.elements