forked from MS06-Project-Team/github_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
34 lines (31 loc) · 830 Bytes
/
stack.py
File metadata and controls
34 lines (31 loc) · 830 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
class Stack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
if not self.empty():
return self.stack.pop()
return -1
def top(self):
if not self.empty():
return self.stack[-1]
return -1
def empty(self):
return True if len(self.stack) == 0 else False
def size(self):
return len(self.stack)
N = int(input())
stack = Stack()
for _ in range(N):
command = input().split()
if command[0] == 'push':
stack.push(command[1])
elif command[0] == 'pop':
print(stack.pop())
elif command[0] == 'top':
print(stack.top())
elif command[0] == 'size':
print(stack.size())
elif command[0] == 'empty':
print(stack.empty())