Skip to content

Instantly share code, notes, and snippets.

@pupba
Last active February 2, 2023 08:28
Show Gist options
  • Save pupba/1a40b3f52891b482ae036dbf2d6555a0 to your computer and use it in GitHub Desktop.
Save pupba/1a40b3f52891b482ae036dbf2d6555a0 to your computer and use it in GitHub Desktop.
Stack
# 파이썬은 리스트 자료구조로 이미 스택이 구현되어 있음
# class로 구현하기
class Stack:
def __init__(self):
self.__heap = []
# 파이썬의 리스트의 공간은 동적으로 증가하여
# 컴퓨터 메모리 만큼 증가 하므로 따로 isFull 구현하지 않았음
def len(self):
return len(self.__heap)
# push
def push(self,item): self.__heap.append(item)
# pop
def pop(self):
return self.__heap.pop() if not self.isEmpty() else print("Stack is Empty!")
# peek
def peek(self):
return self.__heap[-1] if not self.isEmpty() else print("Stack is Empty!!")
# isEmpty
def isEmpty(self):
return not self.__heap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment