📌 Stack
class Stack:
def __init__(self):
self.len = 0
self.list = []
def push(self, num):
self.list.append(num)
self.len += 1
def pop(self):
if self.size() == 0:
return -1
pop_result = self.list[self.len - 1]
del self.list[self.len - 1]
self.len -= 1
return pop_result
def size(self):
return self.len
def empty(self):
return 1 if self.len == 0 else 0
def top(self):
return self.list[-1] if self.size() != 0 else -1
📌 List
List로 구현
//stack = [3, 4, 5]
stack.append(6)
stack.append(7)
// stack [3, 4, 5, 6, 7]
stack.pop() //7
//stack [3, 4, 5, 6]
stack.pop() //6
stack.pop() //5
// stack [3, 4]