본문 바로가기

프로그래밍/CS이론

(3)
Circular Queue in Python global SIZE class Q: def __init__(self, front, rear): self.queue = [None] * SIZE self.front = front self.rear = rear def isQueueFull(self): if self.queue[self.circulate(self.rear)] != None: return True else: return False def enQueue(self, data): if self.isQueueFull(): print("Queue is Full ...") else: self.rear = self.circulate(self.rear) self.queue[self.rear] = data def isQueueEmpty(self): if se.. 2022. 3. 29. 00:00
Example using Stack in Python 1. Stack 클래스 정의 from queue import Empty class ArrayStack: def __init__(self): self._data = [] def __len__(self): return len(self._data) def is_empty(self): return len(self._data) == 0 def push(self, e): self._data.append(e) def pop(self): if self.is_empty(): raise Empty('Stack is empty') return self._data.pop() def top(self): if self.is_empty(): raise Empty('Stack is empty') return self._data[-1.. 2022. 3. 19. 10:37
값에 의한 호출(call-by-value)과 참조에 의한 호출(call-by-reference) 코딩에서 기초적이면서도 매우 중요한 개념입니다. 1. call-by-value #include void swap(int x, int y) { int temp = x; x = y; y = temp; } int main() { int a = 2, b = 3; swap(a, b); printf("a = %d, b = %d\n", a, b); return 0; } 위의 코드는 a와 b의 값을 바꾸어(swap) 출력하는 프로그램을 의도하여 작성한 코드입니다. 즉 a = 3, b = 2라는 출력을 기대한 코드입니다. 하지만 위 코드를 실행시켜 보면 이러한 의도대로 작동되지 않음을 확인할 수 있습니다. 이는 미리 정의한 swap 함수에서 매개 변수가 '값에 의한 호출(call-by-value)'의 원리로 실행되기 때.. 2021. 6. 29. 16:00