-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImplement_stack.py
More file actions
54 lines (38 loc) · 1012 Bytes
/
Implement_stack.py
File metadata and controls
54 lines (38 loc) · 1012 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## Implement Stack From Scratch
class Stack():
def __init__(self):
self.item = []
# Check Stack Is Empty Or Not
def isEmpty(self):
return self.item == []
# Push Data
def push(self, item):
self.item.append(item)
# Pop Data
def pop(self):
return self.item.pop()
def peek(self):
# return self.item[-1]
return self.item[len(self.item)-1]
# Check size of stack
def size(self):
return len(self.item)
stack = Stack()
# Check stack is empty or not
print(stack.isEmpty())
# push some int and string in stack
stack.push("Hello Data!")
stack.push(4)
# check now stack is empty or not
print(stack.isEmpty())
# check size of stack
print(stack.size())
print("------")
print(stack.peek())
print("------")
# pop(remove) data from stack
stack.pop()
stack.pop()
# now again check stack is empty or not and size of stack
print(stack.isEmpty())
print(stack.size())