mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Create __init__.py
Initialising of a Stack Class, has three methods: is_empty, push and pop.
This commit is contained in:
parent
0dbd2df11b
commit
4eddeb9396
23
data_structures/Stacks/__init__.py
Normal file
23
data_structures/Stacks/__init__.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
class Stack:
|
||||
|
||||
def __init__(self):
|
||||
self.stack = []
|
||||
self.top = 0
|
||||
|
||||
def is_empty(self):
|
||||
return self.top == 0
|
||||
|
||||
def push(self, item):
|
||||
if self.top < len(self.stack):
|
||||
self.stack[self.top] = item
|
||||
else:
|
||||
self.stack.append(item)
|
||||
|
||||
self.top += 1
|
||||
|
||||
def pop(self):
|
||||
if self.is_empty():
|
||||
return None
|
||||
else:
|
||||
self.top -= 1
|
||||
return self.stack[self.top]
|
Loading…
Reference in New Issue
Block a user