250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 공분산
- Counterfactual Explanations
- top_k
- Airflow
- requests
- integrated gradient
- airflow subdag
- API
- chatGPT
- subdag
- tensorflow text
- Retry
- grad-cam
- XAI
- TensorFlow
- API Gateway
- hadoop
- youtube data
- session 유지
- flask
- 유튜브 API
- gather_nd
- 상관관계
- spark udf
- login crawling
- UDF
- GenericGBQException
- GCP
- BigQuery
- correlation
Archives
- Today
- Total
데이터과학 삼학년
stack 2개로 queue 만들기 본문
반응형
stack 2개를 이용하면 queue를 만들 수 있다.
stack은 LIFO (Last In First Out)
queue 는 FIFO (First In First Out)
즉, stack을 두개를 이용해서 빈 스택에 다른 스택을 담고 다시 빼는 식으로 queue를 만들 수 있는 것이다.
구현 코드 예시
# stack 2개로 queue 만들기
class Stack:
def __init__(self):
self.stack = []
def __repr__(self):
return f"{self.stack}"
def push(self,item):
self.stack.append(item)
def pop(self):
return self.stack.pop()
def is_empty(self):
if len(self.stack) == 0 : return True
else : return False
st = Stack()
st.push(1)
st.push(2)
st.push(3)
print(st)
[1,2,3]
st.pop()
3
print(st)
[1,2]
class Queue:
def __init__(self):
self.qu = Stack()
self.stack2 = Stack()
def __repr__(self):
return f"{self.qu}"
def push(self,item):
self.qu.push(item)
self.que = self.qu
def pop(self):
while not self.qu.is_empty():
self.stack2.push(self.qu.pop())
result = self.stack2.pop()
while not self.stack2.is_empty():
self.qu.push(self.stack2.pop())
return result
qu = Queue()
qu.push(1)
qu.push(2)
qu.push(3)
print(qu)
[1,2,3]
qu.pop()
1
print(qu)
[2,3]
728x90
반응형
LIST
'Computer Science > Data Structure & Algorithm' 카테고리의 다른 글
동적 계획법(Dynamic Programming)과 분할 정복(Divide and Conquer) (0) | 2021.09.06 |
---|---|
DFS(Depth First Search), BFS(Breadth First Search) - 깊이/너비 우선 탐색 (0) | 2021.01.23 |
피보나치 수열 (0) | 2020.03.04 |
Bubble Sort (버블 정렬) (0) | 2020.01.24 |
Quick Sort(퀵 정렬) (0) | 2020.01.23 |
Comments