Skip to content

Commit 70f070a

Browse files
committed
Added Stack implementation in Python
1 parent 5ba74d9 commit 70f070a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

stack/stack.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Stack:
2+
def __init__(self):
3+
self._data = []
4+
self.size = 0
5+
6+
def push(self, value, *args):
7+
self._data.append(value)
8+
for item in args:
9+
self._data.append(item)
10+
self.size += 1
11+
12+
def pop(self):
13+
self._data.pop(-1)
14+
self.size -= 1
15+
16+
def peek(self):
17+
return self._data[-1]
18+
19+
def printStack(self):
20+
return ('[ ' + ', '.join(map(lambda item: str(item), self._data)) + ']')
21+
22+
myStack = Stack();
23+
myStack.push(5)
24+
myStack.push(10)
25+
myStack.push(89)
26+
print("Peek:", myStack.peek())
27+
myStack.pop()
28+
print("Peek:", myStack.peek())
29+
print(myStack.printStack())
30+

0 commit comments

Comments
 (0)