자료구조
배열로 구현된 스택(클래스)
mickey7
2023. 10. 10. 17:05


class ArrayStack :
#스택의 생성자
def __init__(self, capacity) :
self.capacity = capacity
self.array = [None] * self.capacity
self.top = -1
#스택의 연산들을 멤버 함수로 구현
def isEmpty(self) :
return self.top == -1
def isFull(self) :
return self.top == self.capacity-1
def push(self, e):
if not self.isfull():
self.top += 1
self.array[self.top] = e
else: pass
def pop(self):
if not self.isEmpty():
self.top -= 1
return self.array[self.top+1]
else:pass
def peek(self):
if not self.isEmpty:
return self.array[self.top]
else: pass
함수로 구현한 스택과 차이점
- #1. 함수로 구현한 전역 변수 -> 클래스의 멤버 변수
- #2. 모든 함수 -> 클래스의 메소드(멤버 함수)
- #3. 모든 메소드의 첫 번째 파라미터 = self
- #4. 모든 메소드에서 클래스의 멤버를 사용할 때 self.을 이용해 클래스 내의 변수(데이터 멤버)와 함수(메소드) 표시