-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack.py
38 lines (32 loc) · 1.05 KB
/
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
'''
This is the implementation of stack using python3.So, stack works on principle
'Last in First Out (FILO)'.This implementation uses adapter design pattern to
adapt list class of python3.
'''
class Stack():
def __init__(self):
self._stack = [] # empty list to store items
def size(self):
'''return length of stack'''
return len(self._stack)
def push(self,item):
'''to add items in stack'''
self._stack.append(item)
def pop(self):
'''to remove the last item from stack'''
if len(self._stack) == 0:
print('Stack is Empty')
else:
return self._stack.pop()
def top(self):
'''it's only return the last inserted or top item of the stack'''
if len(self._stack) == 0:
print('Stack is Empty')
else:
return self._stack[-1]
def is_empty(self):
'check stack is empty or not.'
if len(self._stack) == 0:
print('Stack is empty')
else:
print('Stack is not empty')