반응형

 

def stack_ex():
"""리스트를 활용한 stack 구현
append pop 메서드를 이용하면 stack 자료형을 구현할 수 있다."""
stack = []
stack.append(10)
stack.append(20)
stack.append(30)
print("STACK:", stack)
# output : input 방향과 동일
print(stack.pop())
print(stack.pop(-1))
print(stack.pop())
#pop 하기전에 비어있는지 체크
if len(stack) > 0:
print(stack.pop())
else:
print("스택이 비어있음:")
print("STACK:", stack)

def queue_ex():
"""리스트를 응용한 Queue 자료형의 구현 리스트의 append, pop(0)를 이용하면 Queue 구현 가능"""
queue = []
queue.append(1)
queue.append(2)
queue.append(3)
print("QUEUE:", queue)
#output은ㅇ 앞에서부터 : 0번 인덱스
print(queue.pop(0)) #queue는 먼저 넣은것 부터 튀어나와요 [1,2,3] 중에 1번이 튀어나와요
while(len(queue) > 0):
print("Queue item:", queue.pop(0))

if __name__ == "__main__":
#define_list()
#list_opper()
#list_method()
#loop()
#stack_ex()
queue_ex()

반응형

'2020 > Python' 카테고리의 다른 글

Python 튜플(Tuple),packing, unpacking,assignment  (0) 2019.11.19
Python Set(교집합,합집합,차집합)add,remove,discard,update,clear()  (0) 2019.11.19
Python loop  (0) 2019.11.18
Python reverse/ sort/ sorted  (0) 2019.11.18
Python list,copy  (0) 2019.11.18
블로그 이미지

꽃꽂이하는개발자

,