파이썬 스택2 배열로 구현된 스택(클래스) 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 retur.. 2023. 10. 10. 배열을 이용한 스택(함수) (python으로 구현) # 스택의 데이터: 전역 변수 capacity = 10 #배열의 용량을 10으로 고정 array = [None] * capacity #스택의 요소들을 저장할 배열 top = -1#상단의 인덱스(공백상태(-1)로 초기화) #스택의 연산 : 일반 함수 def isEmpty() : if top == -1 : return True else : return False def isFull() : return top == capacity-1 def push(e): global top if not isFull() : top += 1 array[top] = e else: print("stack overflow") exit() def pop(): global top if not isEmpty():.. 2023. 10. 10. 이전 1 다음