-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackList.py
43 lines (31 loc) · 890 Bytes
/
stackList.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
41
42
43
# Implementing Stack using python Lists
# Time and Space complexity of all methods is O(1) - constant
class Stack:
# create stack
def __init__(self):
self.list = []
# printing as string
def __str__(self):
values = self.list.reverse()
values = [str(x) for x in self.list]
return '\n'.join(values)
def isEmpty(self):
if self.list == []:
return True
else:
return False
def push(self, value):
self.list.append(value)
return "Element pushed"
def pop(self):
if self.isEmpty():
return "Stack is empty"
else:
return self.list.pop()
def peek(self):
if self.isEmpty():
return "Stack is empty"
else:
return self.list[len(self.list)-1]
def delete(self):
self.list = None